• 存储过程知识总结【一】


    /*====================================================
    *描述:            存储过程知识点总结,以Northwind数据库的Employees表为例
    ======================================================*/

    --========================1.没有参数的存储过程================
    create procedure usp_NoParameterSelect
    as
    begin
    select * from dbo.Employees
    end

    GO

    --========================执行测试========================
    EXECUTE usp_NoParameterSelect

    GO
    --=======================2.带参数的存储过程===================

    create procedure usp_ParameterSelect
    (
    @employeeID INT
    )
    as

    begin
    select * from dbo.Employees
    where EmployeeID = @employeeID
    end

    --=======================执行测试===========================
    EXECUTE usp_ParameterSelect 1

    GO
    --=======================3.带多个参数的存储过程==================

    create procedure usp_MultiParameterSelect
    @employeeID INT,
    @employeeFirstName nvarchar(10)--要加上参数类型的长度
    as
    begin
    select * from dbo.Employees
    where EmployeeID = @employeeID
    and FirstName = @employeeFirstName
    end

    --========================执行测试===========================
    EXECUTE usp_MultiParameterSelect 1,'Nancy'

    GO

    --=======================4.output输出存储过程====================
    create procedure usp_OutputSelect
    @employeeID int ,
    @employeeFirstName nvarchar(10) output
    as
    begin
    select @employeeFirstName = FirstName from Employees
    where EmployeeID = @employeeID
    end

    --=======================执行测试=============================
    declare @name nvarchar(10)
    execute usp_OutputSelect 1,@name output
    select @name as name
    GO

    --======================5.return输出存储过程======================

    create procedure usp_ReturnSelect
    @employeeFirstName nvarchar(10)
    AS
    begin
    declare @employeeID INT
    select @employeeID = EmployeeID from Employees
    where FirstName = @employeeFirstName

    return @employeeID --**返回值必须是INT类型**
    end

    GO

    --======================执行测试===============================
    declare @employeeID INT
    execute @employeeID = usp_ReturnSelect 'Nancy'
    print @employeeID

    GO

    --=====================6.同时有output和return输出的存储过程=============

    create procedure usp_OutputAndReturnSelect
    @firstName nvarchar(10),
    @lastName nvarchar(20) output
    as
    begin
    declare @employeeID INT;

    select @employeeID = EmployeeID,@lastName=LastName from Employees
    where FirstName = @firstName

    return @employeeID
    end

    GO

    --======================执行测试================================
    declare @employeeID INT
    declare @lastName nvarchar(20)
    execute @employeeID = usp_OutputAndReturnSelect 'Nancy',@lastName output
    select @employeeID,@lastName


    /*===========================================================
    *****************************未完待续***************************************
    *============================================================*/

  • 相关阅读:
    矩形法求积分sin cos exp
    约瑟夫环问题
    KMP算法详解
    找出float型数组的最大值和最小值,分别和第一个和最后一个元素互换
    二重指针应用
    C++学习笔记(一)
    Line学习笔记
    node2vec学习笔记
    deepwalk学习笔记
    如何保证消息不被重复消费?(如何保证消息消费时的幂等性)
  • 原文地址:https://www.cnblogs.com/ucos/p/3512965.html
Copyright © 2020-2023  润新知