2015-05-11 45 views
1

我想更新動態生成的表格列。如何使用動態表名更新表?

我想這

ALTER PROCEDURE [dbo].[spname] 
    @Id INT , 
    @Flag varchar(MAX), 
    @TableName nvarchar(max) , 
AS 
BEGIN  
    IF(@Flag='Process') 
    BEGIN 
    DECLARE @squery nvarchar(max) = '' 

    IF EXISTS(SELECT NAME FROM [DZ_REPO].SYS.TABLES WHERE NAME = @TABLENAME) 
     BEGIN 
      set @squery = 'use [DZ_REPO]' 
      set @squery += 'UPDATE A SET A.cFirstName=B.FN, A.cLastName=B.LN, A.cAddress=B.ADDR, A.cCity=B.CITY, A.cState=B.ST, A.cZIP=B.ZIP, 
      A.cGender=B.GENDER, A.cAge=B.EXACT_AGE, A.cHouseholdIncomeCode=B.HH_INCOME FROM [dbo].['[email protected]+'] as A, [Consumer].[dbo].[CONSUMERDB] as B 
      WHERE A.Email=B.Email AND A.AccountId =('+ @Id+')'   
      print (@squery) 

      set @squery += 'UPDATE A SET A.cGender=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B WHERE A.cGender=B.[Key] AND B.[Column]=N'Gender' AND A.AccountId = ('[email protected]+') 

      UPDATE A SET A.cHouseholdIncome=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B WHERE A.cHouseholdIncomeCode=B.[Key] AND B.[Column]=('+HouseholdIncome+') AND A.AccountId = ('[email protected]+') 

      UPDATE A SET A.cPresenceOfChild=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B WHERE A.cPresenceOfChild=B.[Key] AND B.[Column]=('+PresenceOfChild+') AND A.AccountId = ('[email protected]+')' 
     END 
    END 
END 

但它給錯誤,指出

Msg 156, Level 15, State 1, Procedure spFileProcess, Line 6 Incorrect syntax near the keyword 'AS'. Msg 102, Level 15, State 1, Procedure spFileProcess, Line 26 Incorrect syntax near 'Gender'. Msg 102, Level 15, State 1, Procedure spFileProcess, Line 48 Incorrect syntax near 'False'

我編輯通過

ALTER PROCEDURE [dbo].[spname] 
    @Id INT = 1 , 
    @Flag varchar(MAX) = 'Process', 
    @TableName nvarchar(max) = 'SONY' 
AS 
BEGIN  
    IF(@Flag='Process') 
    BEGIN 
    DECLARE @squery nvarchar(max) = '' 
    print ('call') 
    IF EXISTS(SELECT NAME FROM [DZ_REPO].SYS.TABLES WHERE NAME = @TABLENAME) 
     BEGIN 
      set @squery = 'use [DZ_REPO]' 
      print (@squery) 
      set @squery += 'UPDATE A SET A.cFirstName=B.FN, A.cLastName=B.LN, A.cAddress=B.ADDR, A.cCity=B.CITY, A.cState=B.ST, A.cZIP=B.ZIP, 
      A.cGender=B.GENDER, A.cAge=B.EXACT_AGE, A.cHouseholdIncomeCode=B.HH_INCOME FROM [dbo].['[email protected]+'] as A, [Consumer].[dbo].[CONSUMERDB] as B 
      WHERE A.Email=B.Email AND A.AccountId =('+ @Id+')'   
      print (@squery) 

      set @squery += 'UPDATE A SET A.cGender=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B WHERE A.cGender=B.[Key] AND B.[Column]=N'Gender' AND A.AccountId = ('[email protected]+') 

      UPDATE A SET A.cHouseholdIncome=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B WHERE A.cHouseholdIncomeCode=B.[Key] AND B.[Column]=('+HouseholdIncome+') AND A.AccountId = ('[email protected]+') 

      UPDATE A SET A.cPresenceOfChild=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B WHERE A.cPresenceOfChild=B.[Key] AND B.[Column]=('+PresenceOfChild+') AND A.AccountId = ('[email protected]+')' 
     END 
    END 
END 

,但沒有一個打印是通話和錯誤拋folling

消息207,級別16,狀態1,過程spFileProcess,第28行 列名稱'Gender'無效。 消息207,級別16,狀態1,過程spFileProcess,行30 無效的列名稱'HouseholdIncome'。 消息207,級別16,狀態1,過程spFileProcess,行32 無效的列名稱'PresenceOfChild'。

雖然這coumn已經在MT表

任何解決方案?

+3

你有什麼錯誤,請張貼在你的問題 –

+0

消息156,15級,狀態1,過程spFileProcess,行6 關鍵字'AS'附近的語法不正確。 Msg 102,Level 15,State 1,Procedure spFileProcess,Line 26 'Gender'附近的語法錯誤。 Msg 102,Level 15,State 1,Procedure spFileProcess,Line 48 'False'附近語法不正確。 –

+0

您可以請示出表A和表B的模式。在我看來,任何表中都不存在必需的列。 –

回答

1

您需要在第一個更新語句和第二個更新語句之間有空格。

...A.AccountId =('+ @Id+')' and set @squery += 'UPDATE A.... 

應改爲

...A.AccountId =('+ @Id+')' and set @squery += ' UPDATE A... 

這將是容易調試,如果你printdynamic Sql execution之前。您正在中間打印@squery,即使在您添加更新語句之後,所以更好的爲print,並在追加所有update statements後進行檢查。

