Last active
June 7, 2017 15:52
-
-
Save nullbind/44fb13d43bf6e978e2c3d983ea22e41a to your computer and use it in GitHub Desktop.
This is a TSQL template for executing OS commands through SQL Server using OLE Automation Procedures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This is a TSQL template for executing OS commands through SQL Server using OLE Automation Procedures. | |
-- Enable Show Advanced Options | |
sp_configure 'Show Advanced Options',1 | |
RECONFIGURE | |
GO | |
-- Enable OLE Automation Procedures | |
sp_configure 'Ole Automation Procedures',1 | |
RECONFIGURE | |
GO | |
-- Execute Command via OLE and store output in temp file | |
DECLARE @Shell INT | |
DECLARE @Shell2 INT | |
EXEC Sp_oacreate 'wscript.shell', @Shell Output, 5 | |
EXEC Sp_oamethod @shell, 'run' , null, 'cmd.exe /c "echo Hello World > c:\temp\file.txt"' | |
-- Read results | |
DECLARE @libref INT | |
DECLARE @filehandle INT | |
DECLARE @FileContents varchar(8000) | |
EXEC sp_oacreate 'scripting.filesystemobject', @libref out | |
EXEC sp_oamethod @libref, 'opentextfile', @filehandle out, 'c:\temp\file.txt', 1 | |
EXEC sp_oamethod @filehandle, 'readall', @FileContents out | |
SELECT @FileContents | |
GO | |
-- Remove temp result file | |
DECLARE @Shell INT | |
EXEC Sp_oacreate 'wscript.shell', @Shell Output, 5 | |
EXEC Sp_oamethod @Shell, 'run' , null, 'cmd.exe /c "DEL c:\temp\file.txt"' | |
GO | |
-- Disable Show Advanced Options | |
sp_configure 'Show Advanced Options',1 | |
RECONFIGURE | |
GO | |
-- Disable OLE Automation Procedures | |
sp_configure 'Ole Automation Procedures',1 | |
RECONFIGURE | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment