0

I was wondering if it is possible for SQL Server to check a directory for files and run a stored procedure. I did some research and found this, but I am wondering if there is a way to do what I want WITHOUT SSIS.

EDIT: After reading my post, I realized I should have been more specific. Is there a way to AUTOMATICALLY or set SQL Server to check for files in a directory and run a stored procedure?

3

2 Answers 2

1

You can use xp_cmdshell to run file related commands. To get a directly listing:

exec xp_cmdshell 'dir *.csv';  

You can also use bulk insert to load a file from disk into a table and take actions based on the loaded contents.

Normally you'd use the File Watcher Task with SSIS. But you can also use SQL Server Agent to schedule a task for periodic execution, schedule a task with Windows Task Scheduler, or configure a stored procedure to runs at startup with sp_procoption that pauses (using waitfor) between processing times.

Sign up to request clarification or add additional context in comments.

2 Comments

i guess i should be more specific.. is there a way to automatically set sql server to check a directory and run a stored procedure?
Outside of SSIS, you can schedule a script to do that.
0

for SQL2017 + :

sys.dm_os_enumerate_filesystem('C:\', '*')

OR

DECLARE @LatestBackupFile VARCHAR(255)
    SELECT TOP 1 @LatestBackupFile = fl.file_or_directory_name
    FROM sys.dm_os_enumerate_filesystem(@BackupFolder, @FullBackupFilePattern) fl
    WHERE creation_time >= @LastRestoreEnd 
    ORDER BY creation_time DESC

where @BackupFolder - folder, and @FullBackupFilePattern = 'WideWorldImportersbackups*.bak'

see more at https://support.microsoft.com/en-us/topic/kb4046638-add-the-ability-to-disable-or-enable-a-few-new-dmvs-and-dmfs-introduced-in-sql-server-2017-05d709c5-4522-3168-2cb1-f755fddff9e7

and at https://www.brentozar.com/archive/2017/07/sql-server-2017-less-xp_cmdshell/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.