Application of SQL Server stored procedures

Source: Internet
Author: User
Tags throw exception try catch

1. Create a stored procedure template with Try Catch

Copy the following code, and then create a new query, you can write the SQL statement, after execution, a your own stored procedures are established!

SETAnsi_nulls onGOSETQuoted_identifier onGO-- =============================================--AUTHOR:--DESCRIBE:-- =============================================CREATE PROCEDURE [dbo].[Up_insertjhbdata] --Stored Procedure name@CustomerName VARCHAR( -)--Parameters as     BEGIN            SETNOCOUNT on                     --high-performance, you must have        DECLARE @Now DATETIME        SET @Now = GETDATE()--All operations Guarantee uniform time                BEGINTRY--write the SQL here        ENDTRYBEGINCATCHDECLARE @ErrorMessage NVARCHAR(4000) ; DECLARE @ErrorSeverity INT ; DECLARE @ErrorState INT ; SELECT  @ErrorMessage =error_message (),@ErrorSeverity =error_severity (),@ErrorState =error_state (); PRINT @ErrorMessage             RAISERROR(@ErrorMessage,--Message text.                        @ErrorSeverity,--Severity.                        @ErrorState     --State .                        ) ; RETURN -1 ; ENDCATCHEND

2. Create a stored procedure template with transactions

The control of the transaction is added to the template of the stored procedure with Try Catch

SETAnsi_nulls onGOSETQuoted_identifier onGO-- =============================================--AUTHOR:--DESCRIBE:-- =============================================CREATE PROCEDURE [dbo].[Up_insertjhbdata] --Stored Procedure name@CustomerName VARCHAR( -) --Parameters as     BEGIN           SETNOCOUNT on;--high-performance, you must have        DECLARE @Now DATETIME ; SET @Now = GETDATE() ;--All operations Guarantee uniform time               BEGINTRYBEGIN TRANSACTIONMytrans;--Start a transaction           --write the SQL here           COMMIT TRANSACTIONMytrans;--Transaction COMMIT Statement        ENDTRYBEGINCATCHROLLBACK TRANSACTIONMytrans--always roll back a transaction            --Throw Exception            DECLARE @ErrorMessage NVARCHAR(4000) ; DECLARE @ErrorSeverity INT ; DECLARE @ErrorState INT ; SELECT  @ErrorMessage =error_message (),@ErrorSeverity =error_severity (),@ErrorState =error_state (); RAISERROR(@ErrorMessage,--Message text.                 @ErrorSeverity,--Severity.                 @ErrorState     --State .                 ) ; ENDCATCHEND

3. Cycle templates

  In a stored procedure, some temporary tables are often generated, and then the data for the staging table is processed, and the following templates help the partners to quickly handle such requirements

--Generate temporary table data with line numbers and insert in temporary table #t_table    SELECTRow_number () Over(ORDER  byID) asRownum,name into#T_Table fromTableName--get Total Records    DECLARE @RecordCount INT = 0        SELECT @RecordCount = COUNT(1) from#T_TableDECLARE @CurrRowNum INT = 1        --Current line number    DECLARE @CurrName VARCHAR( -)--Current Field        --Circular Recording     while @CurrRowNum <= @RecordCount         BEGIN            --get current record            SELECT @CurrName =Name from#T_TableWHERERowNum= @CurrRowNum                        --Custom SQL                        SET @CurrRowNum = @CurrRowNum + 1 --to the next record        END

Application of SQL Server stored procedures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.