Look at the database again--(1) Stored procedure

Source: Internet
Author: User

When we perform a relatively simple operation, we generally use SQL statements. But when logic becomes more and more complex, the sequence of SQL statements executed only in sequence shows great limitations. At this point, you can consider using another method, that is, the stored procedure.

Now the database system often uses the client/server computing mode, a lot of work can be done on the client, also can be done on the server side. A very important difference between a client/server system and a traditional database system is that only the data is stored in the traditional database system, all applications are bundled with the application that the user is actually running, and in the client/server system, the program is stored in the database. Because this program is stored in the database as a database object, it is called a stored procedure.


To establish a stored procedure:




General structure of stored procedures:



Application Examples:

Add a record to the news table in the Niunan database.

The fields in the news table are:


Write the appropriate stored procedure:

SET ansi_nulls ongoset quoted_identifier ongo--=============================================--Author: Brisket--Create Date: April 3, 2014 15:06:13--Description: Add News-=============================================create PROCEDURE [dbo]. [news_insert]--definition variable @title varchar (+), @content text, @caid intasbegin--INSERT statement INSERT INTO News (title, [content], CATEGORYID) VALUES   (@title, @content, @caid) END

after executing the stored procedure, you can verify it.

EXEC News_insert "Entertainment News", "where is Dad going 2" Yangyangyang many Belle Handsome dad big Game ", 4


Add success!



To call a stored procedure in C #:

#region Add News///<summary>///News///        <param name= "n" > News entity Class </param>        ///<returns></returns> public bool Insert (news N)        {            //todo: Add News            BOOL flag = FALSE;            The stored procedure is named "News_insert"            string cmdtext = "News_insert";            Assign value to variable            sqlparameter[] paras = new sqlparameter[]{            new SqlParameter ("@content", n.content),            new SqlParameter ("@title", N.title),            new SqlParameter ("@categoryId", N.categoryid)};                        Execute sqlhelper,commandtype for the stored procedure type            int res = SqlHelper. ExecuteNonQuery (Cmdtext, paras, commandtype.storedprocedure);            if (res > 0)            {                return true;            }            return flag;        }        #endregion

There are many advantages to using stored procedures:

1, improve the speed of operation. Before running, the syntax and semantic parsing work for the stored procedure has been completed, and most of the preparation for the SQL statement it executes is completed when it is called. In contrast, when an interactive SQL statement is executed, it is slower.

2, enhanced the function and flexibility of SQL. Stored procedures can be written with process Control statements, with great flexibility to complete complex logic judgments and complex operations.

3, can reduce the network traffic. As mentioned earlier, stored procedures are stored on the server side, and performing all logical processes requires only one process request to the server.

4, reduce the process of writing the workload, this matter is obvious.

5, the indirect realization of security control function. Some users can be authorized to execute a stored procedure to complete an information query within the allowed range, rather than querying directly on tables and views.

Of course, it will also have some drawbacks:

SQL itself is a structured query language, plus some control (assignment, loop and exception handling, etc.), but not oo, in essence or process, in the face of complex business logic, the process of processing will be very laborious. There are also problems such as the inability to debug, the ability to apply caches, and the ability to cut (horizontal or vertical cuts) that do not fit the database. After the database is cut, the stored procedure does not know which database the data is stored in.



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.