我想更新動態生成的表格列。如何使用動態表名更新表?
我想這
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表
任何解決方案?
你有什麼錯誤,請張貼在你的問題 –
消息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'附近語法不正確。 –
您可以請示出表A和表B的模式。在我看來,任何表中都不存在必需的列。 –