The simplest SQL stored procedure call (it is worth noting and practical significance for those who do not store the procedure)

Source: Internet
Author: User

I will not talk about the concept of the stored procedure, because the search engine is more detailed and wonderful than me. I will only give an example to do more practical things.

First, create a database sample and then use the following statement to create a table. Of course, you 'd better insert some data in the table. If you don't want it, you can. Call the added stored procedure first, and then call the Stored Procedure for deletion, modification, and query.

Use [sample] <br/>/******** create the database *******************/<br /> go <br/>/****** object: table [DBO]. [Test] script Date: 03/11/2010 10:36:42 ******/<br/> set ansi_nulls on <br/> go <br/> set quoted_identifier on <br/> go <br/> Create Table [DBO]. [Test] (<br/> [ID] [int] Null, <br/> [name] [nchar] (10) Collate chinese_prc_ci_as null <br/>) on [primary] <br/>

It may be worth noting that the stored procedure is stored in the database, which is the same as the trigger. You only need to put the following statement in the database for execution.

-- ===================================================== ==========< Br/> -- template generated from template Explorer using: <br/> -- create procedure (new menu ). SQL <br/> -- use the specify values for template parameters <br/> -- command (CTRL-shift-m) to fill in the parameter <br/> -- values below. <br/> -- this block of comments will not be added in <br/> -- the definition of the procedure. <br/> -- ======================================== =============< br/> set ansi_nulls on <br/> go <br/> set quoted_identifier on <br/> go <br/> -- ================================================ ========< br/> -- Author: <author, Name> <br/> -- create Date: <create date, >< br/> -- Description: <description,> <br/> -- ======================================== ===========< br/> Create procedure proc_insert <br/> -- add the parameters for the stored procedure here <br/> (<br/> @ temp_id int, <br/> @ temp_name nvarchar (50) <br/>) </P> <p> begin <br/> -- set nocount on added to prevent extra result sets from <br/> -- interfering with select statements. <br/> set nocount on; </P> <p> -- insert statements for procedure here <br/> insert into DBO. test (ID, name) values (@ temp_id, @ temp_name) <br/> end <br/> go <br/> exec proc_test 'test ', 2 </P> <p> -- the key here is that the as should not be less <br/> -- With the in, the end cannot be absent <br/> -- the first storage creation process use create, for the second and later versions, you need to change create to alter <br/> -- that is, edit the Stored Procedure <br/> -- if the parameters following exec do not want to be completely written, it must correspond to the parameter order of the stored procedure.

-- ===================================================== ==========< Br/> -- template generated from template Explorer using: <br/> -- create procedure (new menu ). SQL <br/> -- use the specify values for template parameters <br/> -- command (CTRL-shift-m) to fill in the parameter <br/> -- values below. <br/> -- this block of comments will not be added in <br/> -- the definition of the procedure. <br/> -- ======================================== =============< br/> set ansi_nulls on <br/> go <br/> set quoted_identifier on <br/> go <br/> -- ================================================ ========< br/> -- Author: <author, Name> <br/> -- create Date: <create date, >< br/> -- Description: <description,> <br/> -- ======================================== ===========< br/> Create procedure proc_delete <br/> -- add the parameters for the stored procedure here <br/> @ temp_id int <br/> as <br/> begin <br/> -- set nocount on added to prevent extra result sets from <br/> -- interfering with select statements. <br/> set nocount on; </P> <p> -- insert statements for procedure here <br/> Delete from test where id = @ temp_id <br/> end <br/> go <br/> Exec proc_delete 1 </P> <p> -- as keyword cannot be less <br/> -- there is begin cannot be less end <br/> -- exec call <br/> -- create storage for the first time create, for the second and later versions, you need to change create to alter <br/> -- that is, edit the Stored Procedure <br/> -- if the parameters following exec do not want to be completely written, it must correspond to the parameter order of the stored procedure.

-- ===================================================== ==========< Br/> -- template generated from template Explorer using: <br/> -- create procedure (new menu ). SQL <br/> -- use the specify values for template parameters <br/> -- command (CTRL-shift-m) to fill in the parameter <br/> -- values below. <br/> -- this block of comments will not be added in <br/> -- the definition of the procedure. <br/> -- ======================================== =============< br/> set ansi_nulls on <br/> go <br/> set quoted_identifier on <br/> go <br/> -- ================================================ ========< br/> -- Author: <author, Name> <br/> -- create Date: <create date, >< br/> -- Description: <description,> <br/> -- ======================================== ===========< br/> Create procedure proc_update <br/> -- add the parameters for the stored procedure here <br/> @ temp_id int <br/> as <br/> begin <br/> -- set nocount on added to prevent extra result sets from <br/> -- interfering with select statements. <br/> set nocount on; </P> <p> -- insert statements for procedure here <br/> Update Test Set Name = 'jack' where id = @ temp_id <br/> end <br/> go <br/> exec proc_update 2 </P> <p> -- as keyword cannot be less <br/> -- begin in cannot be less end <br/> -- exec call <br/> -- create, for the second and later versions, you need to change create to alter <br/> -- that is, edit the Stored Procedure <br/> -- if the parameters following exec do not want to be completely written, it must correspond to the parameter order of the stored procedure.

-- ===================================================== ==========< Br/> -- template generated from template Explorer using: <br/> -- create procedure (new menu ). SQL <br/> -- use the specify values for template parameters <br/> -- command (CTRL-shift-m) to fill in the parameter <br/> -- values below. <br/> -- this block of comments will not be added in <br/> -- the definition of the procedure. <br/> -- ======================================== =============< br/> set ansi_nulls on <br/> go <br/> set quoted_identifier on <br/> go <br/> -- ================================================ ========< br/> -- Author: <author, Name> <br/> -- create Date: <create date, >< br/> -- Description: <description,> <br/> -- ======================================== ============< br/> Create procedure proc_select <br/> -- add the parameters for the stored procedure here <br/> as <br/> begin <br/> -- set nocount on added to prevent extra result sets from <br/> -- interfering with select statements. <br/> set nocount on; </P> <p> -- insert statements for procedure here <br/> select * from test <br/> end <br/> go <br/> exec proc_select </P> <p> -- as keyword cannot be less <br/> -- there is a begin and cannot be less end <br/> -- exec call <br/> -- create is used for the first storage creation process, for the second and later versions, you need to change create to alter <br/> -- that is, to edit the Stored Procedure <br/>

I have written the key points in the code above. The key points are described in the final comments of each code.

This is a transitional learning, and I hope it will bring you the feeling of opening up a village.

 

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.