t–sql pl–sql

Hey, folks! In this article, we will be focusing on SQL Stored Procedures.

嘿伙计! 在本文中,我们将重点介绍SQL存储过程



什么是存储过程? (What is a Stored Procedure?)

Stored Procedures can be considered as Macros of System Programming. An SQL Stored Procedure is a customized and pre-defined SQL code that can be used at any point in time during the execution in the database by calling the function accordingly.

存储过程可以视为系统编程的宏。 SQL存储过程是一个自定义的预定义SQL代码,可以通过相应调用该函数在数据库中执行过程中的任何时间点使用它。

SQL Stored Procedures enable the reusability of code and help save a lot of execution time.

SQL存储过程可实现代码的可重用性,并有助于节省大量执行时间。

For example, let us consider a website’s login database wherein for any utility function it is necessary to preview the login details of a user. For the same, we can create a Stored Procedure containing an SQL SELECT statement to view the login details.

例如,让我们考虑一个网站的登录数据库,其中对于任何实用程序功能,必须预览用户的登录详细信息。 同样,我们可以创建一个包含SQL SELECT语句的存储过程来查看登录详细信息。

Thus, every time the database administrator wants to preview the login details of a user, that can be easily done by calling the Stored Procedure.

因此,每次数据库管理员想要预览用户的登录详细信息时,都可以通过调用存储过程来轻松完成。

This saves the overhead of executing the same SQL query again and again.

这样可以节省一次又一次执行同一SQL查询的开销。



创建一个SQL存储过程 (Creating an SQL Stored Procedure)

SQL Stored Procedures are actually defined as SQL code blocks to be called at the time of execution.

SQL存储过程实际上定义为在执行时要调用SQL代码块。

We have created a Table ‘Info’ using SQL CREATE and INSERT query with data columns as ‘id’ and ‘Cost’. SQL SELECT statement is used to display the contents of the table ‘Info’.

我们使用SQL CREATE和INSERT查询创建了一个表'Info',数据列为'id'和'Cost'。 SQL SELECT语句用于显示表“ Info”的内容。

Creating a DataBase:

创建一个数据库:


create table Info(id integer, Cost integer);
insert into Info(id, Cost) values(1, 100);
insert into Info(id, Cost) values(2, 50);
insert into Info(id, Cost) values(3, 65);
insert into Info(id, Cost) values(4, 97);
insert into Info(id, Cost) values(5, 12);select * from Info;

Output:

输出:


1   100
2   50
3   65
4   97
5   12

Having created the database Table, let us create a SQL Stored Procedure using the below command:

创建数据库表之后,让我们使用以下命令创建一个SQL存储过程:

Syntax:

句法:


CREATE PROCEDURE procedure-name
AS
<SQL Commands>
GO;

The GO statement is not a mandatory query. It helps the entire Stored Procedure block to execute separately and at once by the compiler.

GO语句不是强制性查询。 它有助于整个存储过程块分别由编译器立即执行。

Example:

例:


CREATE PROCEDURE show
AS
SELECT * FROM Info
GO;


执行存储过程 (Execution of a Stored Procedure)

SQL Stored Procedures can be called anywhere within the entire program execution through the below command:

可以通过以下命令在整个程序执行过程中的任何位置调用SQL存储过程:

Syntax:

句法:


EXEC procedure-name;

So, everytime we call the SQL Stored Procedure by SQL EXEC query, it executes the entire SQL block at once.

因此,每次我们通过SQL EXEC查询调用SQL存储过程时,它都会立即执行整个SQL块。

Example:

例:


EXEC show;

Output:

输出:


1   100
2   50
3   65
4   97
5   12


具有单个参数的存储过程 (Stored Procedure with a Single Parameter)

SQL Stored Procedures work with parameters as well. We can create Stored Procedures with one or more parameters and then call the procedures by providing the parameters to it.

SQL存储过程也可以使用参数。 我们可以使用一个或多个参数创建存储过程,然后通过向其提供参数来调用该过程。

Syntax:

句法:


CREATE PROCEDURE procedure-name @variable data-type
AS
SELECT * FROM Table
WHERE column = @variable;

Here, we have used SQL WHERE clause to put restrictions to the execution of stored procedure where only those data values will be displayed which satisfies the mentioned condition.

在这里,我们已使用SQL WHERE子句对存储过程的执行施加了限制,在该过程中将仅显示满足上述条件的那些数据值。

Example:

例:


CREATE PROCEDURE disp @Cost nvarchar(30)
AS
SELECT * FROM Info WHERE Cost = @Cost;

In the above example, we have created a stored procedure with a single parameter declared as ‘Cost’ with data type nvarchar. We applied a condition through WHERE clause according to which those values whose ‘Cost’ parameter value = “100” are displayed.

在上面的示例中,我们创建了一个存储过程,该存储过程的单个参数声明为“ Cost”,数据类型为nvarchar。 我们通过WHERE子句应用了一个条件,根据该条件,将显示“成本”参数值=“ 100”的那些值。


EXEC disp @Cost = "100";

Output:

输出:


Cost
100


具有多个参数SQL存储过程 (SQL Stored Procedures with Multiple Parameters)

SQL Stored Procedures occupy multiple parameters of the database by using a SQL AND operator.

SQL存储过程通过使用SQL AND运算符来占用数据库的多个参数。

Syntax:

句法:


CREATE PROCEDURE procedure-name @variable1 data-type, @variable2 data-type
AS
SELECT * FROM Table
WHERE column1 = @variable1 AND column2 = @variable2;

Example:

例:


CREATE PROCEDURE Multi_disp @Cost nvarchar(30), @id INTEGER
AS
SELECT * FROM Info WHERE Cost = @Cost AND id = @id;

So, in this example we have created a SQL Stored Procedure with two parameters ‘id’ and ‘Cost’.

因此,在此示例中,我们创建了一个带有两个参数“ id”和“ Cost”SQL存储过程。


EXEC Multi_disp @Cost = "100" , @id = "1";

As seen, when the compiler encounters the above execution statement, it displays only those data values whose id = 1 and cost = 100.

可以看到,当编译器遇到上述执行语句时,它仅显示id = 1和cost = 100的那些数据值。


id         Cost
1          100


结论 (Conclusion)

Thus, we have come to the end of this topic. Please feel free to comment in case you come across any doubt and for more such articles related to SQL, please do visit SQL JournalDev.

因此,我们到了本主题的结尾。 如果您有任何疑问,请随时发表评论,有关SQL的更多此类文章,请访问SQL JournalDev 。

参考资料 (References)

  • SQL Stored Procedure — DocumentationSQL存储过程-文档

翻译自: https://www.journaldev.com/40682/sql-stored-procedures

t–sql pl–sql

t–sql pl–sql_SQL存储过程–终极指南相关推荐

  1. t–sql pl–sql_SQL Server性能疑难解答的DBA指南–第1部分–问题和性能指标

    t–sql pl–sql It doesn't mean that every SQL Server slowdown is a performance problem. Some specific ...

  2. t–sql pl–sql_SQL Server性能疑难解答的DBA指南–第2部分–监视实用程序

    t–sql pl–sql SQL Server探查器 (SQL Server Profiler ) SQL Server Profiler is a monitoring tool that you ...

  3. t–sql pl–sql_SQL View –完整的介绍和演练

    t–sql pl–sql In relational databases, data is structured using various database objects like tables, ...

  4. t–sql pl–sql_SQL Server处理器性能指标–第1部分–最重要的CPU指标

    t–sql pl–sql Starting with this article, we will present the most important CPU metrics, describe th ...

  5. t–sql pl–sql_SQL Server处理器性能指标–第3部分–指示硬件组件问题的指标

    t–sql pl–sql Part 1 and 第1部分和Part 2 of the SQL Server processor performance metrics series, we descr ...

  6. t–sql pl–sql_SQL Server处理器性能指标–第4部分–处理器指标有助于更深入的调查和故障排除

    t–sql pl–sql In the previous part of this series, we presented the processor metrics that indicate h ...

  7. t–sql pl–sql_SQL Server –在T-SQL中使用最低的度量单位

    t–sql pl–sql A client recently discovered a discrepancy on one of our reports that showed an improve ...

  8. t–sql pl–sql_SQL串联正确完成–第1部分–可疑做法

    t–sql pl–sql This article is a part of three articles series to explore SQL Concatenation techniques ...

  9. t–sql pl–sql_SQL日期时间–您应该知道的15个重要功能!

    t–sql pl–sql Hey, folks! Hope you all are doing well. Today, in this article, we will be highlightin ...

最新文章

  1. Android App性能监控工具
  2. 洛谷 P3374 【模板】树状数组 1
  3. 研发人员为什么留不住
  4. Qt组件中的双缓冲无闪烁绘图
  5. Exception in thread main java.lang.NoClassDefFoundError
  6. TensorFlow 教程 --教程--2.5TensorFlow运作方式入门
  7. Android开发UI之GridLayout的使用
  8. 笔记5 bean的作用域
  9. PyTorch学习—2.张量的创建与张量的操作
  10. 找不org.apache.commons.lang3.builder.EqualsBuilder和commons-lang下载链接
  11. talib python文档_talib 中文文档(一):快速开始
  12. mysql 计算农历_SQL农历转换函数
  13. AT89C51使用DAC0832数模转换,波形发生器
  14. c开头英文语言,字母C开头的英文名
  15. 您可以在Windows PC或Android手机上使用iMessage吗?
  16. xmp预设怎么导入pr_ACR预设(XMP文件)安装教程(通用篇)
  17. 计算机系统原理实验——微程序控制器
  18. CLI、CLR、CTS、CLS
  19. 浅谈tomcat优化
  20. 高精度数字高程数据1m的dem

热门文章

  1. jQuery.access的作用
  2. 基于visual Studio2013解决面试题之1109全排列
  3. SQL语法集锦一:SQL语句实现表的横向聚合
  4. CSS背景图像的简单响应
  5. 80后开网店卖故事:1500多位为感觉而埋单
  6. 正则表达式 查找以某些字符开始 某些字符结束的匹配项 解决之道
  7. Sql 语句:显示 Sql Server 中所有表中的信息
  8. [转载] Python中NumPy简介及使用举例
  9. [转载] numpy数组遍历找到个数最多的元素
  10. [转载] python导入numpy函数库