in Education by
I'm not advanced programmer in SQL and maybe my question is silly, but I haven't found an answer in google. We have some SQL construction for implementing packages of changes: ... BEGIN TRY BEGIN TRANSACTION; ... COMMIT TRANSACTION; END TRY BEGIN CATCH ... END CATCH; ... How can I put the chain of CREATE TRIGGER blocks instead of without errors: -- Table1 CREATE TRIGGER trTable1_Dates ON dbo.Table1 AFTER INSERT, UPDATE AS BEGIN SET NOCOUNT ON; ... SET NOCOUNT OFF; END GO ... -- TableN CREATE TRIGGER trTableN_Dates ON dbo.TableN AFTER INSERT, UPDATE AS BEGIN SET NOCOUNT ON; ... SET NOCOUNT OFF; END GO The purpose is to create all triggers or nothing and print message in CATCH block of code if fails. Edited The errors are: On first trigger's BEGIN: SQL80001: Incorrect syntax near 'BEGIN'. Expecting EXTERNAL. After first trigger, on GO: SQL80001: Incorrect syntax near 'GO'. END TRY: SQL80001: Incorrect syntax near 'TRY'. Expecting CONVERSATION. END CATCH: SQL80001: Incorrect syntax near 'CATCH'. Expecting CONVERSATION. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You need to run each create table statement in a separate scope/batch (because it has to be the first statement in the batch). So you'll have to escape any quotes in the trigger definitions too: BEGIN TRY BEGIN TRANSACTION; exec sp_executesql N'CREATE TRIGGER trTable1_Dates ON dbo.Table1 AFTER INSERT, UPDATE AS BEGIN SET NOCOUNT ON; //An empty string in here has to be '''' to escape the quotes SET NOCOUNT OFF; END' exec sp_executesql N'CREATE TRIGGER trTableN_Dates ON dbo.TableN AFTER INSERT, UPDATE AS BEGIN SET NOCOUNT ON; ... SET NOCOUNT OFF; END' COMMIT TRANSACTION; END TRY BEGIN CATCH ... END CATCH; Transactions are orthogonal to batches and nested scopes, so the transaction covers all activity that occurs inside each EXEC too.

Related questions

0 votes
    What is the use of try & catch? (a) It allows us to manually handle the exception (b) It allows ... section Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    What is the use of try & catch? (a) It allows us to manually handle the exception (b) It allows ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    This question already has answers here: Can I catch multiple Java exceptions in the same catch clause? (10 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    Which of these keywords must be used to handle the exception thrown by try block in some rational manner? (a ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    A single try block must be followed by which of these? (a) finally (b) catch (c) finally & catch ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these keywords are used for the block to handle the exceptions generated by try block? (a) try ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    A single try block must be followed by which of these? (a) finally (b) catch (c) finally & ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    How would you put your graduate degree on your signature block?...
asked Jan 27, 2021 in General by Editorial Staff
0 votes
    Block rewards are given for every successful transaction verification in the network. 1. True 2. False...
asked Oct 26, 2020 in Technology by JackTerrance
0 votes
    I'm beeing bit confused here. SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.bugs_list_item, ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 16, 2022 in Education by JackTerrance
...