简体   繁体   English

JAVA使用delete和insert语句调用存储过程

[英]JAVA call Stored Procedure with a delete and insert statement

My SP do include a DELETE and a INSERT INTO statement. 我的SP确实包含DELETE和INSERT INTO语句。 If DELETE statement success, but INSERT INTO do not, JAVA will only look at the first statement and concluded that the whole SP was executed successfully, which might be wrong. 如果DELETE语句成功,但是INSERT INTO没有,JAVA只会查看第一个语句并断定整个SP是否成功执行,这可能是错误的。

How can I executed more then one statement and get a exception in JAVA if just one of the statement fails. 如果只有一个语句失败,我怎样才能执行多个语句并在JAVA中获得异常。 I have been tried with TRY-CATCH in SQL but it all concluded that the all statement was executed correctly because the first statement success. 我已经尝试过在SQL中使用TRY-CATCH,但这一切都得出结论,所有语句都正确执行,因为第一个语句成功。

It is a MS SQL 2008 Database 它是MS SQL 2008数据库

ALTER PROCEDURE [testing] 
    @bikes  varchar(8000), 
    @groupw varchar(4), 
    @name char(6)

AS
BEGIN


        DELETE CP_customer_kngr_corr_s WHERE knid in ('blue')

        DECLARE @pos INT
        DECLARE @len INT
        DECLARE @value varchar(8000)

        set @pos = 0
        set @len = 0
        set @bikes= @bikes  +','

        WHILE CHARINDEX(',', @bikes, @pos)>0 BEGIN
            set @len = CHARINDEX(',', @bikes, @pos+1) - @pos
            set @value = SUBSTRING(@bikes, @pos, @len)

            INSERT INTO [bikes]
               ([bikes])
            VALUES
                (@value)

            set @pos = CHARINDEX(',', @bikes, @pos+@len) +1
        END 
END

What database is this? 这个数据库是什么? In most databases you can do a RAISEERROR when an operation fails. 在大多数数据库中,您可以在操作失败时执行RAISEERROR。 That would lead to an JDBCException been thrown. 这会导致抛出JDBCException。

You can check if the insert has succeeded by checking @@rowcount. 您可以通过检查@@ rowcount来检查插入是否成功。 If the number of rows effected is zero, you might want to throw an exception. 如果受影响的行数为零,则可能需要抛出异常。

http://technet.microsoft.com/en-us/library/ms187316.aspx http://technet.microsoft.com/en-us/library/ms187316.aspx

You can get rowcount for delete and raise error if number of rows deleted are zero and for insert statements you can use try/catch and raise error in catch block. 如果删除的行数为零,则可以获取rowcount for delete并引发错误;对于insert语句,可以使用try / catch并在catch块中引发错误。

//Perform delete
select @@ROWCOUNT

if @@ROWCOUNT = 0
  //RAISE ERROR

BEGIN TRY
 //perform insert here 
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;

SELECT @ErrorMessage = ERROR_MESSAGE(),
       @ErrorSeverity = ERROR_SEVERITY(),
       @ErrorState = ERROR_STATE();

-- Use RAISERROR inside the CATCH block to return 
-- error information about the original error that 
-- caused execution to jump to the CATCH block.
RAISERROR (@ErrorMessage, -- Message text.
           @ErrorSeverity, -- Severity.
           @ErrorState -- State.
           );

END CATCH;

Also check the error state returned by ERROR_STATE before passing it as a value to the state parameter of RAISERROR 在将其作为值传递给RAISERROR的state参数之前,还要检查ERROR_STATE返回的错误状态

Cheers !! 干杯!!

The problem in my SP was that the SET NOCOUNT ON; 我的SP中的问题是SET NOCOUNT ON; was not set. 没设定。 There by it return that the delete was a success and not a SELECT statement. 由此返回删除是成功的而不是SELECT语句。

Here are the final result. 这是最终结果。

ALTER PROCEDURE [testing] 
    @bikes  varchar(8000), 
    @groupw varchar(4), 
    @name char(6)

AS
BEGIN
    SET NOCOUNT ON;

        DELETE CP_customer_kngr_corr_s WHERE knid in ('blue')

        DECLARE @pos INT
        DECLARE @len INT
        DECLARE @value varchar(8000)

        set @pos = 0
        set @len = 0
        set @bikes= @bikes  +','

        WHILE CHARINDEX(',', @bikes, @pos)>0 BEGIN
            set @len = CHARINDEX(',', @bikes, @pos+1) - @pos
            set @value = SUBSTRING(@bikes, @pos, @len)

            INSERT INTO [bikes]
               ([bikes])
            VALUES
                (@value)

            set @pos = CHARINDEX(',', @bikes, @pos+@len) +1
        END 
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM