I was experiencing the same behavior.
The file was being written, just not in the location I expected.
I had to use the network file path or Universal Naming Convention (UNC)
of the destination folder so the remote SQL Server could write to a location
on my local client machine.
Example Network File Paths (UNC Paths)
\\127.0.0.1\C$\Temp\Test.txt
\\localhost\C$\Temp\Test.txt
\\Full-Device.Name\C$\Temp\Test.txt
Note that C$
points to C:\
This SQL creates and writes 'Hello World' to C:\temp\Test.txt
-- Enabling OLE Automation (If it's already enabled, you don't need to run)
EXECUTE SP_CONFIGURE 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure 'Ole Automation Procedures', 1;
RECONFIGURE WITH OVERRIDE
GO
DECLARE @Auto INT
DECLARE @FileID INT
/* ************************************************************************************************
Use the network address of the file when you want a remote
SQL server to save a file on a client. To obtain the network
address/path of a file use the client IPV4 address or Full Device Name
Examples:
IP Address (nnn.nnn.nnn.nnn) - Find this by running 'ipconfig' on a command line of the destination machine and use IPV4 address
\\IP Address\C$\...\Test.txt
DECLARE @Path varchar(2000) = '\\127.0.0.1\C$\temp\Test.txt' -- This is C:\temp\Test.txt
Full Device Name (FULL-NAME.Of.Machine) - Right click 'This PC' in file explorer on the destination machine and click properties
\\Full Device Name\C$\...\Test.txt
DECLARE @Path varchar(2000) = '\\localhost\C$\temp\Test.txt' -- This is C:\temp\Test.txt
Change 127.0.0.1 to the IPV4 address or 'Full device name' of the client (destination) machine
****************************************************************************************************/
DECLARE @Path varchar(2000) = '\\127.0.0.1\C$\temp\Test.txt' -- This is C:\temp\Test.txt
DECLARE @Text varchar(2000) = 'Hello World'
EXECUTE sp_OACreate 'Scripting.FileSystemObject', @Auto OUT
EXECUTE sp_OAMethod @Auto, 'OpenTextFile', @FileID OUT, @Path, 8, 1
EXECUTE sp_OAMethod @FileID, 'WriteLine', Null, @Text
EXECUTE sp_OAMethod @FileID, 'Close', Null, Null
EXECUTE sp_OADestroy @FileID
EXECUTE sp_OADestroy @Auto
-- Disabling OLE Automation (Run only if it wasn't enabled before)
EXEC sp_configure 'Ole Automation Procedures', 0;
RECONFIGURE WITH OVERRIDE
GO
EXECUTE SP_CONFIGURE 'show advanced options', 0
RECONFIGURE WITH OVERRIDE
GO
Her is a diagram of running a query with local parameters
on a remote SQL server
╔════════════════════════════════════════════════════════╗ ╔════════════════════════════════════════════════════════╗
║ Remote SQL Server Machine ║ ║ Client ║
╠════════════════════════════════════════════════════════╣ ╠════════════════════════════════════════════════════════╣
║ ║ ║ ║
║ ┌──────────────────────────────────────────────┐ ║ ║ ┌──────────────────────────────────────────────┐ ║
║ │ SQL Server | ║ ║ │ SQL Server Management Studio (SSMS) | ║
║ ├──────────────────────────────────────────────┤ ║ ║ ├──────────────────────────────────────────────┤ ║
║ | SQL ... | ║ ║ | SQL ... | ║
║ | EXECUTE @FileID 'WriteLine', Null, @Text ◄───┼────╫─────────╫────┼ All this is happening on the SQL Server | ║
║ | SQL ... │ ▲ | ║ ║ | SQL ... | ║
║ └────────────────────┼─────────────────────┼───┘ ║ ║ └──────────────────────────────────────────────┘ ║
║ | | ║ ║ ║
║ | | ║ ║ Nothing is written to the client file system ║
║ | | ║ ║ ║
║ ┌────────────────────┼─────┐ ┌───────────┼───┐ ║ ║ ┌──────────────────────────┐ ║
║ │ File System | │ │ SSMS | │ ║ ║ │ Client File System │ ║
║ ├────────────────────┼─────┤ ├───────────┼───┤ ║ ║ ├──────────────────────────┤ ║
║ | C │ | | │ | ║ ║ | C | ║
║ | └─ Temp │ | | SQL ... ─┘ | ║ ║ | └─ Temp | ║
║ | └─ Test.txt ◄──┘ | | | ║ ║ | └─ Test.txt | ║
║ └──────────────────────────┘ └───────────────┘ ║ ║ └──────────────────────────┘ ║
╚════════════════════════════════════════════════════════╝ ╚════════════════════════════════════════════════════════╝