|
tsql改成:CREATE PROCEDURE AddToTb1
(@BmNm varchar(10),
@错误 nvarchar(50)='' output)
AS
set nocount on
declare @err intbegin transaction /*下面这个insert是正确的*/
insert into tb1 (BmName,RenNum) values (@bmNm,10)
set @err=@@error
if @err<>0
begin
rollback transaction
select @错误='未知错误'
return -100
end
/*下面这个insert是错误的,因为Rennum(人数)栏位只能接受数字(int),而传入值是字符
按理说传回值应该是-20才对,但是在执行下面第一句出错后,以后的根本就没执行*/
insert into tb1 (BmName,RenNum) values (@bmNm,'hahah')
set @err=@@error
if @err<>0
begin
rollback transaction
select @错误='未知错误'
return -200
end
/*下面这个insert是正确的*/
insert into tb1 (BmName,RenNum) values (@bmNm,20)
set @err=@@error
if @err<>0
begin
rollback transaction
select @错误='未知错误'
return -300
endcommit transaction
select @错误='执行成功'
return 0vba 改成
Private Sub Command0_Click()
On Error GoTo err
Dim cmd As New ADODB.Command
Dim Par As New ADODB.ParameterSet cmd.ActiveConnection = CurrentProject.Connectioncmd.Parameters.Append cmd.CreateParameter("@return_value", adInteger, adParamReturnValue, 8)
Set Par = cmd.CreateParameter("@BmNm", adVarChar, adParamInput, 10, "hahaha")
cmd.Parameters.Append Par
Set Par = cmd.CreateParameter("@错误", adVarChar, adParamInputOutput, 50, "")
cmd.Parameters.Append Parcmd.CommandText = "AddToTb1"
cmd.CommandType = adCmdStoredProc
cmd.ExecuteMsgBox cmd.Parameters("@return_value").Value, vbInformation, "系统提示"
MsgBox Par.Value, vbInformation, "系统提示"Exit Sub
err:
MsgBox "程序错误!", vbInformation, "系统提示"
End Sub
不过如果不加on error 的话就直接提示你数值转换发生错误了 |
|