当前位置:首页 » 存储配置 » sql存储过程判断

sql存储过程判断

发布时间: 2023-07-27 23:40:59

存储过程中用什么来判断sql语句是否执行成功

Transact-SQL 参考

@@ERROR
返回最后执行的 Transact-SQL 语句的错误代码。

语法
@@ERROR

返回类型
integer

注释
当 Microsoft® SQL Server™ 完成 Transact-SQL 语句的执行时,如果语句执行成功,则 @@ERROR 设置为 0。若出现一个错误,则返回一条错误信息。@@ERROR 返回此错误信息代码,直到另一条 Transact-SQL 语句被执行。您可以在 sysmessages 系统表中查看与 @@ERROR 错误代码相关的文本信息。

由于 @@ERROR 在每一条语句执行后被清除并且重置,应在语句验证后立即检查它,或将其保存到一个局部变量中以备事后查看。

示例
A.用 @@ERROR 检测一个特定错误
下面的示例用 @@ERROR 在一个 UPDATE 语句中检测限制检查冲突(错误 #547)。

USE pubs
GO
UPDATE authors SET au_id = '172 32 1176'
WHERE au_id = "172-32-1176"

IF @@ERROR = 547
print "A check constraint violation occurred"

B.用 @@ERROR 有条件地退出一个过程
在此示例中,IF...ELSE 语句在存储过程中的 INSERT 语句后检测 @@ERROR。@@ERROR 变量的值将决定传给调用程序的返回值,以指示此过程的成功与失败。

USE pubs
GO

-- Create the procere.
CREATE PROCEDURE add_author
@au_id varchar(11),@au_lname varchar(40),
@au_fname varchar(20),@phone char(12),
@address varchar(40) = NULL,@city varchar(20) = NULL,
@state char(2) = NULL,@zip char(5) = NULL,
@contract bit = NULL
AS

-- Execute the INSERT statement.
INSERT INTO authors
(au_id, au_lname, au_fname, phone, address,
city, state, zip, contract) values
(@au_id,@au_lname,@au_fname,@phone,@address,
@city,@state,@zip,@contract)

-- Test the error value.
IF @@ERROR <> 0
BEGIN
-- Return 99 to the calling program to indicate failure.
PRINT "An error occurred loading the new author information"
RETURN(99)
END
ELSE
BEGIN
-- Return 0 to the calling program to indicate success.
PRINT "The new author information has been loaded"
RETURN(0)
END
GO

C.用 @@ERROR 检测几条语句的成功
下面的示例取决于 INSERT 和 DELETE 语句的成功操作。局部变量在两条语句后均被设置为 @@ERROR 的值,并且用于此操作的共享错误处理例程中。

USE pubs
GO
DECLARE @del_error int, @ins_error int
-- Start a transaction.
BEGIN TRAN

-- Execute the DELETE statement.
DELETE authors
WHERE au_id = '409-56-7088'

-- Set a variable to the error value for
-- the DELETE statement.
SELECT @del_error = @@ERROR

-- Execute the INSERT statement.
INSERT authors
VALUES('409-56-7008', 'Bennet', 'Abraham', '415 658-9932',
'6223 Bateman St.', 'Berkeley', 'CA', '94705', 1)
-- Set a variable to the error value for
-- the INSERT statement.
SELECT @ins_error = @@ERROR

-- Test the error values.
IF @del_error = 0 AND @ins_error = 0
BEGIN
-- Success. Commit the transaction.
PRINT "The author information has been replaced"
COMMIT TRAN
END
ELSE
BEGIN
-- An error occurred. Indicate which operation(s) failed
-- and roll back the transaction.
IF @del_error <> 0
PRINT "An error occurred ring execution of the DELETE
statement."

IF @ins_error <> 0
PRINT "An error occurred ring execution of the INSERT
statement."

ROLLBACK TRAN
END
GO

D. 与 @@ROWCOUNT 一同使用 @@ERROR
下面的示例用 @@ERROR 和 @@ROWCOUNT 验证一条 UPDATE 语句的操作。为任何可能出现的错误而检验 @@ERROR 的值,而用 @@ROWCOUNT 保证更新已成功应用于表中的某行。

USE pubs
GO
CREATE PROCEDURE change_publisher
@title_id tid,
@new_pub_id char(4)
AS

-- Declare variables used in error checking.
DECLARE @error_var int, @rowcount_var int

-- Execute the UPDATE statement.
UPDATE titles SET pub_id = @new_pub_id
WHERE title_id = @title_id

-- Save the @@ERROR and @@ROWCOUNT values in local
-- variables before they are cleared.
SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT

-- Check for errors. If an invalid @new_pub_id was specified
-- the UPDATE statement returns a foreign-key violation error #547.
IF @error_var <> 0
BEGIN
IF @error_var = 547
BEGIN
PRINT "ERROR: Invalid ID specified for new publisher"
RETURN(1)
END
ELSE
BEGIN
PRINT "ERROR: Unhandled error occurred"
RETURN(2)
END
END

-- Check the rowcount. @rowcount_var is set to 0
-- if an invalid @title_id was specified.
IF @rowcount_var = 0
BEGIN
PRINT "Warning: The title_id specified is not valid"
RETURN(1)
END
ELSE
BEGIN
PRINT "The book has been updated with the new publisher"
RETURN(0)
END
GO

❷ oracle存储过程怎么判断一条sql语句是否成功

oracle存储过程判断一条sql语句是否成功的方法是增加exception处理,如果没有抛出exception,那就证明正常执行了。

在Oracle中,异常分为以下两类:

Oracle预定义异常

用户自定义异常

在Oracle中预定义的异常如下表所示:

在Oracle中有以下三种方式触发异常:

  • 由Oracle自动触发异常

  • 使用RAISE语句手工触发

  • 调用存储过程RAISE_APPLICATION_ERROR手工触发

代码将演示Oracle自动触发异常:

-- Created on 2015-7-14 by JellyThink
declare
iA NUMBER(2) := 10;
begin
iA := iA / 0; -- Oracle自动触发异常
dbms_output.put_line(iA);
exception
when ZERO_DIVIDE then
dbms_output.put_line('Error Code:' || SQLCODE || ' ' || SQLERRM);
when others then
dbms_output.put_line('Others Exception');
end;

❸ sql 怎么判断存储过程是否执行

select * from master.dbo.sysprocesses
-- 列出 spid > 50 的.

对每个 spid 用 dbcc inputbuffer(spid) 看执行的文本, 如果搜索完所有的 spid, 其执行的文本中都没有包括你的存储过程.
则基本上可判断出存储过程未执行.

❹ sql 判断库里是否存在GetUser 存储过程,如果存在,不做更改,如果不存在,就新建这个存储过程 代码要怎么写

通过系统对项表查询过程是否存在。
具体写法如下(CREATE 以后的语句换成你自已的存储过程)
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUser]') and OBJECTPROPERTY(id, N'IsProcere') = 1)
CREATE procere GetUser --请换成你自已的存储过程

@user varchar(100) output
as
set @user=isnull((select username from usertable where usercode=@user--
GO

❺ sql判断存储过程是否存在

--Sqlserver
ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[存储过程名]')andOBJECTPROPERTY(id,N'IsProcere')=1)
--你要做的
GO
--oracle
declare
cou:Integer;
begin
selectcount(*)intocoufromuser_objectswhereobject_type='PROCEDURE'andobject_name='存储过程名';
ifcou>0then
--存在
endif;
end;

❻ C#中使用 SQL 存储过程 ,怎样验证是否成功执行

对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于其他所有类型的语句,返回值为 -1。

验证SQL 存储过程成功执行可以通过输出参数来判断,自己在存储过程中增加
参考http://blog.csdn.net/wendy_chenlu/archive/2008/12/11/3498387.aspx

❼ sql存储过程 如何用IF来判断变量表内数据是否存在

可以定义一个变量接收值

declare @A as varchar(max),@count integer

select @A='select @count = count(*) from '+ @变量表 + ' where 姓名='+@姓名
exec(@A)
if @count > 0
...

❽ SQL存储过程中,if判断语句中有多个判断条

if(@rq2 is null or @rq2!=@a11) //多个判断条件
begin
处理
end
else
begin
处理
end

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:432
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:743
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:146
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:235
java驼峰 发布:2025-02-02 09:13:26 浏览:651
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:538
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726