我正在努力更新数据库上的所有表格,但我无法正确获取代码。我得到了以下代码,但作为以前的解决方案,但我试图使用SP_msforeachtable
,无法获得正确的格式。帮助这将是美好的。这是代码:
USE TPP_DB
GO
DECLARE @SQL NVARCHAR(MAX)
EXEC sp_MSforeachtable
'SELECT @SQL = (
SELECT ''
UPDATE ''' + QUOTENAME(SCHEMA_NAME(o.[schema_id])) + ''.'''' + QUOTENAME(o.name) + '''
SET ''' + QUOTENAME(c.name) + '' = NULL
WHERE '' + QUOTENAME(c.name) + '' = '''''
FROM sys.columns c
JOIN sys.objects o ON c.[object_id] = o.[object_id]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')
PRINT @SQL
EXEC sys.sp_executesql @SQL'
答案 0 :(得分:1)
你可以这样试试,
SELECT 'UPDATE ' + Schema_name(t.schema_id) + '.'
+ t.NAME + ' SET ' + c.NAME + ' =NULL WHERE ' + c.NAME
+ ' ='''';'
FROM sys.tables AS t
INNER JOIN sys.columns c
ON t.OBJECT_ID = c.OBJECT_ID
--WHERE c.NAME = 'ModifiedDate'
ORDER BY Schema_name(t.schema_id),
t.NAME;