数据库综合系列之存储过程
存储过程如同一门程序设计语言,同样包含了数据类型、流程控制、输入和输出和它自己的函数库。 存储过程作用: (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.
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;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The Xiaomi Mi 15 series is expected to be officially released in October, and its full series codenames have been exposed in the foreign media MiCode code base. Among them, the flagship Xiaomi Mi 15 Ultra is codenamed "Xuanyuan" (meaning "Xuanyuan"). This name comes from the Yellow Emperor in Chinese mythology, which symbolizes nobility. Xiaomi 15 is codenamed "Dada", while Xiaomi 15Pro is named "Haotian" (meaning "Haotian"). The internal code name of Xiaomi Mi 15S Pro is "dijun", which alludes to Emperor Jun, the creator god of "The Classic of Mountains and Seas". Xiaomi 15Ultra series covers

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Since the Huawei Mate60 series went on sale last year, I personally have been using the Mate60Pro as my main phone. In nearly a year, Huawei Mate60Pro has undergone multiple OTA upgrades, and the overall experience has been significantly improved, giving people a feeling of being constantly new. For example, recently, the Huawei Mate60 series has once again received a major upgrade in imaging capabilities. The first is the new AI elimination function, which can intelligently eliminate passers-by and debris and automatically fill in the blank areas; secondly, the color accuracy and telephoto clarity of the main camera have been significantly upgraded. Considering that it is the back-to-school season, Huawei Mate60 series has also launched an autumn promotion: you can enjoy a discount of up to 800 yuan when purchasing the phone, and the starting price is as low as 4,999 yuan. Commonly used and often new products with great value

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

PHP database connection guide: MySQL: Install the MySQLi extension and create a connection (servername, username, password, dbname). PostgreSQL: Install the PgSQL extension and create a connection (host, dbname, user, password). Oracle: Install the OracleOCI8 extension and create a connection (servername, username, password). Practical case: Obtain MySQL data, PostgreSQL query, OracleOCI8 update record.
