数据库存储过程的写法

  系统数据库:

exec sp_databases; --查看数据库
exec sp_tables;        --查看表
exec sp_columns student;--查看列
exec sp_helpIndex student;--查看索引
exec sp_helpConstraint student;--约束
exec sp_stored_procedures;
exec sp_helptext 'sp_stored_procedures';--查看存储过程创建、定义语句 经常用到这句话来查看存储过程,like sp_helptext sp_getLoginInfo.
exec sp_rename student, stuInfo;--修改表、索引、列的名称
exec sp_renamedb myTempDB, myDB;--更改数据库名称
exec sp_defaultdb 'master', 'myDB';--更改登录名的默认数据库
exec sp_helpdb;--数据库帮助,查询数据库信息
exec sp_helpdb master;

 

  存储过程基本语法:

CREATE  PROC[EDURE]  存储过程名

              @参数1  数据类型 = 默认值,

               …… ,

              @参数n  数据类型 OUTPUT

            AS

            SQL语句

    GO

 

  实例:(制作考试分数条)

create proc sp_scorerank
    @Gid varchar(10),
    @Cid varchar(10)
    as
        begin
            if(@Cid='')
                begin
                    select a.StuName,Chinese+English+math Total,b.Chinese,b.English,b.Math,
                    Rank() over(order by Chinese+English+math desc) 
                    from Students a left join Score b on a.StuID=b.SID
                    where a.GradeID=@Gid 
                end
            else
                begin
                    select a.StuName,Chinese+English+math Total,b.Chinese,b.English,b.Math,
                    Rank() over(order by Chinese+English+math desc) 
                    from Students a left join Score b on a.StuID=b.SID
                    where a.GradeID=@Gid and a.ClassID=@Cid 
                end
        end
    go

    exec sp_scorerank 'G02',''

  查询结果:

posted @ 2020-08-11 12:04  RookieCoderAdu  阅读(2774)  评论(0编辑  收藏  举报