如何通过变量提供列名来更新Temp表

时间:2017-11-16 14:49:31

标签: sql sql-server stored-procedures

我有动态SQL查询生成的临时表,不同的列名取决于数据。当我想通过给定的硬编码列名更新临时表时,它工作正常。但是当我按变量传递列名时它将不起作用

update #Temp 
set @value = ''' + cast(@TempData as nvarchar(max)) +  ''' 
where id = @CUserID

这里@value拥有像[Dusky Legend]这样的列名这个查询不会起作用,但是

update #Temp 
set [Dusky Legend] = ''' + cast(@TempData as nvarchar(max)) +  ''' 
where id = @CUserID

这个工作正常, 我的问题是我只能通过变量提供列名 这是我的完整代码

 Declare @MarketID AS NVARCHAR(MAX) = '1.136903880';
 Declare @UserID AS NVARCHAR(MAX) = '6a309d84-d1c6-434d-b9df-4f96a74da912';
 declare @TempData as numeric = 1111111111;
 declare @@values as NVARCHAR(MAX) ='';



 DECLARE @colsSelect AS NVARCHAR(MAX);
 DECLARE @colsLoop AS NVARCHAR(MAX);

 DECLARE @query AS NVARCHAR(MAX);


 SELECT @colsSelect = STUFF((SELECT distinct ',' +
                    '00' + ' as ' + QUOTENAME(name) 
 from RunnersInfoes   AS t where marketID =@MarketID 

                  FOR XML PATH(''), TYPE
                 ).value('.', 'NVARCHAR(MAX)') 
                    , 1, 1, '');


                    SELECT @colsLoop = STUFF((SELECT distinct ',' +
                    QUOTENAME(name) 
                    from RunnersInfoes   AS t where marketID =@MarketID 

                    FOR XML PATH(''), TYPE
                   ).value('.', 'NVARCHAR(MAX)') 
                    , 1, 1, '');




                    --print @colsLoop


 set @query=
 '
 ;with cte 
 as  
 (
 select 
 id, ParentId,0 AS Level,Share ,AccountTypeName,FirstName
 from  dbo.View_UserProfile 
 where View_UserProfile.id = ' + '''' + @UserID + '''' +'
 union all 
select 
t.id, t.ParentId,Level + 1 AS Level,t.Share,t.AccountTypeName ,t.FirstName
 from View_UserProfile t  
 inner join cte on t.ParentId = cte.id)

SELECT ID,AccountTypeName as Type,FirstName as Name, ' + @colsSelect + '  into                   #Temp  from cte as t



--exec tempdb..sp_help #Temp


 Declare @CUserID AS NVARCHAR(MAX)

 DECLARE dynamic_cursor CURSOR FOR  
 select ID from #Temp
 OPEN dynamic_cursor  

 FETCH NEXT FROM dynamic_cursor   INTO @CUserID 

 WHILE @@FETCH_STATUS = 0  
BEGIN  




 declare @pos as numeric = 0
 declare  @len as numeric = 0
 declare @value as varchar(255)

 WHILE CHARINDEX('','',  ''' + @colsLoop +''', @pos+1)>0
    BEGIN
   set @len = CHARINDEX('','', ''' + @colsLoop +''', @pos+1) - @pos
   set @value = SUBSTRING(''' + @colsLoop +''', @pos, @len)
   PRINT  @value

   update #Temp set [Dusky Legend] = ''' + cast(@TempData as nvarchar(max)) +   ''' where id = @CUserID





set @pos = CHARINDEX('','', ''' + @colsLoop +''', @pos+@len) +1
         END    




          --print' + cast(@TempData as nvarchar(max)) +'

          --update #Temp set [Dusky Legend] =''' + cast(@TempData as  nvarchar(max)) +  ''' where id = @CUserID



           FETCH NEXT FROM dynamic_cursor INTO @CUserID  

            END

           CLOSE dynamic_cursor  
          DEALLOCATE dynamic_cursor 

           select * from #Temp

             '
           execute  (@query)

1 个答案:

答案 0 :(得分:2)

您需要动态SQL。

declare @value varchar(64) = '[Dusky Legend]'
declare @TempData varchar(max) = 'some value'
declare @CUSerID int = 14


declare @sql varchar(max)
set @sql = '
update #Temp 
set ' + @value + ' = ' + cast(@TempData as nvarchar(max)) +  '
where id = ' + cast(@CUserID as varchar(256))

 print(@sql)
 --exec(@sql)

旁注,您可能需要一个全局TempTable,具体取决于您的操作方式。