varchar列值也應該包含single quotes

確保你通過所有variables否則你@squeryNULL

set @squery = 'use [DZ_REPO]' 

set @squery += 'UPDATE A SET A.cFirstName=B.FN, A.cLastName=B.LN, A.cAddress=B.ADDR, A.cCity=B.CITY, A.cState=B.ST, A.cZIP=B.ZIP, 
      A.cGender=B.GENDER, A.cAge=B.EXACT_AGE, A.cHouseholdIncomeCode=B.HH_INCOME FROM [dbo].['[email protected]+'] as A, [Consumer].[dbo].[CONSUMERDB] as B 
      WHERE A.Email=B.Email AND A.AccountId =('+ @Id+')'   
      print (@squery) 

set @squery += 'UPDATE A SET A.cGender=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B 
       WHERE A.cGender=B.[Key] 
       AND B.[Column]=N'''Gender''' --Here 
       AND A.AccountId = ('[email protected]+') 

UPDATE A SET A.cHouseholdIncome=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B 
WHERE A.cHouseholdIncomeCode=B.[Key] 
AND B.[Column]=('''+HouseholdIncome+''') 
AND A.AccountId = ('[email protected]+') 

UPDATE A SET A.cPresenceOfChild=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B 
WHERE A.cPresenceOfChild=B.[Key] 
AND B.[Column]=('''+PresenceOfChild+''') --here 
AND A.AccountId = ('[email protected]+')' 
+0

當我執行編輯後建議你執行我這樣的錯誤,「Msg 156,級別15,狀態1,過程spFileProcess,行6 關鍵字'AS'附近的語法不正確 Msg 102,Level 15,State 1,過程spFileProcess,Line 26 'Gender'附近的語法錯誤 Msg 102,Level 15,State 1,Procedure spFileProcess,Line 48 'False'附近的語法錯誤。 –

+0

把動態執行之前的打印聲明,也沒有打印 –

+0

@KinjalPatel請確保您**的**值**所有'變量'否則你'@ squery'將'NULL' –

0

我覺得你需要增加空間在你的查詢字符串這樣

set @squery = 'use [DZ_REPO] ' 
set @squery += 'UPDATE A SET A.cFirstName......' 

和刪除, @TableName nvarchar(max)

0

First E rror:從@TableName nvarchar(max)變量的末尾刪除逗號(,)。 第二個錯誤:如果您傳遞一些動態值,則需要聲明Gender,HouseholdIncome,HouseholdIncome,PresenceOfChild。或者如果這是列名,那麼從中刪除+(加號)。

ex: AND B.[專欄] =(HouseholdIncome)

+0

我會嘗試像這樣,並得到錯誤,如「**轉換nvarchar值時轉換失敗」使用[DZ_REPO]更新A SET A.cFirstName = B.FN,A.cLastName = B.LN,A.cAddress = B.ADDR,A.cCity = B.CITY,A.cState = B.ST,A.cZIP = B.ZIP, \t \t \t A.cGender = B.GENDER,A.cAge = B.EXACT_AGE,A. cHouseholdIncomeCode = B.HH_INCOME FROM [dbo]。[SONY] as A,[Consumer]。[dbo]。[CONSUMERDB] as B \t \t \t WHERE A.Email = B.Email AND A.Pic ked IS NULL和A.DataProfilingAccountId =('+ @ Id +')'爲數據類型int。**「 –

+0

使用此A.DataProfilingAccountId =(@Id) –

0

檢查此SQL與評論(這是你的第一個SQL後):

ALTER PROCEDURE [dbo].[spname] 
    @Id INT , 
    @Flag varchar(MAX), 
    @TableName nvarchar(max), --remove comma from here 
AS 
BEGIN  
    IF(@Flag='Process') 
    BEGIN 
    DECLARE @squery nvarchar(max) = '' 

    IF EXISTS(SELECT NAME FROM master.SYS.TABLES WHERE NAME = @TABLENAME) 
     BEGIN 
      set @squery = 'use [DZ_REPO]' 
      set @squery += 'UPDATE A SET A.cFirstName=B.FN, A.cLastName=B.LN, A.cAddress=B.ADDR, A.cCity=B.CITY, A.cState=B.ST, A.cZIP=B.ZIP, 
      A.cGender=B.GENDER, A.cAge=B.EXACT_AGE, A.cHouseholdIncomeCode=B.HH_INCOME FROM [dbo].['[email protected]+'] as A, [Consumer].[dbo].[CONSUMERDB] as B 
      WHERE A.Email=B.Email AND A.AccountId =('+ @Id+')'   
      print (@squery) 

      --if 'Gender' id your fixed column then give as below with double comma 
      --HouseholdIncome - where you define this variable and assign it 
      --PresenceOfChild - where you define this variable and assign it 
      set @squery += 'UPDATE A SET A.cGender=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B WHERE A.cGender=B.[Key] AND B.[Column]=N''Gender'' AND A.AccountId = ('[email protected]+') 

      UPDATE A SET A.cHouseholdIncome=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B WHERE A.cHouseholdIncomeCode=B.[Key] AND B.[Column]=('+HouseholdIncome+') AND A.AccountId = ('[email protected]+') 

      UPDATE A SET A.cPresenceOfChild=B.[Value] FROM [dbo].['[email protected]+'] as A, [dbo].[abc] as B WHERE A.cPresenceOfChild=B.[Key] AND B.[Column]=('+PresenceOfChild+') AND A.AccountId = ('[email protected]+')' 
     END 
    END 
END