Using varibales in a SQL statement

Avatar
  • updated
  • Completed
hi,

i have a SQL statement containing three different formulas using UNION. In each WHERE of these formulas I'm searching for the same two strings. so, the strings are summery 8 times in the whole statement...

Can I somehow declare two variables at the top and use it in the WHERE clauses? 

I tried:

Declare @var1 varchar(50)
Declare @var2 varchar(50)
Set @var1 = 'test1'
Set @var2 = 'test2'

(Select.....

...)

But a click on execute dont give me any result and also no error.
Whats wrong??

Thx, Mike

Avatar
Anonymous
Hello,

Would you mind using query parameters as follows?

SELECT * FROM Table1 WHERE Field1 = :Param1 and Field2 = :Param2

You can use same named parameters as many times as you need. On executing such query you will be prompted for parameter values, so there's no need for "Declare" and "Set" statements.
Avatar
Mike Brinkmeier
Nice, didnt know... works fine so far! Thx

Additional:
I like to use the same string to fill a empty cell: SELECT '<the string>' AS Field1'. SELECT :Param1 AS Field1 wont work... Any workarounds?
Avatar
Anonymous
Sorry, but no. Macros are not supported in this tool.