SQL 中的存储过程
我很了解 SQL 语句,例如 select、insert、update 和 delete。不过我对存储过程很陌生。
我正在使用 SQL Management Studio Express。
有代码示例吗?例如,注册,一个将客户数据保存在数据库中的存储过程。
I am well aware of SQL statements such as select, insert, update and delete. However I am new to stored procedures.
I am using SQL Management Studio Express.
Any code example? For example, Registration, a stored procedure that saves the customer data in the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用存储过程而不是嵌入式 SQL 的最好的一点是,如果操作得当,它们会为数据库提供一个独立于客户端代码的可版本控制的接口。它将 SQL 从客户端代码库中取出,这意味着只要外部接口不变:参数、结果集等,DBA 就可以自由调整查询,甚至完全重塑数据库模式,而无需更改客户端软件。
它还极大地简化了安全性。客户端不需要授予基础表本身的权限。他们所需要的只是对存储过程进行
授予执行
。如果您所做的只是编写一个包含简单插入/更新/删除语句的存储过程,那么您实际上并没有取得多大成就。存储过程应该代表更多的逻辑操作。
请记住,任何真正的数据库都会比用于访问它的软件寿命更长。数据是永恒的;软件是暂时的。
The best thing about using stored procedures in preference to embedded SQL is that, done properly, they present a versionable interface to the database that is independent of the client code. It gets the SQL out of the client codebase, which means that so long as the external interface doesn't change: parameters, results sets, etc., the DBAs are free to tune queries or even completely remodel the database schema without requiring changes to the client software.
It also hugely simplifies security. Clients don't require permission grants to the underlying tables themselves. All they need is
grant execute
on the stored procedures.If all you do is write a stored procedures that wrap simple insert/update/delete statements, you're not really accomplishing much. The stored procedures should represent more logical operations.
Remember, any real database will long outlive the software used to access it. Data is forever; software is transient.
您可以使用 参数化查询 避免 SQL 注入 - 存储过程并不是唯一的方法。
You could use parameterized queries to avoid SQL injection - stored procedures are not the only way.
不错的文章此处解释了如何避免 SQL 注入攻击。阅读附加注意事项。
最简单的方法是将所有查询参数化,如下所示
Nice article here explaining how to avoid SQL Injection attacks. Read the Addition Considerations.
The simpliest way to put it is to paramaterise all your queries like below
这是您请求的示例:
要从另一个位置调用它并传递 5 作为参数,请执行以下操作:
Here's an example as you requested:
To call it from another location passing 5 as parameter, do: