How do I temporarily suppress error Messages in SSMS?
The following command attempts to reseed the identity value for all tables in the current database.
exec sp_MSforeachtable @command1 = 'DBCC CHECKIDENT(''?'', RESEED, 1)'
An error message is generated for each t开发者_运维百科able that does not have an identity column.
Msg 7997, Level 16, State 1, Line 1 'MyTable' does not contain an identity column.
I would like to suppress all error messages for this single line of code above, but not suppress them for the rest of the code in the SQL file.
Any ideas?
You can do something like this (untested, but I think you'll get the gist):
declare tbls cursor for
select object_name([object_id])
from sys.identity_columns
where objectproperty([object_id], 'IsMSShipped') = 0
declare @tbl_name sysname, @cmd nvarchar(max)
open tbls
while(1=1)
begin
fetch next from tbls into @tbl_name
if(@@fetch_status <> 0)
break
set @cmd = 'DBCC CHECKIDENT(''' + @tbl_name + ''', RESEED, 1)'
exec(@cmd)
end
close tbls
deallocate tbls
精彩评论