Reputation: 31
I can't seem to work this out. I want to kill my script if a variable is true. but I can't find any answers so far.
What I'm trying:
raiserror('Error', 18, -1)
Where @Variable='True';
I have declared and assigned the variable already
Upvotes: 2
Views: 541
Reputation: 7260
I want to counter-propose the advice of paneerakbari here.
Sure, Microsoft documentation recommends using throw
instead of raiserror.
However, Erland Sommarskog, a Microsoft MVP with a blog that consists of very enlightened posts, discusses in Part 2 of his Error and Transaction Handling article that THROW
does less stuff that raiserror, it requires a semicolon which might be perilously forgotten. I thus strongly recommend sticking to the less dangerous raiserror
.
Upvotes: 0
Reputation: 725
The other answers here are correct about the structure of the conditional check, but I will recommend that you use THROW
rather than RAISERROR
, based on documentation. Microsoft's documentation https://learn.microsoft.com/en-us/sql/t-sql/language-elements/raiserror-transact-sql?view=sql-server-ver15
IF @Variable = 'True'
BEGIN
THROW 50000, 'Error', 1;
END
And, as an aside, you should strongly consider not using text literals like 'true' or 'false' in your code and instead opt for the BIT
datatype to achieve the same, where possible.
Upvotes: 0
Reputation: 1157
You can not use it like sql query. It is command so you can write as
IF @Variable = 'True'
BEGIN
raiserror('Error', 18, -1);
END
Upvotes: 1
Reputation: 312239
raiseerror
is a T-SQL command. It doesn't have a where
clause, but you could put it inside an if
block:
IF @Variable = 'True'
raiserror('Error', 18, -1)
Upvotes: 4