Created
August 15, 2016 22:33
-
-
Save simoncowie/1e72f47ae3953092ab9c696dc02b0ac4 to your computer and use it in GitHub Desktop.
Create stored proc - ms sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE DB | |
GO | |
CREATE PROCEDURE dbo.spMySpName | |
@param1 int, | |
@param2 int, | |
@paramWithDefault nvarchar(20) = null | |
AS | |
BEGIN | |
DECLARE @someVar nvarchar(100); | |
SELECT @someVar = name | |
FROM table | |
WHERE id = @param2; | |
--basic if | |
IF (@someVar is null) | |
BEGIN | |
--basic print error and exit | |
PRINT N'Error: someVar not found!'; | |
RETURN(1); | |
END | |
DECLARE @anotherthing nvarchar(128); | |
SET @anotherthing = @paramWithDefault + ' ' + @someVar; | |
-- if exists example | |
IF EXISTS (Select * from [users] where UserId = @param2 and someThing = @anotherthing) | |
BEGIN | |
PRINT N'Error: This user already exists!'; | |
RETURN(1); | |
END | |
-- an insert | |
INSERT INTO [users] (UserId, Something) | |
VALUES (@param2,@anotherthing); | |
--SUCCESS | |
DECLARE @PrintMessage nvarchar(100); | |
SET @PrintMessage = N'Success: ' + @ProviderKey | |
+ N' added to the UserLogins table'; | |
PRINT @PrintMessage; | |
RETURN(0); | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment