Я создал следующий сценарий для одного из моих проектов:
SET NOCOUNT ON
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE
--Create the Folder- en filetables.
DECLARE @SQLFolders TABLE ( SQLFolderName VARCHAR(MAX))
DECLARE @SQLFiles TABLE ( SQLFileName VARCHAR(MAX))
DECLARE @MainFolder VARCHAR(MAX)
DECLARE @FileName VARCHAR(MAX)
DECLARE @FolderName VARCHAR(MAX)
DECLARE @SQLStatement VARCHAR(2000)
SET @MainFolder = 'C:\ProjectName\'
--Fill the foldertable and loop through them.
INSERT INTO @SQLFolders VALUES ('CreateScripts\')
INSERT INTO @SQLFolders VALUES ('ChangeScripts\')
DECLARE cFolders CURSOR LOCAL FOR
SELECT [SQLFolderName]
FROM @SQLFolders
OPEN cFolders
FETCH NEXT FROM cFolders INTO @FolderName
WHILE @@FETCH_STATUS = 0
BEGIN
--Fill the file-table and loop through.
SET @SQLStatement = 'dir /b "' + @MainFolder + @FolderName + '*.sql"'
INSERT INTO @SQLFiles
EXECUTE master.dbo.xp_cmdshell @SQLStatement
DECLARE cFiles CURSOR LOCAL FOR
SELECT DISTINCT [SQLFileName]
FROM @SQLFiles
WHERE [SQLFileName] IS NOT NULL AND
[SQLFileName] != 'NULL' AND
[SQLFileName] != 'File Not Found'
ORDER BY [SQLFileName]
OPEN cFiles
FETCH NEXT FROM cFiles INTO @FileName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQLStatement = 'SQLCMD -d hantisdb -i' + @MainFolder + @FolderName + @FileName
EXECUTE master.dbo.xp_cmdshell @SQLStatement
FETCH NEXT FROM cFiles INTO @FileName
END
DELETE FROM @SQLFiles
CLOSE cFiles
DEALLOCATE cFiles
FETCH NEXT FROM cFolders INTO @FolderName
END
CLOSE cFolders
DEALLOCATE cFolders
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 0
RECONFIGURE
EXEC master.dbo.sp_configure 'show advanced options', 0
RECONFIGURE
SET NOCOUNT OFF
Я использую его, чтобы заново создать свою базу данных и каждый день начинать с новой базы данных.Имейте в виду, что файлы будут выполняться в алфавитном порядке!