bcp'queryout'附近的语法不正确

时间:2017-07-17 08:31:57

标签: sql sql-server tsql

我遇到错误消息“'queryout'附近的语法不正确”。 目标是将数据文件从数据库提取到服务器。问题似乎出现在查询文件中。但我不知道在哪里。

DECLARE @DBName VARCHAR(5000);
DECLARE @period VARCHAR(5000);
DECLARE @SQLEXE VARCHAR(8000);
DECLARE @SearchSchema NVARCHAR(4000);


SET @period = '''2017-01-01 00:00:00'' AND ''2017-12-31 23:59:59'''


SET @DBName = (SELECT name FROM master.dbo.sysdatabases where name LIKE '%NAV%');
EXECUTE ('USE [' + @DBName+']'); Select db_name();

SET @SearchSchema = (SELECT REPLACE(name,'Change Log Setup', 'Change Log Entry') as Name  FROM [Demo Database NAV (10-0)].sys.tables where name LIKE '%$Change Log Setup');
PRINT '@SearchSchema: ' + @SearchSchema;



SET @SQLEXE = 'bcp SELECT [Entry No_]
      ,[Date and Time]
      ,[User ID]
      ,[Table No_]
      ,[Field No_]
      ,[Type of Change]
      ,[Old Value]
      ,[New Value]
      ,[Primary Key]
      ,[Primary Key Field 1 No_]
      ,[Primary Key Field 1 Value]
      ,[Primary Key Field 2 Value]
      ,[Primary Key Field 3 No_]
      ,[Primary Key Field 3 Value]
      ,[Record ID]
  FROM [' + @DBName + '].[dbo].[' + @SearchSchema + ']
  WHERE [Date and Time] BETWEEN '+@period+' queryout C:\Users\Public\Documents\1a_EY_change_log_entry.txt -c-T'
  PRINT 'SQLEXE ' + @SQLEXE;

Exec  (@SQLEXE);

Print @SQLEXE的输出ist:

SQLEXE bcp SELECT [Entry No_]
      ,[Date and Time]
      ,[User ID]
      ,[Table No_]
      ,[Field No_]
      ,[Type of Change]
      ,[Old Value]
      ,[New Value]
      ,[Primary Key]
      ,[Primary Key Field 1 No_]
      ,[Primary Key Field 1 Value]
      ,[Primary Key Field 2 Value]
      ,[Primary Key Field 3 No_]
      ,[Primary Key Field 3 Value]
      ,[Record ID]
  FROM [Demo Database NAV (10-0)].[dbo].[CRONUS International Ltd_$Change Log Entry]
  WHERE [Date and Time] BETWEEN '2017-01-01 00:00:00' AND '2017-12-31 23:59:59' queryout C:\Users\Public\Documents\1a_EY_change_log_entry.txt -c-T

错误讯息:

Msg 102, Level 15, State 1, Line 17
Incorrect syntax near 'queryout'.

如果我将命令改为

EXEC xp_cmdshell @SQLEXE;
GO

结果是:

  

复制方向必须为“in”,“out”或“format”。

usage: bcp {dbtable | query} {in | out | queryout | format} datafile
  [-m maxerrors]            [-f formatfile]          [-e errfile]
  [-F firstrow]             [-L lastrow]             [-b batchsize]
  [-n native type]          [-c character type]      [-w wide character type]
  [-N keep non-text native] [-V file format version] [-q quoted identifier]
  [-C code page specifier]  [-t field terminator]    [-r row terminator]
  [-i inputfile]            [-o outfile]             [-a packetsize]
  [-S server name]          [-U username]            [-P password]
  [-T trusted connection]   [-v version]             [-R regional enable]
  [-k keep null values]     [-E keep identity values]
  [-h "load hints"]         [-x generate xml format file]
  [-d database name]        [-K application intent]  [-l login timeout]

NULL

2 个答案:

答案 0 :(得分:0)

尝试使用:

EXEC xp_cmdshell @SQLEXE;  
GO  

而不是EXEC

BCP实用程序在命令行的上下文中运行。

答案 1 :(得分:0)

解决方案是:  1.使用-S确定所需的服务器(而不是MS SQL正在搜索)  2.所有内容必须写成一行(因为命令行解释)  3.""可用于查询,但不能用于路径