Let’s take basic scenario where I want to insert a record during user registration only if no username exists in the database.
My question is will you create 2 separate stored procedures and make 2 calls to the database one for checking whether the username exists or not and 2nd one to actually insert into the database or will you create one stored procedure and write both the queries inside that only?
If you create one stored procedure, then my 2nd question what should you actually return from stored procedure? I normally return hard coded numbers from stored procedures and then check inside code. Is this a good practise?
You could do something like:
Then in the application you could use the @UserExists parameter, 1 would indicate that the user already exists, 0 would indicate that the user did not exist and has been created.
For good practice you should use stored procedures and not inline SQL as you will be vulnerable to SQL injection attacks.