I have a stored proc I am trying to write , where I want to evaluate multiple conditions on the different parameters in one go. What is the best syntax for this?
Below is what I have:
AS
BEGIN
DECLARE @GARRISON nvarchar(255)
DECLARE @ASSETTYPE nvarchar(255)
DECLARE @FIVALUE int
IF ( ( @GARRISON IS NOT 'Netheravon'
AND @GARRISON IS NOT 'Ludgershall'
)
AND @INTERRUPT >= 5)
BEGIN
END
ELSE
END
You can use.
This can be shortened to
The problem you had was that you have
@variable IS NOT 'constant'– this isnt valid SQL Syntax unless checking for null. For equality use=, for non-equality use!=or<>(Both work IIRC).Edit: This is the exact sctipt I used for testing, and works exactly as expected. SQL Server 2008
The above correctly outputs “1” as both conditions evaluate true. If I either change
@INTERRUPTto 4, or@GARRISONto either Netheravon or Ludgershall then there is no output. This is the exact behaviour I expect.Note: You can’t have an empty
BEGIN...ENDblock in SQL, so if that is empty you’ll get the errorIncorrect syntax near 'END'.