【发布时间】:2011-02-16 14:40:24
【问题描述】:
我们正在从 SQL Server 2005 升级到 2008。几乎 2005 实例中的每个数据库都设置为 2000 兼容模式,但我们正在跳转到 2008。我们的测试已经完成,但我们了解到,我们需要加快速度。
我发现了一些存储过程,它们要么从丢失的表中选择数据,要么尝试对不存在的列进行排序。
包装 SQL 以在 SET PARSEONLY ON 中创建过程并在 try/catch 中捕获错误只会捕获 ORDER BY 中的无效列。它没有发现从缺失表中选择数据的过程的错误。然而,SSMS 2008 的智能感知确实找到了问题,但我仍然可以继续并成功运行该过程的 ALTER 脚本而不会抱怨。
那么,为什么我什至可以创建一个运行时失败的过程呢?有没有比我尝试过的工具做得更好的工具?
我发现的第一个工具不是很有用:DbValidator from CodeProject,但它发现的问题比我在 SqlServerCentral 上找到的这个脚本少,后者发现了无效的列引用。
-------------------------------------------------------------------------
-- Check Syntax of Database Objects
-- Copyrighted work. Free to use as a tool to check your own code or in
-- any software not sold. All other uses require written permission.
-------------------------------------------------------------------------
-- Turn on ParseOnly so that we don't actually execute anything.
SET PARSEONLY ON
GO
-- Create a table to iterate through
declare @ObjectList table (ID_NUM int NOT NULL IDENTITY (1, 1), OBJ_NAME varchar(255), OBJ_TYPE char(2))
-- Get a list of most of the scriptable objects in the DB.
insert into @ObjectList (OBJ_NAME, OBJ_TYPE)
SELECT name, type
FROM sysobjects WHERE type in ('P', 'FN', 'IF', 'TF', 'TR', 'V')
order by type, name
-- Var to hold the SQL that we will be syntax checking
declare @SQLToCheckSyntaxFor varchar(max)
-- Var to hold the name of the object we are currently checking
declare @ObjectName varchar(255)
-- Var to hold the type of the object we are currently checking
declare @ObjectType char(2)
-- Var to indicate our current location in iterating through the list of objects
declare @IDNum int
-- Var to indicate the max number of objects we need to iterate through
declare @MaxIDNum int
-- Set the inital value and max value
select @IDNum = Min(ID_NUM), @MaxIDNum = Max(ID_NUM)
from @ObjectList
-- Begin iteration
while @IDNum <= @MaxIDNum
begin
-- Load per iteration values here
select @ObjectName = OBJ_NAME, @ObjectType = OBJ_TYPE
from @ObjectList
where ID_NUM = @IDNum
-- Get the text of the db Object (ie create script for the sproc)
SELECT @SQLToCheckSyntaxFor = OBJECT_DEFINITION(OBJECT_ID(@ObjectName, @ObjectType))
begin try
-- Run the create script (remember that PARSEONLY has been turned on)
EXECUTE(@SQLToCheckSyntaxFor)
end try
begin catch
-- See if the object name is the same in the script and the catalog (kind of a special error)
if (ERROR_PROCEDURE() <> @ObjectName)
begin
print 'Error in ' + @ObjectName
print ' The Name in the script is ' + ERROR_PROCEDURE()+ '. (They don''t match)'
end
-- If the error is just that this already exists then we don't want to report that.
else if (ERROR_MESSAGE() <> 'There is already an object named ''' + ERROR_PROCEDURE() + ''' in the database.')
begin
-- Report the error that we got.
print 'Error in ' + ERROR_PROCEDURE()
print ' ERROR TEXT: ' + ERROR_MESSAGE()
end
end catch
-- Setup to iterate to the next item in the table
select @IDNum = case
when Min(ID_NUM) is NULL then @IDNum + 1
else Min(ID_NUM)
end
from @ObjectList
where ID_NUM > @IDNum
end
-- Turn the ParseOnly back off.
SET PARSEONLY OFF
GO
【问题讨论】:
-
也许通过运行自动化回归测试,您会发现问题所在并能够在存储过程级别进行追踪。
-
您也可以尝试 SET NOEXEC ON 并执行每个存储的过程,但我发现这也不会出现很多错误。 EXEC sys.sp_refreshsqlmodule 部分成功地捕获了一些错误。我发现唯一可靠的方法是运行一个工具来实际执行每个存储过程,表值函数为每个参数传递 NULL,然后将其回滚并记录任何失败。 (并且也从每个视图中进行选择)显然,这取决于您的程序有多耗时以及它们的确切性质,这将如何运作。
-
@John -- 哈!自动回归测试......不适用于这些数据库。近两年来,我们一直在努力清理这个客户的烂摊子!不过我同意,这是可行的方法,但我们没有时间开发测试。
-
嗨!您是否使用存储过程的导出/导入或 sys.sp_refreshsqlmodule 进行了测试(请参阅我的答案)?它适用于您的数据库吗?
-
在 DBA 站点上查看 this answer。您可以使用
sys.procedures代替sys.views。
标签: sql sql-server tsql sql-server-2008 syntax