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!
It's:
DECLARE @MyVariable INT
SET @MyVariable = 8
declare @myvar int
Set @myvar = 8
select * from mytable where id > @myvar
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With