views:

1062

answers:

4

I have a simple query like this:

select * from mytable where id > 8

I want to make the 8 a variable. There's some syntax like

declare @myvar int
myvar = 8

but I don't know the exact syntax.

What is it?

Thanks!

+1  A: 
declare @myvar int

select @myvar = 8
Austin Salonen
+4  A: 

It's:

DECLARE @MyVariable INT
SET @MyVariable = 8
Ed Altorfer
+3  A: 
declare @myvar int

Set @myvar = 8

select * from mytable where id > @myvar
JasonS
A: 

To clarify: both SET and SELECT work, but SET is the ANSI standard. However, if you're setting multiple values at once, then

SET @one = 1
SET @two = 2

will be very slightly slower than

SELECT @one = 1, @two = 2

What you gain in speed may well be offset by readability and clarity, however.

JasonFruit