SQL语句难题!高手进来指点下
ID name
1 北
1 京
1 时
1 间
2 当
2 地
2 时
2 间
现在把传ID进来查询,如果是1,则返回字符串:北京时间,如果是2:返回当地时间
------解决方案--------------------好像貌似游标可以解决
------解决方案--------------------这是我收集的SQL,或许对你有帮助
create function f_hb(@ID int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ' '
select @str = @str + ', ' + cast(name as varchar) from yourtablename where ID = @ID
set @str = right(@str , len(@str) - 1)
return(@str)
End
go
--调用自定义函数得到结果:
select distinct ID ,dbo.f_hb(ID) as name from yourtablename
------解决方案--------------------CREATE function f_str_connect(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ' '
select @str = @str + cast(name as varchar) from tb where id = @id
return(@str)
End