sqlserver存储过程语法,sqlserver存储过程写法

http://www.itjxue.com  2023-01-18 07:10  来源:未知  点击次数: 

怎样在Sql server中创建,执行和删除存储过程

sqlserver存储过程的基本操作:

一、创建存储过程

1、语法格式:

create proc | procedure pro_name

[{@参数数据类型} [=默认值] [output],

{@参数数据类型} [=默认值] [output],

....

]

as

sql_statements以上是最基本语法,举个简单的例子:

create proc p_test

as

select retu = 1存储过程返回一个结果集:1

2、执行存储过程

execute procedure_name '' --存储过程如果有参数,后面加参数格式为:@参数名=value,也可直接为参数值value例子调用结果:

3、删除存储过程

drop procedure procedure_name --在存储过程中能调用另外一个存储过程,而不能删除另外一个存储过程

sql server的存储过程怎么写

第一步:点击数据库下的“可编程性”,选择“存储过程”,点击鼠标右键,选择“新建存储过程”

第二步:在create PROCEDURE 后 输入存储过程的名字,紧跟着的就是定义存储过程的参数,接下来就可以去编写自己所需要组装的存储过程语句了

第三步: 编译存储过程,在工具栏上按下执行按钮,如果没有错误,就编写成功了。

第四步:调用:在sqlserver的语句查询框中,输入exec 存储过程名 参数,执行就可以了。

基本语法格式如下:中括号带的是可选项

create proc | procedure pro_name

[{@参数数据类型} [=默认值] [output],

{@参数数据类型} [=默认值] [output],

....

]

as

begin

SQL_statements

--业务处理

end

在SQL中存储过程的一般语法是什么?

1、 创建语法

create?proc?|?procedure?pro_name

[{@参数数据类型}?[=默认值]?[output],

{@参数数据类型}?[=默认值]?[output],

....

]

as

SQL_statements

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

--创建存储过程

if?(exists?(select?*?from?sys.objects?where?name?=?'proc_get_student'))

drop?proc?proc_get_student

go

create?proc?proc_get_student

as

select?*?from?student;

--调用、执行存储过程

exec?proc_get_student;

3、 修改存储过程

--修改存储过程

alter?proc?proc_get_student

as

select?*?from?student;

4、 带参存储过程

--带参存储过程

if?(object_id('proc_find_stu',?'P')?is?not?null)

drop?proc?proc_find_stu

go

create?proc?proc_find_stu(@startId?int,?@endId?int)

as

select?*?from?student?where?id?between?@startId?and?@endId

go

exec?proc_find_stu?2,?4;

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%';

扩展资料:

SQL存储过程优点:

1、重复使用。存储过程可以重复使用,从而可以减少数据库开发人员的工作量。

2、减少网络流量。存储过程位于服务器上,调用的时候只需要传递存储过程的名称以及参数就可以了,因此降低了网络传输的数据量。

3、安全性。参数化的存储过程可以防止SQL注入式攻击,而且可以将Grant、Deny以及Revoke权限应用于存储过程。

参考资料来源:百度百科—存储过程

(责任编辑:IT教学网)

更多

推荐java认证文章