clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 1044295 fiddles created (9414 in the last week).

CREATE TABLE T ([欄位1] varchar(4), [欄位2] varchar(4), [欄位3] varchar(4)) ; INSERT INTO T ([欄位1], [欄位2], [欄位3]) VALUES ('20', NULL, '21'), (NULL, NULL, NULL), ('12', '13', '15') ;
3 rows affected
 hidden batch(es)


--設定 declare @table_name nvarchar(100) = 'T'; --填寫要查詢的表格名稱 declare @columns table (column_name nvarchar(200)); --保存表格欄位資料 insert into @columns select T1.COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS T1 with (nolock) left join INFORMATION_SCHEMA.TABLES T2 with (nolock) on T1.TABLE_NAME = T2.TABLE_NAME where 1 =1 and Table_Type = 'BASE TABLE' and T2.TABLE_NAME = @table_name; declare @sql nvarchar(max) = 'select '; select @sql = @sql + column_name + ',' from @columns select @sql = @sql + ' case when ' + column_name + ' is null then 1 else 0 end +' from @columns --執行 set @sql = LEFT(@sql, Len(@sql)-1) + N' as 計算結果' + ' from ' + @table_name; exec sp_executesql @sql
欄位1 欄位2 欄位3 計算結果
20 21 1
3
12 13 15 0
 hidden batch(es)