Transaction Processing in Stored Procedures

Source: Internet
Author: User

Create proc addfangweiandtable
(
@ Fangweiname varchar (20), --- azimuth name
@ Tablecount int, --- Number of table bits
@ Baojiancount int, --- Number of compartment
@ Returnval varchar (200) Output --- successful
)
As
Begin tran -- Development

----------------- Add orientation ------------------
If exists (select *
From fangwei
Where fangweiname = @ fangweiname)
Begin
Set @ returnval = '1'
End
Else
Begin
Insert into fangwei (fangweiname, fangweizhuo, fangweibo aojian)
Values (@ fangweiname, @ tablecount, @ baojiancount)
End

If @ error <> 0 -- Error
Begin
Set @ returnval = @ Error
Rollback tran -- rollback
Return
End

--- Variable Declaration
Declare @ tablelou char (10) --- floor
Declare @ tablenum int --- table number
Declare @ tabletype char (10) --- type
Declare @ tablebaojiannum int --- Compartment number

------------------- Add the orientation table -------------------------
Declare @ ID int
Select @ ID = 1

While @ ID <(@ tablecount + 1)
Begin
Set @ tablelou = @ fangweiname
-- Set @ tablenum = 'D' + convert (varchar, @ ID)
Set @ tablenum = @ ID
Set @ tablebaojiannum = 0
Set @ tabletype = 0

Insert into diningtable (tablelou, tablenum, tablebaojiannum, tabletype)
Values (@ tablelou, @ tablenum, @ tablebaojiannum, @ tabletype)

Set @ ID = @ ID + 1

If @ error <> 0 -- Error
Begin
Set @ returnval = @ Error
Rollback tran -- rollback
Return
End

End

 

----------------------- Add compartment --------------------------
Select @ ID = 1
While @ ID <(@ baojiancount + 1)
Begin
Set @ tablelou = @ fangweiname
Set @ tablenum = 0
-- Set @ tablebaojiannum = 'B' + convert (varchar, @ ID)
Set @ tablebaojiannum = @ ID
Set @ tabletype = 1

Insert into diningtable (tablelou, tablenum, tablebaojiannum, tabletype)
Values (@ tablelou, @ tablenum, @ tablebaojiannum, @ tabletype)

Set @ ID = @ ID + 1

If @ error <> 0 -- Error
Begin
Set @ returnval = @ Error
Rollback tran -- rollback
Return
End

End

Commit tran -- submit a transaction
Go

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.