将存储过程结果导出为ex​​cel

时间:2014-12-04 11:41:47

标签: excel sql-server-2014 data-dump

类似的问题可能在这个论坛上被问到有关将存储过程结果导出到excel文件的问题,我尝试了很少尝试但文件没有导出到我期望的文件夹,感谢您的帮助

我创建的存储过程

/****** Object:  StoredProcedure [dbo].[Copy_DataDump_SpecificQuery]    Script Date: 10/28/2014 15:59:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROC [dbo].[Copy_DataDump_SpecificQuery]
@SQLScript VARCHAR(MAX)
,@OutPut_Number INT OUTPUT
,@Output_FIleName VARCHAR(MAX) OUTPUT

AS
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
BEGIN   

BEGIN TRY
DECLARE @server Varchar (100)
SET @server = 'MyServerName'
DECLARE @FileName VARCHAR(255)
DECLARE @Date VARCHAR(12)
SELECT @Date = CONVERT(VARCHAR(10),GETDATE(),120)
SET @Output_FIleName = CAST(NEWID() AS VARCHAR(MAX))+ '.xls'


DECLARE @FilePath VARCHAR (4000)
SET @FilePath = '\\ComputerName\Users\MyUserName\Downloads\Exel_File' + @Output_FIleName  ;

IF OBJECT_ID('tempDB..##temp_Query', 'U') IS NOT NULL
    DROP TABLE ##temp_Query;

----------------------------------------------------------------------------------------------
    DECLARE @Query  AS VARCHAR(MAX)
    DECLARE @FROM_Start INT

    SELECT @FROM_Start = CHARINDEX(' FROM ',@SQLScript)

    SELECT @Query = SUBSTRING(@SQLScript,0,@FROM_Start) + ' INTO ##temp_Query FROM ' + SUBSTRING(@SQLScript,@FROM_Start+6,LEN(@SQLScript))
PRINT (@Query)
    Exec(@Query)    
    ------------------------------------------------------------------------------------------------------------------
    DECLARE @dbName VARCHAR (100)
    SET @dbName = 'master'
    DECLARE @sql VARCHAR (5000)

    SET @sql = 'Select * from tempdb.dbo.##temp_Query'

    DECLARE @dbName1 VARCHAR (100)
    IF OBJECT_ID ('tempDB..##TempExport_Query1','U') is not null
    DROP TABLE ##TempExport_Query1
    IF OBJECT_ID ('tempDB..##TempExport_Query2','U') is not null
    DROP TABLE ##TempExport_Query2

    SELECT  @dbName1 = @dbName
    SELECT  @dbName = 'use ' + @dbName + ';'

    DECLARE @columnNames VARCHAR (8000), @columnConvert VARCHAR (8000), @tempSQL VARCHAR (8000)
    SELECT
        @tempSQL = LEFT(@sql, CHARINDEX('from', @sql) - 1) + ' into ##TempExport_Query1 ' +

        SUBSTRING(@sql, CHARINDEX('from', @sql) - 1, LEN(@sql))
    EXEC (@dbName + @tempSQL)

    --SELECT    *
    --FROM tempdb.INFORMATION_SCHEMA.Columns
    --WHERE table_name = '##TempExport_Query1'

    SELECT  @columnNames = COALESCE(@columnNames + ',', '') + '"' + column_name + '"',
        @columnConvert = COALESCE(@columnConvert + ',', '') + 'convert(nvarchar(4000),'
        + '[' + column_name + ']' + CASE
            WHEN data_type IN ('datetime', 'smalldatetime') THEN ',121'
            WHEN data_type IN ('numeric', 'decimal') THEN ',128'
            WHEN data_type IN ('float', 'real', 'money', 'smallmoney') THEN ',2'
            WHEN data_type IN ('datetime', 'smalldatetime') THEN ',120' ELSE ''
        END + ') as ' + '[' + column_name + ']'
    FROM tempdb.INFORMATION_SCHEMA.Columns
    WHERE table_name = '##TempExport_Query1'

-- execute select query to insert data and column names into new temp table        
    SELECT  @sql = 'select ' + @columnNames + 'temp##SortID  into ##TempExport_Query2 from (select ' + @columnConvert + ', 
        ''2'' as temp##SortID         
           from ##TempExport_Query1 union all select ''' + REPLACE(@columnNames, ',', ''', ''') + ''', 
               ''1'') t '        
    EXEC (@sql)

    SET @sql = 'bcp " select * from ##TempExport_Query2  ORDER BY temp##SortID " queryout "' + @FilePath +
    '" -U uno -P uno   -c -T -S ' + @server   

    EXEC master..xp_cmdshell @sql
SET @OutPut_Number = 1

END TRY
BEGIN CATCH
--DECLARE @ErrorDescription VARCHAR(1000) = 'ERROR - ' + ERROR_MESSAGE()
--      RAISERROR(@ErrorDescription,16,1)
    SET @OutPut_Number = 2
    --SET @OutPut_Message = @@ERROR
END CATCH


End

  

参数

DECLARE @OutPut_Number INT;
DECLARE @OutPut_Message VARCHAR(1000);
EXECUTE dbo.Copy_DataDump_SpecificQuery 'SELECT O.ID AS SystemNumber
        ,'' AS ManualNumber
        ,O.Date AS InvoiceDate
        ,OT.ID AS CustomerCode
        ,OT.Name AS CustomerName
        ,I.ID AS ItemID
        ,I.Description AS Item
        ,OL.UnitQty
        ,OL.FreeQty
        ,OL.UnitPrice
        ,OL.GrossValue
        ,NetSaleValue
        ,DueDate = ''
        ,A.ID AS SalesRepCode
        ,ItemCostPrice = OL.UnitPrice
    FROM TxnOrder O
        INNER JOIN TxnOrderLine OL ON O.UID = OL.TxnOrderUID AND O.SiteUID = Ol.TxnOrder_SiteUID
        INNER JOIN Outlet OT ON O.OutletUID = OT.UID
        INNER JOIN Item I ON OL.ItemUID = I.UID
        INNER JOIN Agent A ON O.AgentUID = A.UID
    WHERE O.mpt_TypeEnum = 1
        AND( O.IsPrinted = 1 OR O.mpt_SalesmodelEnum = 2)
        --AND O.Date >= @StartDate AND O.Date <= @EndDate
        --AND (@DistributorUID IS NULL OR O.DistributorUID = @DistributorUID)
        --AND (@AgentUID IS NULL OR O.AgentUID = @AgentUID)'
,@OutPut_Number OUTPUT
,@OutPut_Message OUTPUT
SELECT @OutPut_Message 

执行后,我在SQL输出窗口

上收到以下输出
  

69111E97-BEDE-4BDE-9EBE-C063DB690E9F.xls

我已按照以下屏幕截图

创建了该文件夹的权限

Folder

enter image description here

1 个答案:

答案 0 :(得分:0)

此问题已解决,我通过RDP登录到服务器,并按照以下方式执行了SP,结果显示...

DECLARE @OutPut_Number INT;
DECLARE @OutPut_Message VARCHAR(1000);
EXECUTE dbo.Copy_DataDump_SpecificQuery 'SELECT O.ID AS SystemNumber
        ,O.Date AS InvoiceDate
        ,OT.ID AS CustomerCode
        ,OT.Name AS CustomerName
        ,I.ID AS ItemID
        ,I.Description AS Item
        ,OL.UnitQty
        ,OL.FreeQty
        ,OL.UnitPrice
        ,OL.GrossValue
        ,NetSaleValue
        ,A.ID AS SalesRepCode
        ,ItemCostPrice = OL.UnitPrice
    FROM TxnOrder O
        INNER JOIN TxnOrderLine OL ON O.UID = OL.TxnOrderUID AND O.SiteUID = Ol.TxnOrder_SiteUID
        INNER JOIN Outlet OT ON O.OutletUID = OT.UID
        INNER JOIN Item I ON OL.ItemUID = I.UID
        INNER JOIN Agent A ON O.AgentUID = A.UID
    WHERE O.mpt_TypeEnum = 1
        AND( O.IsPrinted = 1 OR O.mpt_SalesmodelEnum = 2)'
,@OutPut_Number OUTPUT
,@OutPut_Message OUTPUT
SELECT @OutPut_Message 

AND在我的选择查询中,我删除了以下列

'' AS ManualNumber
   DueDate = ''

由于我已获得通过RDP访问服务器的权限,因此我将以下路径替换为

SET @FilePath = '\\ComputerName\Users\MyUserName\Downloads\Exel_File' + @Output_FIleName 
  

C:\ Users \用户Pathuma \下载\ Excel中           SET @FilePath ='\ C:\ Users \ MyUsername \ Downloads \ Excel'+ @Output_FIleName