0

我有以下存储过程将数据复制到文本文件:

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF
GO


ALTER Procedure BCP_Inv_Text_File
(  
@FileName varchar(100),  
@DocDate varchar(10)
)  
as  
        Declare @str varchar(1000)  
        Declare @SQL varchar(1000)
        set @SQL = 'Select T0.DocNum, T0.DocDate FROM Demo88..OINV T0 WHERE DocDate = @DocDate'
        set @str='Exec Master..xp_Cmdshell ''bcp "'+@SQL+'" queryout "'+@FileName+'" -T -c'''   
        Exec(@str)  

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

然后我希望能够执行与此类似的过程:

EXEC BCP_Inv_Text_File 'C:\Export\Inv.txt', '8/9/2013'

我收到以下错误:

Error = [Microsoft][SQL Native Client][SQL Server]Must declare the scalar variable "@DocDate".

4

1 回答 1

0

我发现为了在 BCP 语句中为 date 参数获得正确的语法,我不得不添加额外的单引号,如下所示:

    set @SQL = 'Select T0.DocNum, T0.DocDate FROM Demo88.dbo.OINV T0 WHERE T0.DocDate = ''''' + @DocDate + ''''''
    set @str='Exec Master..xp_Cmdshell ''bcp "'+@SQL+'" queryout "'+@FileName+'" -T -c'''   
于 2013-10-16T20:49:04.533 回答