SQL Proc (Stored procedure)/tran (things)

Source: Internet
Author: User

Stored procedures are like C # methods

1. The thing is written in the process, directly call the stored procedure

1.1 Procedure with no parameters

/*transaction things, procedure stored procedures */create proc copytable_1_10000 asbegin tran--start things declare @tran_error int;--declaration parameter SET @ tran_error=0;--Assigning a value to a parameter declare @i int,@y int;set @i=10000;set @y=1;/* A new table does not exist, copy data to a new table select * Into Table_3 from (select*from Table_1) as A*/while @y<@i   --Cycle begin/   * New table exists, copy data *   /INSERT INTO Table_3 (materialname,mtype) Select Materialname,mtype from Table_1   set @[email protected]+1;--loop condition   set @[email protected][email protected] @ERROR ;--the system parameter that the thing used to record the error end/* to determine if the execution of the thing is wrong */if (@tran_error >0) [email protected]_error greater than 1 means error, things roll back begin rollback Tran; print ' things rollback ' endelsebegin commit tran print ' commit thing ' end
--Call the stored procedure exec copytable_1_10000

1.2 process with the transfer of parameters

--proc with parameters create proc Showdescription@mtype int--The parameters to be passed Asbeginselect * into  #table_3 from (select Table_1. Materialname,table_2.mtypedescription from Table_1 left join table_2 on  table_1.mtype=table_2.id where [email Protected]) as C select * FROM #table_3end--call, pass @mtype parameter exec showdescription @Mtype =2--Delete drop proc Showdescription

  

SQL Proc (Stored procedure)/tran (things)

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.