sql存储过程,mysql存储过程

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

在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权限应用于存储过程。

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

sql 存储过程怎样保存

方法和详细的操作步骤如下:

1、第一步,创建一个存储过程,该代码如图所示,见下图,转到下面的步骤。

2、第二步,执行完上面的操作之后,执行以下几行代码,并查看执行是否成功,现在,数据库中存在一个存储过程“sp_JY”,见下图,转到下面的步骤。

3、第三步,执行完上面的操作之后,该数组只有三行数据,见下图,转到下面的步骤。

4、第四步,执行完上面的操作之后,代码见下图,写入这些代码并执行,转到下面的步骤。

5、第五步,执行完上面的操作之后,查看JingYan表中的数据,可以看到当前数据为4,这表明对存储过程的调用已成功插入了新数据,见下图,转到下面的步骤。

6、第六步,执行完上面的操作之后,添加另一个存储过程,代码见下图,转到下面的步骤。

7、第七步,执行完上面的操作之后,调用新的存储过程,见下图。这样,就解决了这个问题了。

什么是SQL的存储过程

sql存储过程说简单点就是一个在t-sql下用户可以自行定义的函数,

但是与一般的函数也有不同的地方,比如它的返回值只能return(int类型),如果你要输出什么信息的话只能用output.这也是存储过程的一个特色吧,设定的参数可以有输出。讲起来有点抽象,给你个例子看看吧!

首先创建一个存储过程

create

procedure

cunchuguocheng

@a

int,

@b

int,

@c

int

output

as

begin

select

@c

=

@a+@b

return(0)

end

然后调用这个存储过程

declare

@value

int,

--返回值

@c

int

--结果值

exec

@value

=

cunchuguocheng

2,2,@c

output

select

@value

as

返回值

select

@c

as

结果值

程序写的很简单,你运行一下我想你就会对存储过程有所了解了。

(责任编辑:IT教学网)

更多

推荐HTML/Xhtml文章