SQL多個逗號分開的字段值 取對應的數據名稱信息

字段值函數

 

函數實現:fetch

ALTER function [dbo].[GetName](@strs NVARCHAR(500)) RETURNS NVARCHAR(4000) as
begin
 set @strs=','+@strs+','
 DECLARE @str1 NVARCHAR(4000) DECLARE @str2 NVARCHAR(4000) set @str2=''
 declare SyncOrderCursor cursor for 
 select b.Name from dbo.ShopStore as a inner join dbo.Shop as b  on a.StoreGid=b.Gid where @strs like  '%,'+cast(a.Gid as nvarchar(4000))+',%'
 open SyncOrderCursor fetch next from SyncOrderCursor into @str1
 while @@FETCH_STATUS=0
 begin
     set @str2=@str2+'|'+@str1
  fetch next from SyncOrderCursor into @str1
 end
 close SyncOrderCursor deallocate SyncOrderCursor return @str2
End
---另外一種實現方式 --select A.Gid,stuff(( -- select '|'+B.Name from Shop B -- where charindex(','+CAST(B.Gid as varchar(8000))+',',','+ CAST (B.Gid as varchar(8000))+',')>0 -- order by B.Gid -- for xml path('') -- ),1,1,'') as name  --from ShopStore A

調用函數實現:spa

 [dbo].GetName(Gids) AS ShopNamecode

 |保時捷店鋪|奔馳店鋪xml

相關文章
相關標籤/搜索