• SQL存储过程知识总结


    存储过程知识点总结,以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 
     
  • 相关阅读:
    数据库基本操作
    守护线程
    线程使用的场景
    创建多线程
    用正则表达式去截取网页里文字的方法。参数为读取的网页源代码
    文章生成器,Split方法截取字符串。从硬盘读取文件,和向硬盘存储文件参考代码
    winform 阶段学习总结
    Windowform 窗体关联数据库存储,读取图片,参考代码
    windows form窗体应用程序,建一个记事本参考代码,重点是打开,保存,另存为
    js实现相册翻页,滚动,切换,轮播功能
  • 原文地址:https://www.cnblogs.com/frankly/p/3515775.html
Copyright © 2020-2023  润新知