搜索
首页数据库mysql教程数据库综合系列之存储过程

存储过程如同一门程序设计语言,同样包含了数据类型、流程控制、输入和输出和它自己的函数库。 存储过程作用: (1) 存储过程通过参数传递,安全性高,可防止注入式攻击. (2) 查询的语句在存储过程里,与程序不相关,如果以后要修改程序或者数据库,都不会出现连锁

存储过程如同一门程序设计语言,同样包含了数据类型、流程控制、输入和输出和它自己的函数库。

存储过程作用:

(1) 存储过程通过参数传递,安全性高,可防止注入式攻击.

(2) 查询的语句在存储过程里,与程序不相关,如果以后要修改程序或者数据库,都不会出现连锁反应,增加系统可扩展性.

(3) 网站执行查询的时候,只需要传递简单的参数就可以了,无论是代码优化上还是查询优化上都可以做到高效.

(4) 允许模块化编程,即,可以将一组查询写在一个过程里面,然后在程序里直接调用,而不必每次都写若干个语句来实现相应功能

具体使用:数据表来源http://blog.csdn.net/buyingfei8888/article/details/17399837

1 存储过程进行简单查询

if exists(select 1 from sysobjects where id=object_id('test') and xtype='P') --判断存储过程是否存在
drop proc test;
go
create proc test
as
select s_name 商店名字,s_address 商店地址,c_name 销售人员 from t_shop,t_cash_housewoker where s_id in(select s_id from manage where m_id=1) and t_cash_housewoker.m_id=1

执行:
exec test

对上面几个词汇解释 sysobjects object_id:

1、sysobjects 系统对象表。 保存当前数据库的对象,如约束、默认值、日志、规则、存储过程等 在sqlserver2005,sqlserver2008版本的数据库里,现在已经作为一个视图对象,在每一个数据库的系统视图中,都存在一个sys.sysobjects 视图对象。 sysobjects 重要字段解释: sysObjects ( Name sysname, --object 名称 id int, --object id xtype char(2), -- object 类型 type char(2), -- Object 类型(与xtype 似乎一模一样? 有点郁闷…) uid smallint, -- object 所有者的ID ... --其他的字段不常用到。 ) 注:需要解释的是 xtype 和type 是一模一样的,他的数据为: C = CHECK 约束 D = 默认值或 DEFAULT 约束 F = FOREIGN KEY 约束 FN = 标量函数 IF = 内嵌表函数 K = PRIMARY KEY 或 UNIQUE 约束 L = 日志 P = 存储过程 R = 规则 RF = 复制筛选存储过程 S = 系统表 TF = 表函数 TR = 触发器 U = 用户表 V = 视图 X = 扩展存储过程 AF = 聚合函数 (CLR) FS = 程序集 (CLR) 标量函数 FT = 程序集 (CLR) 表值函数 IF = 内联表函数 IT = 内部表 PC = 程序集 (CLR) 存储过程 PK = PRIMARY KEY 约束(type 为 K) SN = 同义词 SQ = 服务队列 TA = 程序集 (CLR) DML 触发器 TT = 表类型 UQ = UNIQUE 约束(type 为 K) 该表中包含该数据库中的所有对象,如有那些表 存储过程 视图 等信息 2 object_id
在sysobjects系统表中存储着数据库的所有对象,每个对象都有一个唯一的id号进行标识.
   object_id就是根据对象名称返回该对象的id.
2 带参数存储
if (object_id('test', 'P') is not null)
    drop proc test
go
create proc test(@Id int)
as
   select s_name 商店名字,s_address 商店地址,c_name 销售人员 from t_shop,t_cash_housewoker where s_id in(select s_id from manage where m_id=@Id) and t_cash_housewoker.m_id=@Id

go

执行:
exec test 1;

3 带通配符的存储
if (object_id('test', 'P') is not null)
    drop proc test
go
create proc test(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
as
    select * from manage where m_name like @name or m_name like @nextName;
go

执行:
exec test;
exec test '%步%', '%u%';

4 带输出参数的存储过程
if (object_id('test', 'P') is not null)
    drop proc test
go
create proc test(
    @id int, --默认输入参数
    @name varchar(20) out, --输出参数
    @age varchar(20) output--输入输出参数
)
as
    select @name=m_name,@age=m_bir from manage where m_id=@id

执行:
declare @id int,
        @name varchar(20),
        @bir varchar(20);
set @id = 1; 
exec test @id, @name out, @bir output;
select @name, @bir;
print @name + '#' + @bir;
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何在MySQL中删除或修改现有视图?如何在MySQL中删除或修改现有视图?May 16, 2025 am 12:11 AM

todropaviewInmySQL,使用“ dropviewifexistsview_name;” andTomodifyAview,使用“ createOrreplaceViewViewViewview_nameAsSelect ...”。whendroppingaview,asew dectivectenciesanduse和showcreateateviewViewview_name;“ tounderStanditSsstructure.whenModifying

MySQL视图:我可以使用哪些设计模式?MySQL视图:我可以使用哪些设计模式?May 16, 2025 am 12:10 AM

mySqlViewScaneFectectialized unizedesignpatternslikeadapter,Decorator,Factory,andObserver.1)adapterPatternadaptSdataForomDifferentTablesIntoAunifiendView.2)decoratorPatternenhancateDataWithCalcalcualdCalcalculenfields.3)fieldfields.3)

在MySQL中使用视图的优点是什么?在MySQL中使用视图的优点是什么?May 16, 2025 am 12:09 AM

查看InMysqlareBeneForsImplifyingComplexqueries,增强安全性,确保dataConsistency,andOptimizingPerformance.1)他们simimplifycomplexqueriesbleiesbyEncapsbyEnculatingThemintoreusableviews.2)viewsEnenenhancesecuritybyControllityByControllingDataAcces.3)

如何在MySQL中创建一个简单的视图?如何在MySQL中创建一个简单的视图?May 16, 2025 am 12:08 AM

toCreateAsimpleViewInmySQL,USEthecReateaTeviewStatement.1)defitEtheetEtheTeViewWithCreatEaTeviewView_nameas.2)指定usethectstatementTorivedesireddata.3)usethectStatementTorivedesireddata.3)usetheviewlikeatlikeatlikeatlikeatlikeatlikeatable.views.viewssimplplifefifydataaccessandenenanceberity but consisterfort,butconserfort,consoncontorfinft

MySQL创建用户语句:示例和常见错误MySQL创建用户语句:示例和常见错误May 16, 2025 am 12:04 AM

1)foralocaluser:createUser'localuser'@'@'localhost'Indidendify'securepassword'; 2)foraremoteuser:creationuser's creationuser'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Rocaluser'@'localhost'Indidendify'seceledify'Securepassword'; 2)

在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)