I would like to know how I can export data from a select command to a normal text file from MSSQL using the xp_cmdshell bcp method.
I'm using SQL 2012 an my code looks like this :
Declare @filename varchar(50),
@cmd varchar(4000),
@file varchar(100)
set @filename = 'TestMe'
set @file = 'c:\' + @FileName + '.txt'
set @cmd = 'bcp "Select * from TableName" queryout"'
set @cmd = @cmd + @file + '" -T -is'
EXEC xp_cmdshell @cmd
The query executes successfully, but the it gives me an output that says:
bcp: Unable to open input file s: No such file or directory
I even tried creating the file and hardcoding the name, but to no avail. Any help would be greatly appreciated.