原文出自http://blog.youkuaiyun.com/on_my_way20xx/article/details/7276122,此文是转载
1.如何获取存储过程用return返回的值
(1):return 返回一个值
CREATE PROCEDURE testReturn
AS
return 145
GO
--查询分析器中调用
--DECLARE @RC int
--exec @RC=singleValue
--select @RC
(2):output 返回值
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
--查询分析器中调用
--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):返回表
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
--查询分析器中调用
--Create Table #T (col1 varchar(10),col2 varchar(10))
--Insert #T exec tableTestsss
--Select * From #T
--drop table #T
2.如何判断SQL Server事务是否执行成功?
用系统常量@@error在执行每一个sql语句后的值来判断:
begin transaction
delete ...
if @@error<>0
begin
rollback transaction
return
end
insert into ...
if @@error<>0
begin
rollback transaction
return
end
commit transaction