2011-08-11 68 views
0
USE [PreMfg] 
GO 
/****** Object: StoredProcedure [dbo].[procAddOrderItem] Script Date: 08/11/2011 10:28:21  ******/ 
SET ANSI_NULLS ON 
SET ANSI_WARNINGS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[procExcelQuotebyItem] 

( 
     @OrderNumber INT 
) 
AS 

BEGIN 

SET NOCOUNT ON 

DECLARE @Cmd varchar(1000) 
DECLARE @fn varchar(500) 
DECLARE @provider varchar(100) 
DECLARE @ExcelString varchar(100) 

-- New File Name to be created 

SET @fn = '"D:\Pre-Manufacturing\Quote by Item.xls"' 

-- FileCopy command string formation 

SELECT @Cmd = 'Copy "D:\Pre-Manufacturing\Quote by Item (Excel) Template.xls" ' + @fn 

-- FielCopy command execution through Shell Command 

EXEC MASTER..XP_CMDSHELL @cmd, NO_OUTPUT 

    SET @provider = 'Microsoft.Jet.OLEDB.4.0' 
    SET @ExcelString = 'Excel 8.0;Database=' + @fn 

-- Executing the OPENROWSET Command for copying the select contents to Excel sheet. 

exec('INSERT INTO OPENROWSET(''' + @provider + ''',''' + @ExcelString + ''',''SELECT [ITEM NUMBER],[PHOTO],[DESCRIPTION],[CASE PACK],[PIECE PRICE],[CASE PRICE],[WT],[CUBE],[OUTSIDE CASE DIMENSIONS],[UPC#],[CASE UPC#] FROM [Sheet1$]'') 
SELECT [ItemNumber],''' + ' ' + ''',[Item_Description],[Casepack],[Unit Price],[Case Price],[Weight],[Cube],[Case Dims],[UPC],[Case UPC] FROM [Order Summery] WHERE [Order #] = ' + @OrderNumber + '') 

SET NOCOUNT OFF 

END 

USE [PreMfg] 
GO 
/****** Object: StoredProcedure [dbo].[procAddOrderItem] Script Date: 08/11/2011 10:28:21 ******/ 
SET ANSI_NULLS ON 
SET ANSI_WARNINGS ON 

GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[procExcelQuotebyItem] 
(
    @OrderNumber INT 
) 
AS 

BEGIN 
SET NOCOUNT ON 


DECLARE @Cmd varchar(1000) 
DECLARE @fn varchar(500) 
DECLARE @provider varchar(100) 
DECLARE @ExcelString varchar(100) 
DECLARE @SendMail varchar(100) 

-- New File Name to be created 
    SET @fn = 'D:\Pre-Manufacturing\QuotebyItem.xls' 


-- FileCopy command string formation 
    SET @Cmd = 'Copy D:\Pre-Manufacturing\QuotebyItemTemplate.xls ' + @fn 

-- FileCopy command execution through Shell Command 
    EXEC MASTER..XP_CMDSHELL @cmd, NO_OUTPUT 

-- Mentioning the excel destination filename 
    SET @provider = 'Microsoft.Jet.OLEDB.4.0' 
    SET @ExcelString = 'Excel 8.0;Database=' + @fn 

    EXEC('INSERT INTO OPENROWSET(''' + @provider + ''',''' + @ExcelString + ''',''SELECT * FROM [Sheet1$A2:K2]'') 
    SELECT [ITEMNUMBER],[ORDER #],[ITEM_DESCRIPTION],[CASEPACK],[UNIT PRICE],[CASE PRICE],[WEIGHT],[CUBE],[CASE DIMS],[UPC],[CASE UPC] FROM [ORDER SUMMERY] WHERE [Order #] = ''' + @OrderNumber + '''') 

    /* Attach the file to an email and send (the sp_send_dbmail won't accept variables in the arguments, so you have to build it with some double quotes first, then exec the whole string*/ 

    EXEC msdb.dbo.sp_send_dbmail 
    @recipients= '[email protected]', 
    @subject = 'Auto-Generated Quote by Item Report', 
    @body = 'Quote by Item Report attached', 
    @body_format = 'HTML', 
    @file_attachments = @fn 

    /*Cleanup*/ 

    SET @Cmd = 'DEL ' + @fn 
    EXEC xp_cmdshell @Cmd, no_output 

    SET NOCOUNT OFF 
END 
+1

問題是什麼?你有什麼嘗試? – Andomar

回答

0

嘗試改變:

SET @ExcelString = 'Excel 8.0;Database=' + @fn

到:

SET @ExcelString = 'Excel 8.0;Data Source=' + @fn +';'

不是100%肯定在後面的分號上,所以試試看有沒有

+0

得到它的工作: – Bruno

0

有時候SQL Server通過導入得到它的內褲,它的可能是不是你的錯。我的代碼有一天工作,但沒有更改沒有下一個。非常令人沮喪!

確保運行此對您有(這不是一個主數據庫命令)出現問題的DB:我隨機嘗試這個

DBCC FREEPROCCACHE

,它似乎重置的東西。

相關問題