SQL-创建存储过程

/*
    日期:2021-3-6
    人员:LDH
    说明:SQL-演示存储过程
*/

USE TEST;
GO

CREATE TABLE People
(
    Id INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    Name NVARCHAR(20) NOT NULL,
    Lastname NVARCHAR(10) NOT NULL,
    Address NVARCHAR(200) NOT NULL
);

INSERT INTO People
(
    Name,
    Lastname,
    Address
)
VALUES
(N'大大', N'李', '中国上海'),
(N'小小', N'孙', '中国江苏');


SELECT Id,
       Name,
       Lastname,
       Address
FROM People;

-- ===================================================================
-- 创建存储过程(无参数)
-- ===================================================================
GO


CREATE PROCEDURE proc_GetPersonName
AS
BEGIN
    SELECT Id,
           Name,
           Lastname,
           Address
    FROM People;
END;


-- 执行存储过程
EXEC proc_GetPersonName;
GO


-- ===================================================================
-- 创建存储过程(有输入参数)
-- ===================================================================


CREATE PROCEDURE proc_GetPersonNameById @Id int
AS
BEGIN
    SELECT Id,
           Name,
           Lastname,
           Address
    FROM People
    WHERE Id = @Id;
END;

-- 执行存储过程
EXEC proc_GetPersonNameById 2;
GO

-- ===================================================================
-- 创建存储过程(有输出参数)
-- ===================================================================


CREATE PROCEDURE proc_GetPersonFullNameAndAddressById
(
    @Id int,
    @FullName nvarchar(50) OUTPUT,
    @Address nvarchar(200) OUTPUT
)
AS
BEGIN
    SELECT @FullName = Lastname + Name,
           @Address = Address
    FROM People
    WHERE Id = @Id;
END;

-- 执行存储过程
USE [TEST];
GO

DECLARE @return_value INT,
        @FullName NVARCHAR(50),
        @Address NVARCHAR(200);

EXEC @return_value = [dbo].[proc_GetPersonFullNameAndAddressById] @Id = 2, -- 输入参数赋值
                                                                  @FullName = @FullName OUTPUT,
                                                                  @Address = @Address OUTPUT;

SELECT @FullName AS N'@FullName',
       @Address AS N'@Address';

SELECT 'Return Value' = @return_value;

GO

 

上一篇:记录个超级Update语句


下一篇:vue---watch、computed和methods之间的区别