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