如何将列名设置为声明的变量?

时间:2011-05-18 22:33:04

标签: sql sql-server tsql

我正在尝试将列名设置为声明的变量,但我一直收到无效的列名消息。什么是正确的语法?这是查询

Declare @APPSHELTER Table 
(tid int Identity(1,1),App_Id Numeric,PrimaryName Varchar(300),--EmployerAdress Varchar(500), rent_amt  varchar(20),house_payment_amt varchar(20),ins_amt varchar(20),utilities_amt  varchar(20),Trash_Collection_amt   varchar(20),Sewerage_amt varchar(20),Telephone_amt  varchar(20),water_amt    varchar(20),other_amt  varchar(20), total varchar(20), property_taxes_amt varchar(20),insurance_amt varchar(20), other_house_amt varchar(20), gas_amt varchar(20), elec_amt varchar(20), otherfuel_amt varchar(20))

  DECLARE @rent_amt_h NUMERIC
         DECLARE @house_payment_amt_h NUMERIC
         DECLARE @insurance_amt_h NUMERIC
         DECLARE @property_taxes_amt_h NUMERIC
         DECLARE @Other_house_amt_h NUMERIC
         DECLARE @gas_amt_u NUMERIC
         DECLARE @elec_amt_u NUMERIC
         DECLARE @otherfuel_amt_u NUMERIC
         DECLARE @Trash_Collection_amt_u NUMERIC
         DECLARE @Sewerage_amt_u NUMERIC
         DECLARE @Telephone_amt_u NUMERIC
         DECLARE @water_amt_u NUMERIC
         DECLARE @other_amt_u NUMERIC
         DECLARE @total_u NUMERIC

insert into @APPSHELTER(App_Id,PrimaryName,rent_amt,house_payment_amt,ins_amt,utilities_amt,Trash_Collection_amt,Sewerage_amt,Telephone_amt,water_amt,other_amt,total, property_taxes_amt, insurance_amt, other_house_amt, gas_amt, elec_amt, otherfuel_amt  )
         select @app_id,
                ISNULL((select top 1 replace(first_name,'''','''''') + ' ' + isnull(replace(middle_name,'''',''''''),'') + ' '+replace(last_name,'''','''''')
                from app_member (nolock) 
                where app_id = @app_id and msn=1),'') AS PrimaryName,
                isnull(rent_amt,'0' ) AS rent_amt,
                isnull(house_payment_amt,'0') AS house_payment_amt,
                isnull((insurance_amt+property_taxes_amt),'0') AS ins_amt,
                --isnull(HC_Costs_amt,'0') AS utilities_amt,
                isnull(gas_amt,'0') + isnull(elec_amt,'0') + isnull(otherfuel_amt,'0') as utilities_amt,
                isnull(Trash_Collection_amt,'0') AS Trash_Collection_amt,
                isnull(Sewerage_amt,'0') AS Sewerage_amt,
                isnull(Telephone_amt,'0') AS Telephone_amt,
                isnull(water_amt,'0') AS water_amt,
                isnull(other_amt,'0') + isnull(other_house_amt,'0') AS other_amt,
                isnull(total,'0') AS total,
                                 isnull(property_taxes_amt,'0' ) AS property_taxes_amt,
                  isnull(insurance_amt,'0' ) AS insurance_amt,
                   isnull(other_house_amt,'0' ) AS other_house_amt,
                    isnull(gas_amt,'0' ) AS gas_amt,
                     isnull(elec_amt,'0' ) AS elec_amt,
                      isnull(otherfuel_amt,'0' ) AS otherfuel_amt
                 from Ext_App_Group_Other_Expenses  APP_DEP (nolock)
         WHERE APP_DEP.APP_ID=@APP_ID 

 SET @rent_amt_h = 'rent_amt'
          SET @house_payment_amt_h = 'house_payment_amt'
         SET @insurance_amt_h = 'insurance_amt'
         SET @property_taxes_amt_h = 'property_taxes_amt'
         SET @Other_house_amt_h = 'other_house_amt'
         SET @gas_amt_u = 'gas_amt'
         SET @elec_amt_u = 'elec_amt'
         SET @otherfuel_amt_u = 'otherfuel_amt'
         SET @Trash_Collection_amt_u = 'Trash_Collection_amt'
         SET @Sewerage_amt_u = 'Sewerage_amt'  
         SET @Telephone_amt_u = 'Telephone_amt'
         SET @water_amt_u = 'water_amt'
         SET @other_amt_u = 'other_amt'
         SET @total_u = 'total'

DECLARE @APPSHELTER_COUNT INT


 if (rent_amt!=0 or house_payment_amt!=0 or insurance_amt != 0 or property_taxes_amt != 0 or  gas_amt != 0 or elec_amt != 0 or otherfuel_amt != 0 or Trash_Collection_amt != 0 or Sewerage_amt != 0 or Telephone_amt != 0 or water_amt != 0 or other_house_amt != 0 or other_amt != 0 or total !=0)
                begin
           SET @APPSHELTER_COUNT  =  (select Count(APP_ID) FROM ext_app_group_other_expenses (nolock)  WHERE APP_ID = @App_Id )
         end
         else
         begin
           SET @APPSHELTER_COUNT  =  0
         end

实际上,我正在检查这些文本框中的值是否为空。如果不是,我必须设置计数!

2 个答案:

答案 0 :(得分:0)

2件事:

(1)。我认为这是错误的 - 我没有看到这些变量在任何地方宣布....

if (rent_amt!=0 or house_payment_amt!=0 or insurance_amt != 0 or property_taxes_amt != 0 or  gas_amt != 0 or elec_amt != 0 or otherfuel_amt != 0 or Trash_Collection_amt != 0 or Sewerage_amt != 0 or Telephone_amt != 0 or water_amt != 0 or other_house_amt != 0 or other_amt != 0 or total !=0) 

(2)。您在顶部@rent_amt_h,@ house_payment_amt_h等处将一组变量定义为NUMERIC,但是在底部您将它们设置为字符串值。这也会引发错误 - 如果你试图说这些是你的列名,那么这不是你的方法 - 你也不是在任何地方选择它。请澄清你希望实现的目标......

答案 1 :(得分:0)

请在下面找到符合您要求的简化代码。要测试它,请在insert语句中将值从0更改为1:

-- Dynamic SQL command string variable
DECLARE @cmdSQL VARCHAR(1000)

-- Column names
DECLARE @rent_amt_h VARCHAR(20) = 'App_Id'
DECLARE @house_payment_amt_h VARCHAR(20) = 'PrimaryName'

-- Count variable
DECLARE @APPSHELTER_COUNT INT

-- Make SQL command
SET @cmdSQL = '
    Declare @APPSHELTER Table 
        (App_Id Numeric,PrimaryName Varchar(300))

    insert into @APPSHELTER(App_Id,PrimaryName) values (0,0)

SELECT * FROM @APPSHELTER WHERE ' + @rent_amt_h + ' !=0 OR ' + @house_payment_amt_h + ' != 0'

-- Execute dynamic SQL
EXEC(@cmdsql)
IF @@ROWCOUNT != 0
    SET @APPSHELTER_COUNT = 1
ELSE
    SET @APPSHELTER_COUNT = 0

SELECT @APPSHELTER_COUNT