--查看数据库
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;--删除游标

[BILL WEI]SQL 存储过程学习的更多相关文章

  1. sql 存储过程学习

    1.存储过程 存储过程可以包含数据操纵语句.变量.逻辑 控制语句等,比如:单个select语句, select语句块,select语句与逻辑控制块. 存储过程优点: 执行速度更快 允许模块化程序设计 ...

  2. SQL数据库问题 解释一下下面的代码 sql 存储过程学习

    SQL数据库问题 解释一下下面的代码 2008-08-13 11:30wssqyl2000 | 分类:数据库DB | 浏览1154次 use mastergocreate proc killspid( ...

  3. [BILL WEI]SQL 如何将查询到的列作为表名去查询数据

    我们在做sql查询的时候,有时候需要将查询的列作为表名,去引用,然后再次查询 declare @table_name varchar(20) select @table_name=table_name ...

  4. [BILL WEI] SQL 巧用临时表

    在写sql 的时候,我们有时候,只需要拿出某些特定的数据,去跟一些表进行关联,但是如果直接通过表关联的话,可能拿到 的数据有冗余,这个时候,我们就可以巧用临时表,讲自己需要的特定数据筛选出来,然后组成 ...

  5. SQL存储过程概念剖析

    一.SQL存储过程的概念,优点及语法 定义:将常用的或很复杂的工作,预先用SQL语句写好并用一个指定的名称存储起来, 那么以后要叫数据库提供与已定义好的存储过程的功能相同的服务时,只需调用execut ...

  6. SQL server存储过程学习

    由于之前使用 Linq to Sql来操作数据库,对于数据库的存储过程.函数等比较薄弱.乘着自己闲着的时候,就百度自学了一点存储过程,以防以后要用. 基础通俗易懂的存储过程通过 存储过程学习 ,然后自 ...

  7. SQL server学习(三)T-SQL编程、逻辑控制语句和安全模式

    T-SQL编程 T-SQL编程与C语言类似,只是语法稍有不同而已,总体思想还是没有变化的.多的就不说了,还是从变量开始. 变量也分为全局变量和局部变量,表示方式稍有不同. 局部变量: 局部变量必须以标 ...

  8. SQL Server 学习系列之五

    SQL Server 学习系列之五 SQL Server 学习系列之一(薪酬方案+基础) SQL Server 学习系列之二(日期格式问题) SQL Server 学习系列之三(SQL 关键字) SQ ...

  9. SQL Sever 学习系列之一

    SQL Sever 学习系列之一 本学习系列,从实际工作需要中积累,对于一个新手而言,写出几条漂亮的查询语句,应该是可以受启发的. 一.问题的需求是:员工薪酬发放,现有资金能发放多少人,哪些人应得? ...

随机推荐

  1. setTimeOut传参数(转)

    无论是window.setTimeout还是window.setInterval,在使用函数名作为调用句柄时都不能带参数.带参数则立马执行,没有延时效果.可通过下面方式实现.  <script  ...

  2. json tree

    // 确定取消 ' return div; }, setCss: function () { var s = this.el.style; s.position = 'absolute'; s.top ...

  3. iOS 的 Gif 渲染引擎 FLAnimatedImage-b

    公司的项目有个首页加载一张2M左右的git图,刚做的时候是使用的SDWebImage里面的方法: + (UIImage *)sd_animatedGIFNamed:(NSString *)name; ...

  4. GWT RPC

    GWT RPC GWT RPCRemote Procedure Calls GWT: Google Web Toolkit的缩写,有了 GWT可以使用 Java 编程语言编写 AJAX 前端,然后 G ...

  5. shell复习---文件解压命令

    需要自己部署服务端,故在申请了空间之后,需要自己安装linux自己,自己安装Apache等,所以下载的压缩文件需要运行.网上找了一些解压命令不对,特别试了下面的方法有效,特别记录一下: 用ssh 登陆 ...

  6. 1305: [CQOI2009]dance跳舞 - BZOJ

    Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会 ...

  7. Mysql InnoDB彻底释放磁盘空间

    Innodb数据库对于已经删除的数据只是标记为删除,并不真正释放所占用的磁盘空间,这就导致InnoDB数据库文件不断增长. 如果需要彻底释放磁盘空间,则需要先导出数据,然后删除数据文件,最后导入数据. ...

  8. PAT-乙级-1033. 旧键盘打字(20)

    1033. 旧键盘打字(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 旧键盘上坏了几个键,于是在敲一段文 ...

  9. 1028-Digital Roots

    描述 The digital root of a positive integer is found by summing the digits of the integer. If the resu ...

  10. ***php 数组添加关联元素的方法小结(关联数组添加元素)

    我们这里介绍的是在数组中再增加关联数组了,这个就合成了多维数组,下面我来给大家举几个实例,希望对各位同学会有所帮助哈. 在"php 数组添加元素方法总结这篇文章中介绍了如何给数组添加元素,那 ...