First, decide if a stored procedure is needed. In QIE use qie.doUpdateQuery and wrap it in a try catch like this:
try {
var result = qie.doUpdateQuery('AdventureWorks2012', "INSERT INTO\n" +
" [Production].[ProductReview] ([ProductID],[ReviewerName],[ReviewDate],[EmailAddress],[Rating])\n" +
"VALUES (1,'rich',CURRENT_TIMESTAMP,'rich@test.com',30)");
//The next statement is never called because the rating is constrained to a value from 1 - 5
qie.debug("*** result: " + result);
} catch (err) {
qie.debug("*** error ***" + err);
}
Note: The above insert fails because the rating is outside the range of 1 - 5. Passing 30 causes it to fail with this error:
com.qvera.qie.exception.NoResultsException: com.microsoft.sqlserver.jdbc.SQLServerException: The INSERT statement conflicted with the CHECK constraint "CK_ProductReview_Rating". The conflict occurred in database "AdventureWorks2012", table "Production.ProductReview", column 'Rating'.
Query: INSERT INTO
[Production].[ProductReview] ([ProductID],[ReviewerName],[ReviewDate],[EmailAddress],[Rating])
VALUES (1,'rich',CURRENT_TIMESTAMP,'rich@test.com',30)
If a stored procedure is required, there are couple things that need to change.
First, stored procedures with INSERT/UPDATE/DELETE statements present a problem because they return a rows affected count. Use the "SET NOCOUNT ON" either within the stored procedure itself or with in the script passed to qie.doQuery as a workaround.
In QIE:
var sp_call =
"SET NOCOUNT ON\n" +
"BEGIN\n" +
"DECLARE @return_value int,\n" +
" @rCode char(1)\n" +
"\n" +
" EXEC @return_value = [dbo].[richRatings]\n" +
" @code = " + rating + ",\n" +
" @rCode = @rCode OUTPUT\n" +
"\n" +
" SELECT\n" +
" @return_value as N'return_value',\n" +
" @rCode as N'rCode'\n" +
"END";
or in stored procedure:
CREATE PROC [dbo].[richRatings]
( @code INT,
@rCode CHAR OUTPUT
)
AS
SET NOCOUNT ON
SET @rCode = 'A';
DECLARE @ErrorVar INT;
DECLARE @RowCountVar INT;
BEGIN TRY
INSERT INTO [Production].[ProductReview]
([ProductID],[ReviewerName],[ReviewDate],[EmailAddress],[Rating])
VALUES
(1,'rich',CURRENT_TIMESTAMP,'rich@test.com',@code)
END TRY
BEGIN CATCH
SELECT @ErrorVar = @@ERROR, @RowCountVar = @@ROWCOUNT;
IF @ErrorVar <> 0
BEGIN
SET @rCode = 'E'
END
IF @RowCountVar = 0
BEGIN
SET @rCode = 'E'
END
END CATCH;
RETURN 0
GO
Second, the variables declared in this example stored procedure are just for illustration. When called via a JDBC driver, @rCode, @ErrorVar, and @RowCount may not have been set without the try catch blocks.