2017-11-28 2 views
1

Ich habe eine Select-Anweisung und ich habe diese Select-Anweisung einer Variablen zugewiesen @ var1 jetzt muss ich überprüfen, ob eine Spalte einen Wert hat oder nicht @ var1 ist es möglich?Wie überprüft man, ob eine select-Variable einen Spaltenwert hat

SET @STRVAR1='SELECT type, a.groupnumber, idnumber, relation,a.code, flag, planname, a.insurancename, address1, address2, REPLACE(zipcode,-,'') AS zipcode, cityName, statecode,c.PrintBillProTaxonomy FROM TABLE1, TABLE2, TABLE3 WHERE b.citycode=c.citycode and a.insurancecode = c.insurancecode AND Code = ''PT0000'' AND a.No = ''GT56789'' AND a.Flag = ''SAMPLE''' 
    EXEC (@STRVAR1) 

MEIN ZUSTAND SOLL:

Beispiel unter Verwendung einer Tabelle:

IF @STRVAR.COLUMNNAME<>'' 
BEGIN 
PRINT'TEXT' 
END 
+0

nicht möglich, ohne es auszuführen – Squirrel

+0

WENN ICH AUSFÜHREN, WIRD ES MÖGLICH sein? –

+0

ja. Aber Ihre Abfrage wird nicht ausgeführt. Es gibt einen Syntaxfehler – Squirrel

Antwort

1

Sie die Ergebnisse Ihrer Abfrage in einer temporären Tabelle oder eine Tabelle Variable und dann abfragen, die Tabelle speichern variable:

declare @t1 table (type varchar(10), groupnumber int, idnumber int, etc....) 
-- you need to declare all your resultset columns here and their respective types 

declare @strvar1 nvarchar(max) 
SET @strvar1='SELECT type, a.groupnumber, idnumber, relation,a.code, flag, planname, a.insurancename, address1, address2, REPLACE(zipcode,-,'') AS zipcode, cityName, statecode,c.PrintBillProTaxonomy 
       FROM TABLE1, TABLE2, TABLE3 
       WHERE b.citycode=c.citycode and a.insurancecode = c.insurancecode AND Code = ''PT0000'' AND a.No = ''GT56789'' AND a.Flag = ''SAMPLE''' 

insert into @t1 
exec(@strvar1) 

select * 
from @t1 
where columnname <> '' 

Es wäre gut, die Verknüpfungslogik Ihrer Abfrage inzu ändern.

Verwandte Themen