I have a file on a remote machine that I want to 'copy' to the SQL Server host machine to a specific area. I'm wondering what my options are for doing this through a SQL Server stored procedure, and more specifically I want to stream the file contents, not reading the entire contents into memory first.
I'm basically looking for a streamed version of this:
CREATE PROCEDURE [dbo].[SaveFile]
(@filename nvarchar(255), @contents nvarchar(max))
AS
BEGIN
DECLARE @ObjectToken INT
EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT
EXEC sp_OASetProperty @ObjectToken, 'Type', 1
EXEC sp_OAMethod @ObjectToken, 'Open'
EXEC sp_OAMethod @ObjectToken, 'WriteText', NULL, @contents
EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, 'C:\\Destination\\' + @filename, 2
EXEC sp_OAMethod @ObjectToken, 'Close'
EXEC sp_OADestroy @ObjectToken
END
- Can I have a
filestream
or something equivalent as a stored procedure input parameter? - Have a CLR stored procedure handle the stream?
- Perhaps one procedure could open the stream and return the
@ObjectToken
, another couldWriteText
a chunk from the stream, and a third procedure couldSaveToFile
andClose
it, but this seems like!a.GoodIdea
and I'd have to ensure closure and destruction through some sort of timeout. - Other options?
It's being sourced from a C# console application and I'd prefer to stream these large files instead of something like File.ReadAllText()
, loading a multi-gigabyte string into a variable, and then calling a stored procedure with the entire contents of the file.