SQL Server Stored Procedures

Source: Internet
Author: User

Long time no use of stored procedures, suddenly a use to find a lot of things to forget, wrote two. Easy access. Examples are very simple, convenient to view the structure, the use of parameters.

SELECT  * fromproduction.product;--1 Creating a stored procedure that returns a result setDROP PROCEDUREdbo. pr_product;CREATE PROCEDUREpr_product as     BEGIN         SELECT  *         fromproduction.product; END; --Executing stored procedures    EXECdbo. pr_product;--2 Creating a stored procedure that requires the input of a parameter    CREATE PROCEDUREPr_selectproduct@color VARCHAR( -)--define an input parameter     as         BEGIN            SELECT  *             fromproduction.productWHEREColor= @color;--requires the same color column as the input parameter        END; --Executing stored procedures        EXECDbo. Pr_selectproduct@color = 'Black' --varchar (+)--3 Creating a stored procedure that requires the input of two parameters        CREATE PROCEDUREpr_selectproducttwoparameters@safetyStockLevel INT ,            @recorderPoint INT         as             BEGIN                SELECT  *                 fromproduction.productWHERESafetystocklevel= @safetyStockLevel                         andReorderPoint> @recorderPoint            END; --Executing stored procedures            EXECDbo. Pr_selectproducttwoparameters@safetyStockLevel =  +,--int                @recorderPoint =  - --int                        EXECDbo. Pr_selectproducttwoparameters@safetyStockLevel =  -,--int                @recorderPoint =  - --int            --4 Creating a stored procedure with a return value            CREATE PROCEDUREPr_getproductid@makeFlag INT ,                @color VARCHAR(Ten) ,                @productID INTOUTPUT as                 BEGIN                    SELECT  @productID =ProductID fromproduction.productWHEREMakeFlag= @makeFlag                             andColor= @color                END; --Executing stored procedures                DECLARE @productID INT                EXECDbo. Pr_getproductid@makeFlag = 0,@color = 'Black',--varchar (TEN)                    @productID = @productIDOUTPUT--int--declares a variable to receive the return value after the stored procedure is executed                SELECT  @productID  asProductID;--5 Creating a stored procedure with default input parameters                CREATE PROCEDUREPr_selectproductname@name VARCHAR(Ten)= '%'                 as                     BEGIN                        SELECT  *                         fromproduction.productWHEREName like @name; END; --Executing stored procedures                    EXECDbo. Pr_selectproductname;--Default Parameters                    EXECDbo. Pr_selectproductname@name = 'B%' --varchar (10) manual input parameters

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.