In SQL, how does one obtain the value returned by the stored procedure using return and determine whether the SQL transaction is successfully executed?

Source: Internet
Author: User

1. How to obtain the value returned by return for a stored procedure
(1 ):Return returns a value.
Create procedure testreturn
As
Return 145
Go

-- Query calls in Analyzer
-- Declare @ RC int
-- Exec @ rc = singlevalue
-- Select @ RC

(2 ):Output Return Value
Create procedure testoutput
@ P1 int,
@ P2 int output,
@ P3 int output,
@ P4 varchar (10) Output

As
Select @ P2 = @ P1 * 2
Select @ P3 = @ P1 * 3
Select @ P4 = 'sfdsdfsdf'
Go

-- Query calls in Analyzer
-- Declare @ p2_output int
-- Execute testoutput 4, @ p2_output output
-- Select @ p2_output
-- =========================================== --
-- Declare @ p2_output int, @ p3_output int
-- Execute testoutput 4, @ p2_output output, @ p3_output output
-- Select @ p2_output, @ p3_output


(3 ):Return table
Create procedure tabletestsss
As
Declare @ ordershippertab table (col1 varchar (80), col2 varchar (80 ))
Insert @ ordershippertab values ('11', '12 ')
Insert @ ordershippertab values ('21', '22 ')
Insert @ ordershippertab values ('31', '32 ')
Insert @ ordershippertab values ('42', '42 ')
Select * From @ ordershippertab
Go

-- call in query analyzer
-- Create Table # T (col1 varchar (10), col2 varchar (10 ))
-- insert # T exec tabletestsss
-- select * from # T
-- drop table # T

2. how can I determine whether the SQL Server transaction is successfully executed?
use the system constant @ error to judge the value after each SQL statement is executed:
begin transaction
Delete...
If @ error <> 0
begin
rollback transaction
return
end
insert...
If @ error <> 0
begin
rollback transaction
return
end
commit transaction

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.