我想将查询结果转换为excel文件,并将该文件存储为表格中的varbinary。可以通过访问如下所示的本地路径来完成。
DECLARE @cmd VARCHAR(8000), @export_directory VARCHAR(300) = 'D:\', @filename VARCHAR(300) = ''
CREATE TABLE ##temp_data(value VARCHAR(100))
INSERT ##temp_data
SELECT 'TEST'
SELECT @filename = 'excelfile.csv'
SELECT @cmd = 'bcp "SELECT * FROM ##temp_data " queryout "#OUTDIR#\#OUTFILE#" -q -S "#SERVER#" -c -t "," -T'
SET @cmd = REPLACE(@cmd, '#DB#', DB_NAME());
SET @cmd = REPLACE(@cmd, '#OUTDIR#', @export_directory);
SET @cmd = REPLACE(@cmd, '#OUTFILE#', @filename);
SET @cmd = REPLACE(@cmd, '#SERVER#', @@SERVERNAME);
EXECUTE master..xp_cmdshell @cmd,no_output
GO
-----------------------------
CREATE TABLE ##VarbinaryExample (id int, DocData VARBINARY(MAX))
INSERT INTO ##VarbinaryExample
SELECT 1, bulkcolumn
FROM OPENROWSET( BULK 'D:\excelfile.csv', SINGLE_BLOB ) AS y
-----------------------------
GO
SELECT*FROM ##VarbinaryExample
GO
但是,如果不将文件导出到本地路径,是否可能?