如何使用动态表名更新表?

时间:2015-05-11 09:07:40

标签: sql-server stored-procedures

我想更新动态生成的表列。

我试过这个

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].['+@TableName+'] 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].['+@TableName+'] as A, [dbo].[abc] as B WHERE A.cGender=B.[Key] AND B.[Column]=N'Gender' AND A.AccountId = ('+@Id+')

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

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

但它给出了错误陈述

  

消息156,级别15,状态1,过程spFileProcess,第6行不正确   关键字“AS”附近的语法。消息102,级别15,状态1,过程   spFileProcess,第26行'Gender'附近的语法不正确。 Msg 102,等级   15,状态1,过程spFileProcess,第48行语法不正确   '假'

我按

编辑
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].['+@TableName+'] 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].['+@TableName+'] as A, [dbo].[abc] as B WHERE A.cGender=B.[Key] AND B.[Column]=N'Gender' AND A.AccountId = ('+@Id+')

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

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

但没有一个打印是调用和错误抛出后续

消息207,级别16,状态1,过程spFileProcess,第28行 列名称“性别”无效。 消息207,级别16,状态1,过程spFileProcess,第30行 列名称'HouseholdIncome'无效。 消息207,级别16,状态1,过程spFileProcess,第32行 列名称“PresenceOfChild”无效。

虽然这个库已经在mt表中了

任何解决方案?

4 个答案:

答案 0 :(得分:1)

您需要在First Update语句和第二个Update语句之间留出空格。

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

应该改为

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

如果print之前dynamic Sql execution,则很容易调试。您正在中间打印@squery,即使在您添加更新语句之后,最好还是print并在追加所有update statements之后进行检查。

varchar列值也应该用single quotes括起来。

确保您将传递给variables @squery NULL <{1}}的所有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].['+@TableName+'] 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].['+@TableName+'] as A, [dbo].[abc] as B WHERE A.cGender=B.[Key] AND B.[Column]=N'''Gender''' --Here AND A.AccountId = ('+@Id+') UPDATE A SET A.cHouseholdIncome=B.[Value] FROM [dbo].['+@TableName+'] as A, [dbo].[abc] as B WHERE A.cHouseholdIncomeCode=B.[Key] AND B.[Column]=('''+HouseholdIncome+''') AND A.AccountId = ('+@Id+') UPDATE A SET A.cPresenceOfChild=B.[Value] FROM [dbo].['+@TableName+'] as A, [dbo].[abc] as B WHERE A.cPresenceOfChild=B.[Key] AND B.[Column]=('''+PresenceOfChild+''') --here AND A.AccountId = ('+@Id+')'

$('.toggle').on('toggle', function (e, active) {
    $.each($('.toggle'), function(index, element) {
        if(element.hasClass('off')){
            $('.toggle').removeClass('off');
            $('.toggle').addClass('on');        
        }
        if(element.hasClass('on')){
            $('.toggle').removeClass('on');
            $('.toggle').addClass('off');        
        }
    }); 
});

答案 1 :(得分:0)

我认为您需要在此查询字符串中添加空格

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

@TableName nvarchar(max)

之后删除

答案 2 :(得分:0)

第一个错误:从@TableName nvarchar(max)变量的末尾删除逗号(,)。 第二个错误:如果要传递一些动态值,则需要声明Gender,HouseholdIncome,HouseholdIncome,PresenceOfChild。或者如果这是列名,那么从中移除+(加号)。

例如: AND B. [Column] =(HouseholdIncome)

答案 3 :(得分: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].['+@TableName+'] 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].['+@TableName+'] as A, [dbo].[abc] as B WHERE A.cGender=B.[Key] AND B.[Column]=N''Gender'' AND A.AccountId = ('+@Id+')

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

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