是否有可能将MS SQL结果传输到新的MS Excel文件?

我正在使用下面的查询将SQL结果传输到现有的Excel文件。 是否可以将它们转移到新的excel表单中? 使用SSIS我们可以做到这一点,但是我想知道在SSMS中是否有可能做到这一点。

SP_CONFIGURE 'show advanced options', 1 RECONFIGURE WITH OVERRIDE GO SP_CONFIGURE 'Database Mail XPs', 1 RECONFIGURE WITH OVERRIDE GO EXEC sp_configure 'ad hoc distributed queries', 1 RECONFIGURE GO EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1 INSERT INTO OPENROWSET ('Microsoft.ACE.OLEDB.12.0', 'Excel 8.0;Database=E:\LessThan1300\OutofScope.xlsx;','select * from [Sheet1$]') select * from tboutofscope where InflowDate >= CONVERT(date,getdate()) 

你可以像这样创build你的模板文件的副本:

 DECLARE @sqlscript VARCHAR(4000), @pathtotemplatefile VARCHAR(MAX) = 'E:\LessThan1300\template.xlsx', @pathtonewfile VARCHAR(MAX) = 'E:\LessThan1300\OutofScope.xlsx', @xlsxdatabase varchar(4000) --SET @pathtotemplatefile = 'c:\i\1.xlsx' --SET @pathtonewfile = 'c:\i\2.xlsx' SET @sqlscript='copy ' + @pathtotemplatefile + ' ' + @pathtonewfile EXECUTE master..xp_cmdshell @sqlscript, no_output set @xlsxdatabase = 'Excel 8.0;Database=' + @pathtonewfile + ';' INSERT INTO OPENROWSET ('Microsoft.ACE.OLEDB.12.0', @xlsxdatabase,'select * from [Sheet1$]') select * from tboutofscope where InflowDate >= CONVERT(date,getdate()) 

您可以根据需要dynamic更改新文件的名称,“创build”它,然后用预期的数据填充它。