DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = STUFF((
SELECT '
UNION ALL
SELECT TOP(1) ''' + name + ''' AS tbl FROM ' + name + ' WHERE Manager IS NULL'
FROM (
SELECT name = QUOTENAME(SCHEMA_NAME(o.[schema_id])) + '.' + QUOTENAME(o.name)
FROM sys.objects o
JOIN sys.columns c ON o.[object_id] = c.[object_id]
WHERE o.name LIKE 'temp_table_%'
AND c.name = 'Manager'
AND c.is_nullable = 1
) t
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 13, '')
--PRINT @SQL
EXEC sys.sp_executesql @SQL
輸出 -
SELECT TOP(1) '[dbo].[temp_table_2342]' AS tbl FROM [dbo].[temp_table_2342] WHERE Manager IS NULL
UNION ALL
SELECT TOP(1) '[dbo].[temp_table_234]' AS tbl FROM [dbo].[temp_table_234] WHERE Manager IS NULL
UNION ALL
...
'經理是NULL' - 列或值在此列? – Devart
@Devart值在這一列中。 – alwaysVBNET
所以您的預期輸出只是其中列Manager中的值至少有一個值爲空的表的名稱? – ughai