Store Procedure
Store Procedure
Store Procedure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName>
-- Add the parameters for the stored procedure here
<@Param1, sysname, @p1> <Datatype_For_Param1, , int> =
<Default_Value_For_Param1, , 0>,
<@Param2, sysname, @p2> <Datatype_For_Param2, , int> =
<Default_Value_For_Param2, , 0>
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
EXEC myStoredProcedure;
Store Procedure : Parameter
END
GO
Execute Store Procedure with Parameter
END
Execute Store Procedure with Parameter
-- Declare Table
DECLARE @temp TABLE
(
RowID INT IDENTITY (1,1),
Name VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50)
)
-- Insert Table
INSERT INTO @temp (Name,LastName,Country) VALUES ('Wichit','Exsys',@pCountry)
INSERT INTO @temp (Name,LastName,Country) VALUES ('Parinya','Exsys',@pCountry)
INSERT INTO @temp (Name,LastName,Country) VALUES ('May','Exsys',@pCountry)
-- Return Rows
SELECT * FROM @temp
END
Declare Table
DECLARE @i INT
SET @i = 1
SET @i = @i + 1
END -- WHILE
END
Declare Cursor
-- Declare cursor
DECLARE cursor_name CURSOR FOR
SELECT ... FROM table_name
-- Open cursor
OPEN cursor_name
FETCH NEXT FROM cursor_name
INTO ...
-- Close cursor
CLOSE cursor_name ;
DEALLOCATE cursor_name ;
Declare Cursor ALTER PROCEDURE [dbo].[mySP_Cursor]
@p_LocCodeVARCHAR(10)
AS
BEGIN
-- Declare Variable
DECLARE @LocName VARCHAR(50)
DECLARE @LocStatus int
-- Open Cursor
OPEN cursor_Loc
FETCH NEXT FROM cursor_Loc
INTO @LocName, @LocStatus;
-- Display
PRINT 'Location Name : ' + @LocName + ', Status : ' + cast(@LocStatus as
varchar(10))
-- Close cursor
CLOSE cursor_Loc;
DEALLOCATE cursor_Loc;
END
IF.. ELSE
IF (Expression) IF (Expression)
BEGIN BEGIN
// Statement // Statement
END END
ELSE IF (Expression)
BEGIN
IF (Expression) // Statement
BEGIN END
// Statement ELSE
END BEGIN
ELSE IF (Expression) // Statement
BEGIN END
// Statement
END
TRY CATCH SYNTAX
BEGIN TRY
// Statement
END TRY
BEGIN CATCH
// Exception
END CATCH
TRY CATCH :Sample 1
CREATE PROCEDURE [dbo].[mySP_TryCath1]
@pOne DECIMAL(10,2),
@pTwo DECIMAL(10,2)
AS
BEGIN
BEGIN TRY
SELECT @pOne/@pTwo
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
END CATCH
END
TRY CATCH :Sample 2
CREATE PROCEDURE [dbo].[mySP_insertLocation]
@p_transactionNo bigint,
@p_LocCodevarchar(10),
@p_LocNameVARCHAR(50),
@p_Statusint,
@pResultint OUTPUT,
@pMessageVARCHAR(500) OUTPUT
AS
BEGIN
BEGIN TRY
-- insert Location
INSERT INTO [Staging_Location] VALUES
(@p_transactionNo,@p_LocCode,@p_LocName,@p_Status);
SET @pResult = @@ROWCOUNT;
SET @pMessage = 'Insert Data Successfully';
END TRY
BEGIN CATCH
SET @pResult = 0;
SELECT @pMessage = ERROR_MESSAGE();
END CATCH
END
TRY CATCH :Sample 2
DECLARE @pResult2 INT
DECLARE @pMessage2 VARCHAR(500)
SELECT@pResult AS 'pResult'
SELECT@pMessage AS 'pMessage'
Function Syntax
RETURN @diffAmt
END
Function Sample
BEGIN TRANSACTION
BEGIN TRY
-- Statement 1
-- Statement 2
COMMIT
END TRY
BEGIN CATCH
ROLLBACK
END CATCH
TRANSACTION ,COMMIT,ROLLBACK Sample
create PROCEDURE [dbo].[mySP_insertLocation2]
@p_transactionNo bigint,
@p_LocCodevarchar(10),
@p_LocNameVARCHAR(50),
@p_Statusint,
@pResultint output,
@pMessageVARCHAR(500) OUTPUT
AS
BEGIN
BEGIN TRANSACTION
BEGIN TRY
-- insert Location
INSERT INTO [Staging_Location] VALUES
(@p_transactionNo,@p_LocCode,@p_LocName,@p_Status);
END
TRANSACTION ,COMMIT,ROLLBACK Sample
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/language-elements-transact-
sql?view=sql-server-2017