標題:SQL server 數據庫使用整理
1.字符串表名多次查詢
2.讀取SQL中Json字段中的值:JSON_VALUE(最新版本支持,屬性名大小寫敏感)
1.字符串表名多次查詢
SELECT ROW_NUMBER() OVER (ORDER BY value ASC) rowid,value tname into #t1 FROM STRING_SPLIT('CCN_VendTransStaging,CCN_SpecTransStaging', ',')
Declare?? @row int,? --行記錄數
@count int--總記錄數
Declare @tname varchar(100)--table name
select @count=COUNT(1),@row =1 from #t1
while @row <=? @count --循環開始
BEGIN
select @tname=tname from #t1 where rowid=@row --當前列的數據
declare @s nvarchar(1000) set @s = 'select '''+@tname+''' as '''+ @tname+''',COUNT(*) counts from? '+@tname
print(@s)
exec(@s) -- 成功
set @row=@row +1
END
drop table #t1
2.讀取SQL中Json字段中的值:JSON_VALUE(最新版本支持,屬性名大小寫敏感)
--{"Type":"Info","Data":{"Info":{"Name":"hljTest128"}}}
SELECT top 11 JSON_VALUE(message,'$.Data.Info.Name')as no FROM tableMessage