Sample Header Ad - 728x90

Nest a SQL file into another in SQL Server

3 votes
2 answers
4669 views
I have a simple SQL file I'm using to perform some test:
`
DECLARE @VAR1 AS VARCHAR(100) = '12345'
DECLARE @VAR2 AS INTEGER      = 54321

WAITFOR DELAY '00:00:02'

INSERT Orders SELECT @VAR1 AS COL1, @VAR2 AS COL2, ...
...
` Before launching all that, I would like to do some cleaning:
`
/* Clean SpecialSubSubEntries */
DELETE SpecialSubSubEntries
  WHERE Id IN (SELECT Id FROM SubSubEntries
                 WHERE SubEntryId IN (SELECT Id FROM SubEntries
                                        WHERE EntryId IN (SELECT Id FROM Entries
                                                            WHERE Name='ENTRY01')
                                      )
               )

DELETE SubSubEntries
  WHERE SubEntryId IN (SELECT Id FROM SubEntries
                         WHERE EntryId IN (SELECT Id FROM Entries
                                             WHERE Name='ENTRY01')
                      )
...
` For readability reasons, I'd like to put all the cleaning commands in a file "cleanup.sql" and launch it at the beginning of my SQL-file. How do I do something like that? I guess it's something like: EXECUTE ".\cleanup.sql" ... but I'm not sure about the syntax.
Asked by Dominique (609 rep)
Jun 30, 2022, 07:46 AM
Last activity: Jun 7, 2023, 08:09 PM