当我在BCP命令中使用“>”大于然后字符“时,此功能不起作用

时间:2019-05-14 10:29:58

标签: sql bcp xp-cmdshell

我有一个有效的存储过程,该存储过程可以基于SQL查询创建文件

运行SP时,这就是我得到的结果

    NULL
    Starting copy...
    SQLState = S1000, NativeError = 0
    Error = [Microsoft][ODBC Driver 13 for SQL Server]Warning: BCP import                 with a format file will convert empty strings in delimited columns to NULL.
    NULL
    21 rows copied.
    Network packet size (bytes): 4096
    Clock Time (ms.) Total     : 1      Average : (21000.00 rows per sec.)
    NULL

文件创建成功

现在,我想更改SP,唯一要做的就是将=符号改为>符号

从这里

     where art.bitInactivo=0  and datediff(day,art.Dtmalteracao, getdate())=5

对此

    where art.bitInactivo=0  and datediff(day,art.Dtmalteracao, getdate())>5    

甚至与此

    where art.bitInactivo=0  and datediff(day,art.Dtmalteracao, getdate())<5  

现在它在输出中为NULL

我得出的结论是,小于和大于符号必须对BCP命令本身起作用。

但是我用Google搜索,没有人抱怨这一点,只是转义了单引号,仅此而已。

整行在这里。符号在where子句的结尾附近

    SET @bcpCommand = 'bcp "select ''192.168.1.60'',1,art.strCodigo,left(replace(replace(replace(art.strDescricao,''\"'',''''),char(147),''''),char(148),''''),40),replace(art.CA_NomeCient,char(0),''''),''0,00'',''0,00'',''0,00'',''0,00'',''0,00'',art.strAbrevMedVnd,''1'',artfam.strCodFamilia,art.strCodBarras,null,art.ca_exportabalanca,0,0,0,0,0,art.ca_etiquetadef,art.CA_numeradorbalanca,0,0,0,''0,000'',''0,00'',replace(isnull(ca_qtddef,0.00),''.'','',''),''0,000'',''0,000'',''0,000'',''0,00'' from Emp_002.dbo.Tbl_Gce_Artigos art with(NOLOCK) left outer JOIN Emp_002.dbo.Tbl_Gce_ArtigosFamilias artfam with(NOLOCK) on artfam.strCodArtigo=art.strCodigo and artfam.strCodTpNivel=''1'' where art.bitInactivo=0  and datediff(day,art.Dtmalteracao, getdate())>5  " queryout '

如果您需要完整的SP,则为

    USE [Emp_002]
    GO
    /****** Object:  StoredProcedure [dbo].[usr_geraficheiro]    Script Date: 14/05/2019 10:08:45 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[usr_geraficheiro]
    AS
    declare @prevAdvancedOptions int
    declare @prevXpCmdshell int

    select @prevAdvancedOptions = cast(value_in_use as int) from sys.configurations where name = 'show advanced options'
    select @prevXpCmdshell = cast(value_in_use as int) from sys.configurations where name = 'xp_cmdshell'

    if (@prevAdvancedOptions = 0)
    begin
        exec sp_configure 'show advanced options', 1
        reconfigure
    end

    if (@prevXpCmdshell = 0)
    begin
        exec sp_configure 'xp_cmdshell', 1
        reconfigure
    end

    /* do work */
    DECLARE @OutputFile NVARCHAR(100) ,    @FilePath NVARCHAR(100) ,    @bcpCommand NVARCHAR(1500)

    SET @bcpCommand = 'bcp "select ''192.168.1.60'',1,art.strCodigo,left(replace(replace(replace(art.strDescricao,''\"'',''''),char(147),''''),char(148),''''),40),replace(art.CA_NomeCient,char(0),''''),''0,00'',''0,00'',''0,00'',''0,00'',''0,        00'',art.strAbrevMedVnd,''1'',artfam.strCodFamilia,art.strCodBarras,null,art.ca_exportabalanca,0,0,0,0,0,art.ca_etiquetadef,art.CA_numeradorbalanca,0,0,0,''0,000'',''0,00'',replace(isnull(ca_qtddef,0.00),''.'','',''),''0,000'',''0,000'',''0        ,000'',''0,00'' from Emp_002.dbo.Tbl_Gce_Artigos art with(NOLOCK) left outer JOIN Emp_002.dbo.Tbl_Gce_ArtigosFamilias artfam with(NOLOCK) on artfam.strCodArtigo=art.strCodigo and artfam.strCodTpNivel=''1'' where art.bitInactivo=0  and         datediff(day,art.Dtmalteracao, getdate())>5  " queryout '
    SET @FilePath = 'C:\compussql\ExportBalanca\'
    SET @OutputFile = 'artigos.txt'
    SET @bcpCommand = @bcpCommand + @FilePath + @OutputFile + ' -c -C 65001 -t; -T -S '+ @@servername
    exec master..xp_cmdshell @bcpCommand

    SET @bcpCommand = 'bcp "Select ''D1'' " queryout '
    SET @FilePath = 'C:\compussql\ExportBalanca\'
    SET @OutputFile = 'BMLink.ctl'
    SET @bcpCommand = @bcpCommand + @FilePath + @OutputFile + ' -c -C 65001 -t; -T -S '+ @@servername
    exec master..xp_cmdshell @bcpCommand

    SET @bcpCommand = 'bcp "Select ''192.168.1.60'', 1, art.CA_numeradorbalanca, null, art.ca_zonacap, null from Emp_002.dbo.Tbl_Gce_Artigos art WITH(NOLOCK) where art.bitInactivo=0 and datediff(DAY,dtmAlteracao, getdate())<5" queryout '
    SET @FilePath = 'C:\compussql\ExportBalanca\'
    SET @OutputFile = 'ingredientes.txt'
    SET @bcpCommand = @bcpCommand + @FilePath + @OutputFile + ' -c -C 65001 -t; -T -S '+ @@servername
    exec master..xp_cmdshell @bcpCommand





    if (@prevXpCmdshell = 0)
    begin
        exec sp_configure 'xp_cmdshell', 0
        reconfigure
    end

    if (@prevAdvancedOptions = 0)
    begin
        exec sp_configure 'show advanced options', 0
        reconfigure
    end

0 个答案:

没有答案