zoukankan      html  css  js  c++  java
  • 数据库存储过程的写法

      系统数据库:

    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',''

      查询结果:

    记录编程的点滴,体会学习的乐趣
  • 相关阅读:
    225. Implement Stack using Queues
    150. Evaluate Reverse Polish Notation
    159 Longest Substring with At Most Two Distinct Characters
    142. Linked List Cycle II
    打印沙漏 (20 分)
    人见人爱A-B
    人见人爱A+B
    杨辉三角
    进制转换
    汉字统计
  • 原文地址:https://www.cnblogs.com/AduBlog/p/13474757.html
Copyright © 2011-2022 走看看