2011-05-18 1 views
0

J'essaye de placer un nom de colonne à une variable déclarée mais je continue à recevoir le message de nom de colonne non valide. Quelle est la bonne syntaxe? Voici la requêteComment définir les noms de colonne pour une variable déclarée?

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 

En fait, j'essaie de vérifier si les valeurs dans ces zones de texte sont nulles ou non. Sinon, je dois régler le compte!

+2

Où êtes-vous définissez un nom de colonne à une variable? Quelle ligne lance l'erreur? –

+0

Lorsque vous diagnostiquez un problème et que vous postez une question ici, essayez de ** réduire ** votre problème au minimum absolu qui montre toujours l'erreur. Cet énorme gob de SQL est effrayant et prend beaucoup de temps à lire et à comprendre. Aussi: lorsque vous réduisez votre code de problème, vous trébuchez souvent sur la cause et vous pouvez le réparer vous-même. –

Répondre

0

2 choses:

(1). Je pense que ceci est erroné - je ne vois pas ces variables déclarées n'importe où ....

 
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). Vous avez défini un ensemble de variables dans le top @rent_amt_h, @house_payment_amt_h, etc. comme NUMERIC, mais en bas, vous les définissez sur une valeur de chaîne. Cela va aussi jeter une erreur - si vous essayez de dire que ce sont les noms de vos colonnes, alors ce n'est pas la façon de le faire - vous ne le sélectionnez pas non plus. Veuillez clarifier ce que vous espérez réaliser ...

0

Veuillez trouver ci-dessous le code simplifié qui a semblé répondre à vos exigences. Pour tester modifier les valeurs de 0 à 1 dans la déclaration insert:

-- 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 
Questions connexes