链接服务器“(null)”的OLE DB提供程序“Microsoft.Jet.OLEDB.4.0”报告错误

时间:2011-08-11 17:46:08

标签: sql excel stored-procedures

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= 'user@domain.com', 
    @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

2 个答案:

答案 0 :(得分:0)

尝试更改:

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

为:

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

在尾部分号上不是100%确定,所以在有和没有

的情况下尝试

答案 1 :(得分:0)

有时候,SQL Server会在导入过程中出现问题,而可能不是你的错。我的代码工作了一天但不是下一个没有变化。很沮丧!

确保针对您遇到问题的数据库运行此操作(它不是主数据库命令):

DBCC FREEPROCCACHE

我随机尝试了这个,似乎重置了一些东西。