存储过程实践

1.不含参数:

CREATE PROCEDURE pass1_xp 
AS
    SELECT * FROM StuTest WHERE Total>=250
EXEC pass1_xp

2.含参数:

CREATE PROCEDURE pass2_xp @pass INT
AS
    SELECT * FROM StuTest WHERE Total>=@pass
EXEC pass2_xp 250

3.含默认参数

CREATE PROCEDURE pass3_xp @pass INT = NULL
AS
    IF @pass IS NULL    
        BEGIN
            SELECT * FROM StuTest WHERE Total > = 250
        END
    ELSE
        BEGIN
            SELECT * FROM StuTest WHERE Total > = @pass
        END
EXEC pass3_xp

4.带输入参数的存储过程

CREATE PROCEDURE pass4_xp 
@pass   INT = NULL,
@studyNum VARCHAR(10) OUT,
@Name VARCHAR(10) OUTPUT
AS
    IF @pass IS NULL    
        BEGIN
            SELECT * FROM StuTest WHERE Total > = 250
        END
    ELSE
        BEGIN
            SELECT @Name=Stu_Name,@studyNum=Stu_ID FROM StuTest WHERE Total = @pass
        END
DECLARE @pass INT,
        @studyNum VARCHAR(10),
        @Name VARCHAR(10)
SET @pass=297
EXEC pass4_xp @pass,@studyNum OUT,@Name OUTPUT
SELECT @Name +'的学号是:'+@studyNum
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE name='pass4_xp')
DROP PROC pass4_xp
GO
USE Sample
GO
CREATE PROC pass4_xp
@TITLE VARCHAR(40) , @SUM DECIMAL OUTPUT 
AS 
   SELECT 'Stu_Name' = Stu_Name FROM t
   WHERE Stu_Name LIKE @TITLE +'%'
   SELECT @SUM = SUM(Total) FROM t 
   WHERE Stu_Name LIKE @TITLE +'%'
DECLARE @SUM DECIMAL
EXEC pass4_xp '李',@SUM OUTPUT
SELECT @SUM

demo:

IF EXISTS(SELECT * FROM SYSOBJECTS WHERE name='pass4_xp')
DROP PROC pass4_xp
GO
USE Sample
GO
CREATE PROC pass4_xp
@TITLE VARCHAR(40) , @SUM INT OUTPUT,@SPE VARCHAR(10) OUTPUT
AS 
   SELECT @SUM = SUM(Stu_PS) FROM StuInfo 
   WHERE Stu_Spe LIKE @TITLE + '%'
   SELECT DISTINCT @SPE = Stu_Spe FROM StuInfo 
   WHERE Stu_Spe LIKE @TITLE + '%'
DECLARE @TITLE VARCHAR(40),@SUM INT,@SPE VARCHAR(10)
SET @TITLE='信息'
EXEC pass4_xp @TITLE,@SUM OUTPUT,@SPE OUTPUT
PRINT @SPE +'总分为:'+ CAST(@SUM AS VARCHAR(10))
DECLARE @SPE VARCHAR(10)
SELECT DISTINCT @SPE = Stu_Spe FROM StuInfo 
   WHERE Stu_Spe LIKE '信息%'
PRINT @SPE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值