2017-07-17 333 views
-1

我遇到了錯誤消息「queryout'附近的語法不正確'」。 目標是從數據庫中提取數據文件到服務器。問題似乎在查詢文件中。但我不知道在哪裏。bcp'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 '[email protected]+' queryout C:\Users\Public\Documents\1a_EY_change_log_entry.txt -c-T' 
    PRINT 'SQLEXE ' + @SQLEXE; 

Exec (@SQLEXE); 

打印@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

+0

'EXEC'將執行一些SQL代碼,而不運行命令行。 – DavidG

+0

這篇文章是重複https://stackoverflow.com/questions/45073064/problems-with-bcp-output/45073531?noredirect=1#comment77233470_45073531,作者是相同的 – sepupic

+0

可能重複的[bcp輸出問題] (https://stackoverflow.com/questions/45073064/problems-with-bcp-output) – DavidG

回答

0

解決的辦法是: 1.使用-S,以確定你所需要的服務器(而不是MS SQL正在搜索) 2.一切都必須寫在一行(因爲命令行解釋) 3「」可用於查詢但不適用於路徑

0

嘗試使用:

EXEC xp_cmdshell @SQLEXE; 
GO 

而不是EXEC

BCP實用程序的命令行的上下文中運行。

+0

這是他的「起點」,看到上面的鏈接 – sepupic

+0

您好改變的命令不起作用 – WIbadeneralp