I want to know how to put validation into Store Procedure in SQL server.
Thank you.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Akkiraju IvaturiPosted Aug 31, 2012, 7:10 PM
Here are the examples:
1. Always the balance should be greater than 1000
Declare @balance decimal(15,3)
SELECT @balance = balance from Account where customerid=5
IF (@balance < 1000)
BEGIN
RAISEERROR("Balance is less than 1000")
RETURN
END
2. Check if selected date should be greater than equal to today's date
SET @SelectedDate = '12/12/2010'
IF (@SelectedDate < GetDate())
BEGIN
RAISEERROR("Date error")
Return
END
RaiseError is the method to raise an error and abrupt from executing the next statements in sql stored procedure and it is propagated to the client. So, we need to catch the errors either raised by our own logic or by the sql server and display the friendly message to the user and log the error to find the issue.