日期:2014-05-17 浏览次数:20495 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-04-26 16:40:36
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([mobile] int,[content] varchar(4))
insert [huang]
select 123,'qwer' union all
select 234,'qwer' union all
select 345,'qwer' union all
select 456,'asdf' union all
select 567,'asdf' union all
select 678,'sdfg' union all
select 789,'wert'
--------------开始查询--------------------------
--手动创建函数
ALTER FUNCTION [dbo].[F_Str] ( @content VARCHAR(64) )
RETURNS NVARCHAR(1000)
AS
BEGIN
DECLARE @S NVARCHAR(100)
SELECT @S = ISNULL(@S + ',', '') + CONVERT(VARCHAR(64), mobile)
FROM [huang]
WHERE [content] = @content
RETURN @S
END
--查询
SELECT DISTINCT content,mobile=dbo.f_str(content) FROM [huang]
----------------结果----------------------------
/*
content mobile
------- ----------------------------------------------------------------------------------------------------------------
asdf 456,567
qwer 123,234,345
sdfg 678
wert 789
*/