Home > database >  How to batch execute SQL script, don't interrupt when you make a mistake
How to batch execute SQL script, don't interrupt when you make a mistake

Time:05-08

Reference script:
The execute sp_addextendedproperty 'MS_Description', 'XXXX', 'user', 'dbo', 'table', 'TB1, null, null;
Execute sp_addextendedproperty 'MS_Description', 'XXXX2', 'user', 'dbo', 'table', 'TB2, null, null;

In SSMS execution if the first error, when the second will not be interrupted to perform

Only this can
The execute sp_addextendedproperty 'MS_Description', 'XXXX', 'user', 'dbo', 'table', 'TB1, null, null;
GO
Execute sp_addextendedproperty 'MS_Description', 'XXXX2', 'user', 'dbo', 'table', 'TB2, null, null;

But my statement is SQL patchwork, couldn't GO of line breaks


To a
The execute sp_addextendedproperty 'MS_Description', 'XXXX', 'user', 'dbo', 'table', 'TB1, null, null; GO
Execute sp_addextendedproperty 'MS_Description', 'XXXX2', 'user', 'dbo', 'table', 'TB2, null, null; GO
Message 102, level 15, state 1, line 1
"GO around" grammatical errors,
Message 102, level 15, state 1, line 2
"GO around" grammatical errors,

CodePudding user response:

It is ok to add try catch:
 - 1. 
BEGIN the TRY
The execute sp_addextendedproperty 'MS_Description', 'XXXX', 'user', 'dbo', 'table', 'TB1, null, null;
END the TRY
The BEGIN CATCH
The SELECT ERROR_MESSAGE () AS ErrorMessage
END the CATCH
- 2.
BEGIN the TRY
Execute sp_addextendedproperty 'MS_Description', 'XXXX2', 'user', 'dbo', 'table', 'TB2, null, null;
END the TRY
The BEGIN CATCH
The SELECT ERROR_MESSAGE () AS ErrorMessage
END the CATCH


CodePudding user response:

Use;
(semicolon)?
 

DECLARE @ SQL VARCHAR (1000)

SET @ SQL='execute sp_addextendedproperty' MS_Description ', '
' ' 'receiving time,
'user', 'dbo', 'table', 'BS_Track_Info', 'column', 'RecieveTime';
The execute sp_addextendedproperty 'MS_Description',
' ' 'receiving time,
'user', 'dbo', 'table', 'BS_Track_Info', 'column', 'RecieveTime';
The execute sp_addextendedproperty 'MS_Description',
"' primary key ',
'user', 'dbo', 'table', 'BS_Track_Info', ' 'the column ", "id"'


The exec (@ SQL)
  • Related