我正在尝试使用SQL Server 2012从select查询中将数据插入到文本文件中。但是我无法帮助我。
代码
EXEC xp_cmdshell 'bcp "Select RecodLine from WorkDataLoad" queryout "D:\Test.txt" -T -SSubhash-Pc -c -t,'
错误
开始复制......
SQLState = S0002,NativeError = 208
错误= [Microsoft] [SQL Server Native Client 11.0] [SQL Server]无效的对象名称'WorkDataLoad'。
SQLState = S1000,NativeError = 0
错误= [Microsoft] [SQL Server Native Client 11.0]无法解析列级排序规则BCP复制失败
答案 0 :(得分:0)
尝试这种方式:)
SET NOCOUNT ON
DECLARE @TestTable TABLE (ID INT IDENTITY(1,1), Data NVARCHAR(1000))
DECLARE
@AllData NVARCHAR(MAX),
@FileDest NVARCHAR(100)
--Replace only the path here for example ' >>C:\myfile.txt'',no_output'
SET @FileDest = ' >>E:\test.txt'',no_output'
SET @AllData = ''
INSERT INTO @TestTable VALUES
('This is my first row'),
('hello from second row')
SELECT @AllData +=
'EXEC xp_cmdshell ''@ECHO '+Data+@FileDest+CHAR(10)
FROM @TestTable
EXEC(@AllData)
答案 1 :(得分:0)
我认为SELECT语句中缺少DataBase鬃毛,请使用如下查询:
Select RecodLine from DatabaseName.SchemaName.WorkDataLoad
或者,您必须提供一个参数来指定数据库名称,如:
EXEC xp_cmdshell 'bcp "Select RecodLine from WorkDataLoad" queryout "D:\Test.txt" -d "DatabaseName" -T -SSubhash-Pc -c -t,'