2011-05-18 73 views
0

我想設置一個列名稱爲一個聲明的變量,但我不斷收到無效的列名稱消息。什麼是正確的語法?這裏是查詢如何將列名稱設置爲聲明的變量?

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 [email protected]_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

你在哪裏設置列名的變量?哪條線投擲錯誤? – 2011-05-18 23:44:40

+0

當診斷問題並在此發佈問題時,嘗試**將您的問題減少到絕對最小值,但仍顯示錯誤。這個巨大的SQL收集令人恐懼,需要很長時間才能閱讀和理解。另外:當你減少你的問題代碼時,你自己經常絆倒原因,你可以自己修復它 – 2011-05-19 05:01:32

回答

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,但在底部您將它們設置爲字符串值。這也會引發一個錯誤 - 如果你試圖說這些是你的列名,那麼這不是做這件事的方法 - 你也不是在任何地方選擇它。請澄清你希望實現什麼...

0

請找到下面的簡化代碼,這似乎符合您的要求。爲了測試它從0 insert語句更改值,以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