行转列存储过程

行转列存储过程

 

 1  GO  3 
 4 /****** Object: StoredProcedure [dbo].[GetChildTableValue] Script Date: 2013/12/6 1:03:44 ******/
 5 SET ANSI_NULLS ON  6 GO  7 
 8 SET QUOTED_IDENTIFIER ON  9 GO 10 
11 CREATE procedure [dbo].[GetChildTableValueBadnewfish] 12 ( 13     
14     @TableName nvarchar(100), 15     @WantValueFeild nvarchar(100), 16     @SearchFeildName nvarchar(100), 17     @SearchFeildValue nvarchar(100) 18 ) 19 as
20 BEGIN 21 
22  
23  --select @s = isnull(@s+',','')+b from tb where a=@id 24  declare @str nvarchar(500) 25  declare @rValue nvarchar(500) 26  --declare @s varchar(500); 27  set @str=N' select @s = isnull(convert(nvarchar(50),@s)+'','','''')+convert(nvarchar(50),'+@WantValueFeild+') from '+@TableName+' where '+@SearchFeildName+'= '+@SearchFeildValue 28  --print @str 29  exec sp_executesql @str,N'@s nvarchar(500) OUTPUT,@sIn nvarchar(50)',@sIn='@s',@s=@rValue OUTPUT 30  --print @rValue 31  select @rValue 32 
33  
34 END 35 
36 
37 GO

 

 

你可能感兴趣的:(存储过程)