Avatar of steven
steven
Flag for United States of America asked on

bcp query out

Is it possible to use the bcp command to query out the below

select co.co_num as 'W/O',
co.co_Line as Line,
cu.name as 'Customer Name',
i.product_code as 'Material',
c.order_date,
LTRIM(RTRIM(REPLACE(SUBSTRING(udfvcld1,0,CHARINDEX('X',udfvcld1,0)),'"',''))) od,
case when u.udfvcld1 like '%dia%'Then NULL else  LTRIM(RTRIM(REPLACE(SUBSTRING(udfvcld1,CHARINDEX('X',udfvcld1,0)+1,CHARINDEX('X',udfvcld1,CHARINDEX('X',udfvcld1,0)+1) - CHARINDEX('X',udfvcld1,0) -1),'"','')))  end as id,
LTRIM(RTRIM(REPLACE(SUBSTRING(udfvcld1,CHARINDEX('X',udfvcld1,CHARINDEX('X',udfvcld1,0)+1)+1,LEN(udfvcld1)),'"',''))) length,
co.qty_ordered as qty,
case when c.type = 'B' then 'Yes' else 'NO' end as blanket,
case when ship_partial = 1 then 'YES' else 'NO' end as partial,
co.due_date  --select *
from  coitem_mst co
      inner join co_mst c on c.co_num = co.co_num
      inner join item_mst i on i.item = co.item
      inner join custaddr_mst cu on cu.cust_num = c.cust_num            
                                                       and cu.cust_seq =c.cust_Seq
            left join UserDefinedFields u on u.RowId = i.rowpointer
where convert(nvarchar(10),co.createdate,101) = convert(nvarchar(10),getdate(),101)
Microsoft SQL Server

Avatar of undefined
Last Comment
Pawan Kumar

8/22/2022 - Mon
Pawan Kumar

Please try this BCP queryout code-

[b]Solution[/b]

[code]

SET NOCOUNT ON;
       
DECLARE 
     @filename varchar(150),
     @query varchar(5000),
     @bcp varchar(5000),
     @year char(4) = '2015',
     @month varchar(7) = 'March',
     @debug bit = 0

SET @Query = ' " select co.co_num as ''W/O'',
co.co_Line as Line,
cu.name as ''Customer Name'',
i.product_code as ''Material'',
c.order_date,
LTRIM(RTRIM(REPLACE(SUBSTRING(udfvcld1,0,CHARINDEX(''X'',udfvcld1,0)),''"'',''''))) od,
case when u.udfvcld1 like ''%dia%''Then NULL else  LTRIM(RTRIM(REPLACE(SUBSTRING(udfvcld1,CHARINDEX(''X'',udfvcld1,0)+1,CHARINDEX(''X'',udfvcld1,CHARINDEX(''X'',udfvcld1,0)+1) - CHARINDEX(''X'',udfvcld1,0) -1),''"'','''')))  end as id,
LTRIM(RTRIM(REPLACE(SUBSTRING(udfvcld1,CHARINDEX(''X'',udfvcld1,CHARINDEX(''X'',udfvcld1,0)+1)+1,LEN(udfvcld1)),''"'',''''))) length,
co.qty_ordered as qty,
case when c.type = ''B'' then ''Yes'' else ''NO'' end as blanket,
case when ship_partial = 1 then ''YES'' else ''NO'' end as partial,
co.due_date  --select *
from  coitem_mst co 
      inner join co_mst c on c.co_num = co.co_num 
      inner join item_mst i on i.item = co.item
      inner join custaddr_mst cu on cu.cust_num = c.cust_num            
                                                       and cu.cust_seq =c.cust_Seq
            left join UserDefinedFields u on u.RowId = i.rowpointer
where convert(nvarchar(10),co.createdate,101) = convert(nvarchar(10),getdate(),101) " '
     
SET @filename = '"C:\Pawan\tblName_' + @month + '_' + @year + '.txt" -c -T'

SET @bcp = ' EXEC master..xp_cmdshell '' bcp ' + @query + ' queryout ' + @filename + ''''

EXEC ( @bcp )

SET NOCOUNT OFF;

Open in new window

steven

ASKER
Error....


Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'W'.
Msg 103, Level 15, State 4, Line 6
The identifier that starts with '',''))) od,
case when u.udfvcld1 like '%dia%'Then NULL else  LTRIM(RTRIM(REPLACE(SUBSTRING(udfvcld1,CHARINDEX('X',udfvcld1,0)+1' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 8
The identifier that starts with '',''))) length,
co.qty_ordered as qty,
case when c.type = 'B' then 'Yes' else 'NO' end as blanket,
case when ship_partial = 1' is too long. Maximum length is 128.
Msg 105, Level 15, State 1, Line 19
Unclosed quotation mark after the character string ''.
Pawan Kumar

Checking.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
steven

ASKER
Again  I am in debt to you FINE JOB worked great thanks you very much
Pawan Kumar

Welcome. Happy to help :)