Based on below C# code i have wrote the statement in SQL stored procedure when inv_qty == 2 , to pop up alert message.
But after showing the alert message.The data transaction has been recorded.How to stop the transaction if pop up the alert message is active?
As i know SQL stored procedure is not able to stop the transaction if write the statement, How can i achieve this?
float INV_QTY = Convert.ToInt32(_cmd.Parameters["@RecordFound"].Value.ToString());
if (INV_QTY == 2)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('" + Item + " Qty is more than exist Qty');window.location.href = 'MMS_Issue.aspx';", true);
}
USE [CIMProRPT01]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[MMSIssue_InsertOrUpdate]
@INV_TRANS_ID VARCHAR(40)
,@INV_ID VARCHAR(40)
,@INV_LOCATION VARCHAR(40)
,@INV_QTY FLOAT
,@INV_TRANS_REQUESTOR VARCHAR(40)
,@INV_TRANS_REFNO VARCHAR(40)
,@INV_TRANS_REMARK VARCHAR(255)
,@INV_REASON_ID VARCHAR(40)
,@INV_REASON_REMARK VARCHAR(255)
,@INV_CREATE_DATE DATETIME
,@INV_CREATE_USER VARCHAR (255)
,@INV_FROMLOC VARCHAR (40)
,@RecordFound INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT * FROM OTH_INV_QTY_LOC WHERE INV_ID = @INV_ID and INV_LOCATION = @INV_LOCATION)
BEGIN
UPDATE OTH_INV_QTY_LOC SET [INV_ID] = @INV_ID,INV_LOCATION = @INV_LOCATION , INV_QTY = INV_QTY - @INV_QTY WHERE INV_ID = @INV_ID AND INV_LOCATION = @INV_LOCATION
INSERT INTO OTH_INV_TRANSACTION (INV_TRANS_ID,INV_ID,INV_TRANS_LOCATION,INV_TRANS_QTY,INV_TRANS_REQUESTOR,INV_TRANS_REFNO,INV_TRANS_REMARK,INV_REASON_ID,INV_REASON_REMARK,INV_CREATE_DATE,INV_CREATE_USER,INV_FROMLOC)VALUES (@INV_TRANS_ID,@INV_ID,@INV_LOCATION,@INV_QTY,@INV_TRANS_REQUESTOR,@INV_TRANS_REFNO,@INV_TRANS_REMARK,@INV_REASON_ID,@INV_REASON_REMARK,@INV_CREATE_DATE,@INV_CREATE_USER,@INV_FROMLOC)
DECLARE @InvFindQTY FLOAT SET @InvFindQTY = ( SELECT INV_QTY FROM OTH_INV_QTY_LOC)
IF @InvFindQTY >= @INV_QTY
BEGIN
SELECT @RecordFound = 2
END
ELSE
BEGIN
SELECT @RecordFound = 1
END
END
ELSE
BEGIN
SELECT @RecordFound = 0
END
END
You cannot pause initiated transaction, you need to split up your procedure into two parts and call the second after your popup or whatever condition you feel like.
If you want to rollback transaction you an use BEGIN TRAN and COMMIT/ROLLBACK respectively. .NET Transactions can also be used for rollback.
You wrote when inv_qty == 2
but your procedure returns @RecordFound
and based on my understanding of the sql everything is fine if @InvFindQTY >= @INV_QTY
and the proc returns @RecordFound = 2
@INV_QTY
is passed to the procedure inv_qty
of the table oth_inv_qty_loc
is updated inv_qty = inv_qty - @INV_QTY
if the condition inv_id = @INV_ID AND inv_location = @INV_LOCATION
is given @InvFindQTY = (SELECT inv_qty FROM oth_inv_qty_loc)
is retrieved after the update @RecordFound
is set depending on @InvFindQTY >= @INV_QTY
So basically it seems that you can wrap the statement in a transaction and update the table only if inv_qty >= @INV_QTY
which means if the current stock is greater or equal the amount @INV_QTY
which someone wants to take out of the stock.
ALTER PROCEDURE [dbo].[MMSIssue_InsertOrUpdate]
@INV_TRANS_ID VARCHAR(40)
,@INV_ID VARCHAR(40)
,@INV_LOCATION VARCHAR(40)
,@INV_QTY FLOAT
,@INV_TRANS_REQUESTOR VARCHAR(40)
,@INV_TRANS_REFNO VARCHAR(40)
,@INV_TRANS_REMARK VARCHAR(255)
,@INV_REASON_ID VARCHAR(40)
,@INV_REASON_REMARK VARCHAR(255)
,@INV_CREATE_DATE DATETIME
,@INV_CREATE_USER VARCHAR (255)
,@INV_FROMLOC VARCHAR (40)
,@RecordFound INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT *
FROM OTH_INV_QTY_LOC
WHERE INV_ID = @INV_ID
AND INV_LOCATION = @INV_LOCATION)
BEGIN
BEGIN TRY
-- start the transaction
BEGIN TRANSACTION
UPDATE OTH_INV_QTY_LOC
SET [INV_ID] = @INV_ID,INV_LOCATION = @INV_LOCATION , INV_QTY = INV_QTY - @INV_QTY
WHERE INV_ID = @INV_ID AND INV_LOCATION = @INV_LOCATION
-- you can start the transaction from here
-- BEGIN TRANSACTION
INSERT INTO OTH_INV_TRANSACTION (INV_TRANS_ID,INV_ID,INV_TRANS_LOCATION,INV_TRANS_QTY,INV_TRANS_REQUESTOR,INV_TRANS_REFNO,INV_TRANS_REMARK,INV_REASON_ID,INV_REASON_REMARK,INV_CREATE_DATE,INV_CREATE_USER,INV_FROMLOC)
VALUES (@INV_TRANS_ID,@INV_ID,@INV_LOCATION,@INV_QTY,@INV_TRANS_REQUESTOR,@INV_TRANS_REFNO,@INV_TRANS_REMARK,@INV_REASON_ID,@INV_REASON_REMARK,@INV_CREATE_DATE,@INV_CREATE_USER,@INV_FROMLOC)
DECLARE @InvFindQTY FLOAT
SET @InvFindQTY = ( SELECT INV_QTY FROM OTH_INV_QTY_LOC)
IF @InvFindQTY >= @INV_QTY
BEGIN
SELECT @RecordFound = 2;
THROW 51000, '@RecordFound = 2', 1;
END
ELSE
BEGIN
SELECT @RecordFound = 1
END
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
END CATCH
-- Commit the transaction
IF XACT_STATE() = 1 COMMIT TRANSACTION
-- For safety
ELSE IF XACT_STATE() = -1 ROLLBACK TRANSACTION
END
ELSE
BEGIN
SELECT @RecordFound = 0
END
END
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.