Three methods for obtaining SQL Server Stored Procedure definitions: SQL Server Stored Procedure

Source: Internet
Author: User

Three methods for obtaining SQL Server Stored Procedure definitions: SQL Server Stored Procedure

Concept of Stored Procedure

Stored Procedure is a set of SQL statements for specific functions. Compiled and stored in the database. You can specify the name of the stored Procedure and provide parameters for execution.

Stored Procedures can contain logical control statements and data manipulation statements. They can accept parameters, output parameters, return one or more result sets, and return values.

Since the stored procedure is compiled on the database server and stored in the database at the time of creation, the stored procedure runs faster than a single SQL statement block. At the same time, because you only need to provide the stored procedure name and necessary parameter information during the call, it can also reduce network traffic and simple network burden to a certain extent.

Advantages of Stored Procedures

A. stored procedures allow standard component programming

After a stored procedure is created, it can be called and executed multiple times in the program without re-writing the SQL statement of the stored procedure. Database professionals can modify the stored procedure at any time, but it has no impact on the application source code, which greatly improves the program portability.

B. Fast execution of Stored Procedures

If an operation contains a large number of T-SQL statement codes that are executed multiple times, the stored procedure is much faster than the batch execution. Because the stored procedure is pre-compiled, when a stored procedure is run for the first time, the query optimizer analyzes and optimizes the stored procedure and provides the storage plan that is finally stored in the system table. The T-SQL Statement of batch processing needs to be pre-compiled and optimized every time, so the speed will be slower.

C. reduce network traffic through stored procedures

For the same database object operation, if the T-SQL statements involved in this operation are organized into a stored procedure, when the stored procedure is called on the client, only this call statement is passed in the network; otherwise, multiple SQL statements are passed. This reduces network traffic and network load.

D. stored procedures can be fully utilized as a security mechanism

The system administrator can restrict the permissions of a stored procedure to prevent unauthorized users from accessing data and ensure data security.

The concepts of stored procedures and the advantages of stored procedures described above are all paving the way for this article. We will introduce three methods for defining the stored procedures of SQL Server. Let's take a look at them!

First:

Declare @ p_text varchar (max) SELECT @ p_text = text FROM syscomments WHERE id = (SELECT id FROM sysobjects WHERE name = 'stored procedure name') print @ p_text

However, when the content of the stored procedure is relatively long, the text content is not completely displayed.

Second:

declare @p_text varchar(max)SELECT @p_text= definition FROM sys.sql_modules JOIN sys.objects ON sys.sql_modules.object_id=sys.objects.object_id --and type='P' and sys.objects.name='sp_MSupd_dboInd_IndependenceReportTo' print @p_text

The content format is good, but the Print itself cannot display more than 8000 bytes of content, so the Stored Procedure content is too long.

Third:

exec sp_helptext '[sp_MSupd_dboRpt_Customer]'

Return the defined content, but the content of a text line is a record line, which can solve the above shortcomings.

The above is a small series of three methods for getting SQL Server Stored Procedure definitions, I hope to help you, if you have any questions, please leave a message, the small series will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.