验证T-SQL存储过程的可靠方法

前端之家收集整理的这篇文章主要介绍了验证T-SQL存储过程的可靠方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们正在从sql Server 2005升级到2008.几乎2005实例中的每个数据库都设置为2000兼容模式,但我们跳到2008年.我们的测试已经完成,但我们学到的是我们需要得到的它更快.

我发现了一些存储过程,它们从缺少的表中选择数据或尝试ORDER BY不存在的列.

包装sql以在SET PARSEONLY ON中创建过程并在try / catch中捕获错误仅捕获ORDER BY中的无效列.在从缺失表中选择数据的过程中找不到错误.然而,SSMS 2008的智能感知会找到问题,但我仍然可以继续并成功运行ALTER脚本,而不会抱怨它.

那么,为什么我甚至可以创建一个在运行时失败的程序呢?那里有什么工具比我尝试过的更好吗?

我找到的第一个工具不是很有用:DbValidator from CodeProject,但它找到的问题比我在sqlServerCentral上找到的脚本少,后者发现了无效的列引用.

  1. -------------------------------------------------------------------------
  2. -- Check Syntax of Database Objects
  3. -- Copyrighted work. Free to use as a tool to check your own code or in
  4. -- any software not sold. All other uses require written permission.
  5. -------------------------------------------------------------------------
  6. -- Turn on ParSEOnly so that we don't actually execute anything.
  7. SET PARSEONLY ON
  8. GO
  9.  
  10. -- Create a table to iterate through
  11. declare @ObjectList table (ID_NUM int NOT NULL IDENTITY (1,1),OBJ_NAME varchar(255),OBJ_TYPE char(2))
  12.  
  13. -- Get a list of most of the scriptable objects in the DB.
  14. insert into @ObjectList (OBJ_NAME,OBJ_TYPE)
  15. SELECT name,type
  16. FROM sysobjects WHERE type in ('P','FN','IF','TF','TR','V')
  17. order by type,name
  18.  
  19. -- Var to hold the sql that we will be Syntax checking
  20. declare @sqltocheckSyntaxFor varchar(max)
  21. -- Var to hold the name of the object we are currently checking
  22. declare @ObjectName varchar(255)
  23. -- Var to hold the type of the object we are currently checking
  24. declare @ObjectType char(2)
  25. -- Var to indicate our current location in iterating through the list of objects
  26. declare @IDNum int
  27. -- Var to indicate the max number of objects we need to iterate through
  28. declare @MaxIDNum int
  29. -- Set the inital value and max value
  30. select @IDNum = Min(ID_NUM),@MaxIDNum = Max(ID_NUM)
  31. from @ObjectList
  32.  
  33. -- Begin iteration
  34. while @IDNum <= @MaxIDNum
  35. begin
  36. -- Load per iteration values here
  37. select @ObjectName = OBJ_NAME,@ObjectType = OBJ_TYPE
  38. from @ObjectList
  39. where ID_NUM = @IDNum
  40.  
  41. -- Get the text of the db Object (ie create script for the sproc)
  42. SELECT @sqltocheckSyntaxFor = OBJECT_DEFINITION(OBJECT_ID(@ObjectName,@ObjectType))
  43.  
  44. begin try
  45. -- Run the create script (remember that PARSEONLY has been turned on)
  46. EXECUTE(@sqltocheckSyntaxFor)
  47. end try
  48. begin catch
  49. -- See if the object name is the same in the script and the catalog (kind of a special error)
  50. if (ERROR_PROCEDURE() <> @ObjectName)
  51. begin
  52. print 'Error in ' + @ObjectName
  53. print ' The Name in the script is ' + ERROR_PROCEDURE()+ '. (They don''t match)'
  54. end
  55. -- If the error is just that this already exists then we don't want to report that.
  56. else if (ERROR_MESSAGE() <> 'There is already an object named ''' + ERROR_PROCEDURE() + ''' in the database.')
  57. begin
  58. -- Report the error that we got.
  59. print 'Error in ' + ERROR_PROCEDURE()
  60. print ' ERROR TEXT: ' + ERROR_MESSAGE()
  61. end
  62. end catch
  63.  
  64. -- Setup to iterate to the next item in the table
  65. select @IDNum = case
  66. when Min(ID_NUM) is NULL then @IDNum + 1
  67. else Min(ID_NUM)
  68. end
  69. from @ObjectList
  70. where ID_NUM > @IDNum
  71.  
  72. end
  73. -- Turn the ParSEOnly back off.
  74. SET PARSEONLY OFF
  75. GO

解决方法

您可以选择不同的方式.首先,sql SERVER 2008支持存储在STORED PROCEDURE的DB包含依赖项中的依赖关系(参见 http://msdn.microsoft.com/en-us/library/bb677214%28v=SQL.100%29.aspx,http://msdn.microsoft.com/en-us/library/ms345449.aspxhttp://msdn.microsoft.com/en-us/library/cc879246.aspx).您可以使用sys.sql_expression_dependencies和sys.dm_sql_referenced_entities来查看和验证.

但是,验证所有STORED PROCEDURE的最简单方法如下:

>出口所有存储过程
>删除旧的现有存储过程
>导入刚出口的STORED PROCEDURE.

如果升级DB,则不会验证现有存储过程,但如果您创建新存储过程,则将验证该过程.因此,在导出和导出所有存储过程后,您将收到报告的所有现有错误.

您还可以使用如下代码查看和导出存储过程的代码

  1. SELECT definition
  2. FROM sys.sql_modules
  3. WHERE object_id = (OBJECT_ID(N'spMyStoredProcedure'))

更新:要查看存储过程spMyStoredProcedure引用的对象(如表和视图),您可以使用以下命令:

  1. SELECT OBJECT_NAME(referencing_id) AS referencing_entity_name,referenced_server_name AS server_name,referenced_database_name AS database_name,referenced_schema_name AS schema_name,referenced_entity_name
  2. FROM sys.sql_expression_dependencies
  3. WHERE referencing_id = OBJECT_ID(N'spMyStoredProcedure');

更新2:在对我的回答的评论中,Martin Smith建议使用sys.sp_refreshsqlmodule而不是重新创建存储过程.所以用代码

  1. SELECT 'EXEC sys.sp_refreshsqlmodule ''' + OBJECT_SCHEMA_NAME(object_id) +
  2. '.' + name + '''' FROM sys.objects WHERE type in (N'P',N'PC')

一个接收脚本,可用于验证存储过程依赖性.输出将如下所示(使用AdventureWorks2008的示例):

  1. EXEC sys.sp_refreshsqlmodule 'dbo.uspGetManagerEmployees'
  2. EXEC sys.sp_refreshsqlmodule 'dbo.uspGetWhereUsedProductID'
  3. EXEC sys.sp_refreshsqlmodule 'dbo.uspPrintError'
  4. EXEC sys.sp_refreshsqlmodule 'HumanResources.uspUpdateEmployeeHireInfo'
  5. EXEC sys.sp_refreshsqlmodule 'dbo.uspLogError'
  6. EXEC sys.sp_refreshsqlmodule 'HumanResources.uspUpdateEmployeeLogin'
  7. EXEC sys.sp_refreshsqlmodule 'HumanResources.uspUpdateEmployeePersonalInfo'
  8. EXEC sys.sp_refreshsqlmodule 'dbo.uspSearchCandidateResumes'
  9. EXEC sys.sp_refreshsqlmodule 'dbo.uspGetBillOfMaterials'
  10. EXEC sys.sp_refreshsqlmodule 'dbo.uspGetEmployeeManagers'

猜你在找的MsSQL相关文章