跪多行转字符串的funtion
跪求一个多行转字符串的funtion
现在遇到问题是, 怎么将查询的结果当做参数传入function呢?
------解决方案--------------------
------解决方案--------------------
非要用函数?
现在遇到问题是, 怎么将查询的结果当做参数传入function呢?
--查询结果如下
select id from table :
ID
--------------------------------------------------
01
0101
0102
-- 需求效果如下(结果如下SQL)
select '01,0101,0102' :
------------
01,0101,0102
------解决方案--------------------
SELECT STUFF((select ','+id from TABLE FOR XML PATH('')),1,1,'')
------解决方案--------------------
非要用函数?
----------------------------------------------------------------
-- Author :DBA_HuangZJ(發糞塗牆)
-- Date :2014-08-26 10:29:11
-- Version:
-- Microsoft SQL Server 2012 - 11.0.5058.0 (X64)
-- May 14 2014 18:34:29
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[Tb]
if object_id('[Tb]') is not null drop table [Tb]
go
create table [Tb]([ID] varchar(4))
insert [Tb]
select '01' union all
select '0101' union all
select '0102'
--------------开始查询--------------------------
select DISTINCT
stuff((select ','+id from (SELECT id FROM TB) b
for xml path('')),1,1,'') 'id'
from (SELECT id FROM TB) a
----------------结果----------------------------
/*
id
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01,0101,0102
*/