SQL Server SELECT到JSON函数

我想输出一个SELECT语句的结果作为JSON对象。

我想这是一个function,而不是一个存储过程

例如,下表中的用户

 id name active 1 Bob Jones 1 2 John Smith 0 

会像这样返回:

 [{"id":1,"name":"Bob Jones","active":1},{"id":2,"name":"John Smith","active":0}] 

提前致谢。

你可以使用for xml path ,例如:

 declare @t table(id int, name nvarchar(max), active bit) insert @t values (1, 'Bob Jones', 1), (2, 'John Smith', 0) select '[' + STUFF(( select ',{"id":' + cast(id as varchar(max)) + ',"name":"' + name + '"' + ',"active":' + cast(active as varchar(max)) +'}' from @t t1 for xml path(''), type ).value('.', 'varchar(max)'), 1, 1, '') + ']' 

输出:

 [{"id":1,"name":"Bob Jones","active":1},{"id":2,"name":"John Smith","active":0}] 

只是为了改善最新的技术变化的答案。 与SQL服务器2016年

 select id, name ,active from tableName FOR JSON AUTO 

所以,首先,我要感谢基里尔Polishchuk的基本代码示例…谢谢!

我拿了它,去build立一个程序来做我需要做的事情,那就是给我一个基于“任何”结果集,我想在SQL Server中的表对象(而不是variables)的JSON输出。

理想情况下,我想这是一个函数,但由于限制你可以在一个函数内做的事情,这部分将不得不等待…也许V2。 🙂

是的,注册一个扩展的程序(CLR)肯定比较容易,但是我想暂时避免这个路线。

PS:临时表,只要放在'tempdb ..#tablename'

这里是:

  /* Author: Goran Biljetina Create date: 03/13/2013 Description: consume a table object (not table var), output it as JSON Properties string */ /* --> example run -- EXEC dbo.JSONreturn @tblObjNameFQ='[database].[schema].[object_name_table]'; */ CREATE PROCEDURE dbo.JSONreturn ( @committedRead bit = 0 --> if 1 then committed else uncommitted read ,@debugmode bit = 0 --> if 1 display certain outputs ,@tblObjNameFQ varchar(128) --> fully qualified table object name, ie db.schema.object_name ,@stringJSON nvarchar(max) = null OUTPUT ) AS BEGIN if @committedRead=0 begin SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; --> evaluate if necessary in test phase end else if @committedRead=1 begin SET TRANSACTION ISOLATION LEVEL READ COMMITTED; end SET NOCOUNT ON; ---------------------------------------------------------------------------------------------------------- if (PATINDEX('%[\.]%',@tblObjNameFQ)<1 AND patindex('%#%',@tblObjNameFQ)<1) OR LEN(@tblObjNameFQ)>(3*128) begin PRINT 'table (object) name not fully qualified or invalid!' RETURN -1 end declare @objname varchar(128) ,@dbname varchar(128) ,@schema varchar(128) ,@maxColNum int ,@inc int ,@dqsl_misc varchar(max) ,@dsql_wrapper varchar(max) ,@dsql_what varchar(max) ,@dsql_where varchar(max) ,@dsql_complete varchar(max) create table #maxColNum (column_id int) create table #ColPrep (colString varchar(max), column_id int) create table #JSONoutput (string nvarchar(max)) if patindex('%#%',@tblObjNameFQ)>0 begin set @objname = (PARSENAME(@tblObjNameFQ,1)) set @dbname = 'tempdb' end else if patindex('%#%',@tblObjNameFQ)<1 begin set @dbname = SUBSTRING(@tblObjNameFQ,1,PATINDEX('%[\.]%',@tblObjNameFQ)-1) set @objname = convert(varchar,(PARSENAME(@tblObjNameFQ,1))) set @schema = convert(varchar,(PARSENAME(@tblObjNameFQ,2))) end --select @objname[@objname], @dbname[@dbname], @schema[@schema] --select @dbname+'.'+@schema+'.'+@objname set @dqsl_misc = ' select max(column_id) from '+@dbname+'.sys.columns where object_id = (select object_id from '+@dbname+'.sys.objects where type = ''U'' and name like ''%'+@objname+'%'') ' insert into #maxColNum exec(@dqsl_misc) set @maxColNum = (select column_id from #maxColNum) set @dsql_what = '' set @dsql_wrapper = ' select ''['' + STUFF(( select '',{''+<<REPLACE>> +''}'' ' set @dsql_where = ' from '+@dbname+'.'+case when @schema is null then '' else @schema end+'.'+@objname+' t1 for xml path(''''), type ).value(''.'', ''varchar(max)''), 1, 1, '''') + '']'' ' set @dqsl_misc = ' select ''"''+sysc.name+''": '' +case when syst.name like ''%time%'' or syst.collationid is not null then ''"''''+cast(''+sysc.name+'' as varchar(max))+''''",'' when syst.name = ''bit'' then ''''''+cast((case when ''+sysc.name+''=1 then ''''true'''' else ''''false'''' end) as varchar(max))+'''','' else ''''''+cast(''+sysc.name+'' as varchar(max))+'''','' end as colString, sysc.column_id from '+@dbname+'.sys.columns sysc join '+@dbname+'.sys.systypes syst on sysc.system_type_id = syst.xtype and syst.xtype <> 240 and syst.name <> ''sysname'' where object_id = (select object_id from '+@dbname+'.sys.objects where type = ''U'' and name like ''%'+@objname+'%'') order by sysc.column_id ' insert into #ColPrep exec(@dqsl_misc) set @inc = (select MIN(column_id) from #ColPrep) while @inc<=@maxColNum begin set @dsql_what = @dsql_what+(select case when @inc = @maxColNum then replace(colString,',','') else colString end from #ColPrep where column_id = @inc) set @inc=@inc+1 IF @inc>@maxColNum set @dsql_what = ''''+@dsql_what+'''' IF @inc>@maxColNum BREAK ELSE CONTINUE end set @dsql_complete = REPLACE(@dsql_wrapper,'<<REPLACE>>',@dsql_what)+@dsql_where insert into #JSONoutput exec(@dsql_complete) SET @stringJSON = (Select string from #JSONoutput) ---------------------------------------------------------------------------------------------------------- END