I just was hoping somebody could double-check my tran logic herein --- basically, i have two identical tables --- @flag defaults to 0, but if @flag = F, i should insert into QHFinal table.. else, i should insert into QH table.
i often times get a little screwed up on the flow control, and the error handling.
(begin tran/begin/end/end/end/com
mit tran)
is this correct?
CREATE PROC procedurename (
@field1...
@field2...
...
@flag char(1)=0
)
AS
SET NOCOUNT ON
BEGIN
/* created for the insertion into QH/QHFinal tables */
BEGIN TRANSACTION
IF(@flag='F')
BEGIN
INSERT INTO database.dbo.QHFinal (
[........bunch of fields.......] )
VALUES (@.........same bunch of fields..... )
SELECT SCOPE_IDENTITY() AS QHID
IF @@ERROR <> 0
BEGIN
RAISERROR('Failed to insert into database.dbo.QHFinal',16,-
1)
RETURN
END
END
ELSE
BEGIN
INSERT INTO database.dbo.QH (
[......bunch of fields....... )
VALUES (@..........same bunch of fields........ )
SELECT SCOPE_IDENTITY() AS QHID
IF @@ERROR <> 0
BEGIN
RAISERROR('Failed to insert into database.dbo.QH',16,-1)
RETURN
END
IF @@ERROR = 0
BEGIN
COMMIT TRANSACTION
END
END
END
Start Free Trial