在实际开发过程中,前端经常会用到JSON数据进行控件绑定,为了简化前端开发,MS SQL直接将数据结果集合转换为JSON字符串。
比如:前端HTML Select元素通过Jquery绑定JSON地区的数据
MSSQL存储过程模拟代码:
//数据库
declare @AreaTable table
(
Item varchar(10)
)
//模拟表数据
insert into @AreaTable values
('北京'),
('上海'),
('广东')
//处理过程
declare @Return varchar(max)
select @Return = isnull(@Return + ',','') +'{item='''+ isnull(Item,'')+'''}' from @AreaTable
select '['+@Return+']' as AreaItem
//返回JSON: [{item='北京'},{item='上海'},{item='广东'}]
