• [BILL WEI]SQL 存储过程学习


    --查看数据库
    exec sp_databases ;
    --查看表
    exec sp_tables ;
    --查看列
    exec sp_columns WMS_ASN;
    --查看索引
    exec sp_helpindex WMS_ASN;
    --约束
    exec sp_helpconstraint WMS_ASN;
    --列出数据库中的所有存储过程
    exec sp_stored_procedures
    --查看存储过程创建、定义语句
    exec sp_helptext 'xp_revokelogin'
    --修改表、索引、列的名称
    exec sp_rename WMS_ASN,WMS_ASNS
    exec sp_rename 'wms_asn.kids','KID','column'
    select * from wms_asn

    --更改数据库名称
    exec sp_renamedb myTempDB, myDB;
    --更改登录名的默认数据库
    exec sp_defaultdb 'master', 'myDB';
    --数据库帮助,查询数据库信息
    exec sp_helpdb
    exec sp_helpdb master

    --重命名索引
    exec sp_rename N'WMS_ASN.PK_WMS_ASN_KID1', N'PK_WMS_ASN_KID', N'index';
    exec sp_help 'WMS_ASN';

    --查询所有存储过程
    select * from sys.objects where type = 'P';
    select * from sys.objects where type_desc like '%pro%' and name like 'sp%';

    Ø 用户自定义存储过程

    1. 创建语法

    create proc | procedure pro_name
    [{@参数数据类型} [=默认值] [output],
    {@参数数据类型} [=默认值] [output],
    ....
    ]
    as
    SQL_statements

    2.创建不带参数存储过程

    --创建存储过程
    if (exists (select * from sys.objects where name = 'proc_get_asn'))
    drop proc proc_get_asn
    go
    create proc proc_get_asn
    as
    select * from wms_asn;

    --调用、执行存储过程
    exec proc_get_asn;

    3.修改存储过程

    --修改存储过程
    alter proc proc_get_asn
    as
    select * from wms_asn where type='0003' ;
    drop proc proc_get_asn
    4. 带参存储过程

    --带参存储过程
    if (object_id('proc_find_asn', 'P') is not null)
    drop proc proc_find_asn
    go
    create proc proc_find_asn(@startId numeric, @endId numeric)
    as
    select * from wms_asn where gross_weight between @startId and @endId
    go

    exec proc_find_asn 500, 1000;

    exec proc_find_asn 0,10000;


    5. 带通配符参数存储过程

    --带通配符参数存储过程
    if (object_id('proc_findStudentByName', 'P') is not null)
    drop proc proc_findStudentByName
    go
    create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
    as
    select * from student where name like @name and name like @nextName;
    go

    exec proc_findStudentByName;
    exec proc_findStudentByName '%o%', 't%';


    6. 带输出参数存储过程

    if (object_id('proc_getStudentRecord', 'P') is not null)
    drop proc proc_getStudentRecord
    go
    create proc proc_getStudentRecord(
    @id int, --默认输入参数
    @name varchar(20) out, --输出参数
    @age varchar(20) output--输入输出参数
    )
    as
    select @name = name, @age = age from student where id = @id and sex = @age;
    go

    --
    declare @id int,
    @name varchar(20),
    @temp varchar(20);
    set @id = 7;
    set @temp = 1;
    exec proc_getStudentRecord @id, @name out, @temp output;
    select @name, @temp;
    print @name + '#' + @temp;


    7. 不缓存存储过程

    --WITH RECOMPILE 不缓存
    if (object_id('proc_temp', 'P') is not null)
    drop proc proc_temp
    go
    create proc proc_temp
    with recompile
    as
    select * from student;
    go

    exec proc_temp;

    8. 加密存储过程

    --加密WITH ENCRYPTION
    if (object_id('proc_temp_encryption', 'P') is not null)
    drop proc proc_temp_encryption
    go
    create proc proc_temp_encryption
    with encryption
    as
    select * from student;
    go

    exec proc_temp_encryption;
    exec sp_helptext 'proc_temp';
    exec sp_helptext 'proc_temp_encryption';

    9. 带游标参数存储过程

    if (object_id('proc_cursor', 'P') is not null)
    drop proc proc_cursor
    go
    create proc proc_cursor
    @cur cursor varying output
    as
    set @cur = cursor forward_only static for
    select id, name, age from student;
    open @cur;
    go
    --调用
    declare @exec_cur cursor;
    declare @id int,
    @name varchar(20),
    @age int;
    exec proc_cursor @cur = @exec_cur output;--调用存储过程
    fetch next from @exec_cur into @id, @name, @age;
    while (@@fetch_status = 0)
    begin
    fetch next from @exec_cur into @id, @name, @age;
    print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);
    end
    close @exec_cur;
    deallocate @exec_cur;--删除游标

  • 相关阅读:
    c# 进制
    java生成验证码
    java基础练习题
    java九九乘法表
    java list集合练习
    深入理解Java的接口和抽象类
    java 接口 练习
    java泛型详解
    Java 继承 小练习
    Java单例模式深入详解
  • 原文地址:https://www.cnblogs.com/teamate/p/3718827.html
Copyright © 2020-2023  润新知