日期:2014-05-17 浏览次数:20670 次
DECLARE @s NVARCHAR(4000)
SET @s = ''
SELECT @s = @s + ',' + QUOTENAME(name)
FROM syscolumns
WHERE id = OBJECT_ID('表名')
AND name NOT IN ( '不希望显式的列' )
SET @s = STUFF(@s, 1, 1, '')
EXEC('select '+@s+' from 表名 ')
DECLARE @sql NVARCHAR(MAX)
SELECT
@sql=ISNULL(@sql+NCHAR(13)+NCHAR(10),'')+'Update tb Set '+name+'='''' Where '+name+' is null'
FROM sys.syscolumns
WHERE id=object_id('tb')
Exec ('Begin try Begin tran
'+@sql+'
Commit tran
End try
Begin catch
Declare @Error_Message nvarchar(4000)
Set @Error_Message=error_message()
Raiserror 50001 @Error_Message
Rollback tran
End catch')