February 22, 2017 at 6:24 am
hi,
-- i want to use throw iin following way , and it is working but the problem is it showing one red mark below throw.
because this is written in followning link "The statement before the THROW statement must be followed by the semicolon (;) statement terminator."
i would like to known why it has been wirtten and how to use it in following case.
https://msdn.microsoft.com/en-us/library/ee677615.aspx
begin try
begin tran
if (1=2)
begin
select 1/0
end
else
begin
THROW 51000, 'The record does not exist.', 1;
end
commit tran
end try
begin catch
select ERROR_MESSAGE()
end catch
yours sincelrey
February 22, 2017 at 6:33 am
It works as written, and is correct (except for the missing commit tran/rollback tran). In this case the SSMS intellisense is incorrectly flagging a lack of a ;
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 22, 2017 at 6:47 am
GilaMonster - Wednesday, February 22, 2017 6:33 AMIt works as written, and is correct (except for the missing commit tran/rollback tran). In this case the SSMS intellisense is incorrectly flagging a lack of a ;
i forgot to write rollback,( but i am taking following way of useing throw is correct)
begin try
begin tran
if (1=2)
begin
select 1/0
end
else
begin
THROW 51000, 'The record does not exist.', 1;
end
commit tran
end try
begin catch
rollback tran
select ERROR_MESSAGE()
end catch
February 22, 2017 at 7:31 am
Here's the code again, formatted for readability:
BEGIN TRY
BEGIN TRAN;
IF (
1 = 2
)
BEGIN
SELECT 1 / 0;
END;
ELSE
BEGIN
THROW 51000, 'The record does not exist.', 1;
END;
COMMIT TRAN;
END TRY
BEGIN CATCH
ROLLBACK TRAN;
SELECT ERROR_MESSAGE();
END CATCH;
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply