SQL Server BCP command query with where clause - sql-server

I had the BCP command working until I added the where clause. The use of the single quote around a char value is causing a syntax error. I have tried embedding triple single quotes and also tried using double quotes with success. Any ideas?
Use tmseprd
DECLARE #sql VARCHAR(8000);
SELECT #sql = 'bcp "select studentid from tmseprd.dbo.Feith_Emas_Compare Where status = 'U' and counselor >199 and stage > 200 " queryout "C:\EMAS_Feith\advmove.txt" -c -t, -T -S' + ##Servername;
EXEC master..xp_cmdshell #sql;

You could use '' and space after -S:
Select #sql = 'bcp "select studentid from tmseprd.dbo.Feith_Emas_Compare Where status = ''U'' and counselor >199 and stage > 200 " queryout "C:\EMAS_Feith\advmove.txt" -c -t, -T -S ' + ##Servername;

Related

Export SQL to XML using bcp with where condition

I try to export data to XML with this code, it's normally working fine:
declare #cmd nvarchar(255);
select #cmd = 'bcp "SELECT * from [db].[dbo].[TW_StockReport](0,25,26,4,29,30,19,31) row For XML auto, XMLSCHEMA, root(''node'')" ' +
'queryout "D:\Temp\dbstockandsalereport.xml" -S -T -w -r -t';
exec xp_cmdshell #cmd;
but when I try to add a WHERE condition, it's no longer working:
declare #cmd1 nvarchar(1155);
select #cmd1 = 'bcp "SELECT INVENTLOCATIONID, AgeOfItem AS ''ProductYear'', ISNULL(SUM(Qty), 0) AS ''Unit'', COUNT(c.ItemID) AS ''ProductType''
FROM [db].[dbo].[TW_ItemsNonMovement] c
LEFT JOIN [db].[dbo].[TempInventTable] d ON c.ITEMID = d.ITEMID collate Thai_CI_AS
WHERE d.TW_DEPARTMENTID = ''PMMan'' AND DateDiff <= 120 AND Qty > 0
GROUP BY INVENTLOCATIONID, AgeOfItem ORDER BY INVENTLOCATIONID, AgeOfItem row FOR XML AUTO, XMLSCHEMA, ROOT(''node'') ;"' +
'queryout "D:\Temp\PMMan-120.xml" -S -T -w -r -t';
exec xp_cmdshell #cmd1;
It is better to create a stored procedure (SP) and put there your entire SELECT statement. After that just call that stored procedure in bcp.
Additionally. this way you can always test the SP on its own and make sure it does what is expected of it.
So it will be like follows:
bcp "EXEC yourStoredProcedure;" ...

How do I convert text file to UTF-8 ,"bcp ... queryout -cC65001..." doesn't work

I'm working on a store procedure(MSSQL 2016), trying to output a text file by using xp_cmdShell.
Here's a part of script:
declare #bcpstr varchar (1000);
set #bcpstr = ''
set #bcpstr = 'bcp "select * from [table_name]''" queryout ' + #file_path + 'headonly.txt -t , -cC 65001 -U' + #user+' -P' + #pwd+' -S '+ #server_name
Exec Master ..xp_cmdShell #bcpstr
But what I get is ANSI text file headonly.txt.
Any suggestions? Many thanks!!
As far as I can tell, the switches should be -c -C65001, not -cC65001.
Try again with
set #bcpstr =
'bcp '+
'"select * from [table_name]" '+
'queryout "'+#file_path+'headonly.txt" '+
'-t, '+
'-c '+
'-C65001 '+
'-U '+#user+' '+
'-P '+#pwd+' '+
'-S '+#server_name;
PS: I've added double quotes around the file path to accomodate for spaces in the path, otherwise your statement will fail.

BCP - Data duplication during export command

I have a procedure which is for exporting data from table to .DAT file. There's a job running on it which calls SP every 10 Min. .DAT file contains H= column names, B= table data, T= No of rows processed(.SIG file appends this to .DAT file).
Following is the stored procedure :
CREATE PROCEDURE [dbo].[Test_Export] #count2 VARCHAR(10)
AS
BEGIN
SET NOCOUNT ON;
--Alerts_Balance
DECLARE #bcp_sql VARCHAR(1000)
DECLARE #filename VARCHAR(200), #count1 VARCHAR(10)=20000
SET #filename = 'C:\Test'+'.DAT';
SELECT #bcp_sql = 'bcp "SELECT ''H'',''Name'',''City'',''State'',''Country'' union all SELECT top 10 ''B'', [Name],[City],[State],[Country] FROM Test_Table" queryout ' + #filename + ' -c -t, -T -S '+ ##servername
EXEC master.sys.xp_cmdshell #bcp_sql
SELECT #bcp_sql = 'bcp "SELECT ''T'', '+cast(IIF(cast(#count1 as int)>cast(#count2 as int), #count2+2, #count1+2) as varchar(10))+'" queryout '+ #filename + '.sig -c -t, -T -S '+ ##servername
EXEC master.sys.xp_cmdshell #bcp_sql
SELECT #bcp_sql = 'type ' + #filename + '.sig >> "' + #filename + '"'
EXEC master.sys.xp_cmdshell #bcp_sql
SELECT #bcp_sql = 'copy nul: ' + #filename + '.sig'
EXEC master.sys.xp_cmdshell #bcp_sql
EXEC (#bcp_sql);
END
It was working fine before with proper data format in .dat.
From couple of days, sometimes .dat file contain no "B" (body) data. Sometimes SIG file appends T 2 times to .DAT file on PROD environment. Sometimes it gives correct output also. This issue is coming only on PROD environment
Any idea why it is behaving like this?

how to export sql data to csv using bcp

I use simple code
declare #sql varchar(8000)
select #sql = 'bcp ExcelAnalysis.dbo.ClearDB out c:\csv\comm.txt -c -t, -T -S '+ ##servername
exec master..xp_cmdshell #sql
but this code export all my table, but i need only some fields and conditions
like:
declare #sql varchar(8000)
select #sql = 'bcp
SELECT
,[vl]
,[data]
,[URL]
,[parse]
,[Strata]
,[Id]
FROM [ExcelAnalysis].[dbo].[ClearDB] where [data]> "01.05.2017" and NOT [vl] ="mag"and NOT [vl] ="Maxximo" out c:\csv\status.txt -c -t, -T -S '+ ##servername
exec master..xp_cmdshell #sql
but if i use any fields, the bcd returns message with his syntax.
How do correct?
First Part : Create a view in database and second part to execute statement to get results into CSV.Let me know if you need more help
use [ExcelAnalysis].
go
;
create view [dbo].[vw_ClearDB] as
SELECT
[vl]
,[data]
,[URL]
,[parse]
,[Strata]
,[Id]
FROM [dbo].[ClearDB] where [data]> "01.05.2017" and NOT [vl] ='magand'
NOT [vl] ='Maxximo'
GO
;
declare #sql varchar(8000)
select #sql = 'bcp ExcelAnalysis.dbo.vw_ClearDB out c:\csv\comm.txt -c -t, -T -S '+ ##servername
exec master..xp_cmdshell #sql
Bcp queryout option should be used.
Syntax would be:
SET #sql = 'bcp "SELECT [vl]
,[data]
,[URL]
,[parse]
,[Strata]
,[Id]
FROM [dbo].[ClearDB]
WHERE [data] > ''01.05.2017''
AND NOT [vl] =''mag''
AND NOT [vl] =''Maxximo''"
queryout c:\csv\comm.txt
-c -t, -T -S '+ ##servername + '\' + ##servicename

Create .txt file out from SQL statement

I try to make a SQL statement to create a .txt file for each id that added to database, but this not working. I try this:
DECLARE #FileName varchar(50), #bcpCommand varchar(2000)
SET #FileName = REPLACE('G:\'+CONVERT(char(8),GETDATE(),1)+'.txt','/','-')
SET #bcpCommand = 'bcp "SELECT * FROM dbo.dbTest WHERE (DATEADD(MINUTE,-1420, GETDATE())) < [date];" queryout "'
SET #bcpCommand = #bcpCommand + #FileName + '" -U garth -P pw -c'
PRINT #bcpCommand
and of course I need this result savede in G:/, but this not working for me. My db name are testDB
Did you mean you want to create a bcp file for each database? Simply loop over all the databases, and ensure you specify the database name parameter for your bcp statement so it knows what database to execute the queryout query in. One other thing you will need to fix though, is the filename. You can modify it below to append the db name to the output file, so you do not end up overwriting each results.
DECLARE #FileName varchar(50), #bcpCommand varchar(2000)
declare #x int = 5
declare #y int
declare #dbname varchar(100)
select #y = max(database_id) from sys.databases
while #x <= #y
begin
select #dbname = name from sys.databases where database_id = #x
if #dbname is not null
begin
SET #FileName = REPLACE('G:\'+CONVERT(char(8),GETDATE(),1) +'.txt','/','-')
SET #bcpCommand = 'bcp "SELECT * FROM dbo.dbTest WHERE (DATEADD(MINUTE,-1420, GETDATE())) < [date];" queryout "'
SET #bcpCommand = #bcpCommand + #FileName + '" -U garth -P pw -c -d"' + #dbname + '"'
PRINT #bcpCommand
end
set #x = #x + 1
end
Sample results:
bcp "SELECT * FROM dbo.dbTest WHERE (DATEADD(MINUTE,-1420, GETDATE())) < [date];" queryout "G:\05-16-15.txt" -U garth -P pw -c -d"db1"
bcp "SELECT * FROM dbo.dbTest WHERE (DATEADD(MINUTE,-1420, GETDATE())) < [date];" queryout "G:\05-16-15.txt" -U garth -P pw -c -d"db2"
bcp "SELECT * FROM dbo.dbTest WHERE (DATEADD(MINUTE,-1420, GETDATE())) < [date];" queryout "G:\05-16-15.txt" -U garth -P pw -c -d"db3"
bcp "SELECT * FROM dbo.dbTest WHERE (DATEADD(MINUTE,-1420, GETDATE())) < [date];" queryout "G:\05-16-15.txt" -U garth -P pw -c -d"db4"

Resources