EXEC ('SELECT * FROM #Final WHERE Aged ' + @AgedStatement + ' ' + @ClearStatement + ' ' + @IsHeldStatement + ' Order by RecordType, SUBSTRING(Account, 12, 8), Account, CFI, Aged, SWDN')>I am writing some stored procedures in SQL2000. I don't like T-SQL much but nothing I can do about it :)
>Key int, >Val1 int, >Val2 int, >val3, int, >-- >-- >val40 int >>The actual table has a ton of columns with weird names. I did not create the table, nor the program, just inherited and must finish it what somebody else has left.
>DECLARE @Val1 INT, @Val2 INT, @Val3 INT, @Val4 INT, @Val5 INT > >SELECT @Val1=Val1, @Val2=Val2, @Val3=Val3, @Val4=Val4, @Val5=Val5 -- and a lot more >FROM table WHERE -- condition >>
@Column2Get CHAR(20)
>SELECT @(whatever_var)=(whatever_column) FROM table WHERE -- condition >>
>IF @Column2Get='Val1' >BEGIN > SELECT @Val1=Val1 FROM table WHERE -- condition >END >ELSE >BEGIN > IF @Column2Get='Val1' > BEGIN > SELECT @Val2=Val2 FROM table WHERE -- condition > END > ELSE > -- another 40 times! >END >>Is there a more elegant solution to build the query on the fly, retrieving the proper column, in a stored procedure?