Solved

Uisng bcp utility to copy data from sql to file and vice versa

Posted on 2004-09-15
17
491 Views
Last Modified: 2007-12-19
Hi guys, hope you all are doing fine, guys I am having a trouble I am using the bcp utility to copy the data from one table to a text file but I just wanted to give two date parameters in that so that the data withing those date parameters should come and should be copied to the text file but I am not bale to get the syntax right. ia m using the below mentioned command


exec master..xp_cmdshell 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and '+@dt2 '" queryout "c:\test.txt" -c -q -U"username" -P"password"'

I have declared the @dt1 and @dt2 parameters.

Please help me out urgently.
0
Comment
Question by:faisal_n_k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
17 Comments
 
LVL 11

Expert Comment

by:ram2098
ID: 12062303
It is always good to write it like this

declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and '+@dt2 ' + ' " queryout "c:\test.txt" -c -q -U"username" -P"password"'


exec master..xp_cmdshell @sqlquery
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12062316
I guess one more syntax error....

declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and ' + @dt2  + ' " queryout "c:\test.txt" -c -q -U"username" -P"password"'


exec master..xp_cmdshell @sqlquery
0
 

Author Comment

by:faisal_n_k
ID: 12062394
Thanks ram for the comments but I ran the command it is giving me an error. The command that I ran is below


declare @dt1 as datetime, @dt2 as datetime
Set @dt1 = '2004/08/01'
Set @dt2 = '2004/08/10'
declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and ' + @dt2  + ' " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'


exec master..xp_cmdshell @sqlquery



It gives me an error like

Server: Msg 241, Level 16, State 1, Line 6
Syntax error converting datetime from character string.

Thanks for helping
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 11

Assisted Solution

by:ram2098
ram2098 earned 30 total points
ID: 12062428
Declare both of them as varchar..I guess that will work

declare @dt1 as varchar(30), @dt2 as varchar(30)

Set @dt1 = '2004/08/01'
Set @dt2 = '2004/08/10'
declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and ' + @dt2  + ' " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'


exec master..xp_cmdshell @sqlquery
0
 

Author Comment

by:faisal_n_k
ID: 12062491
But ram it is giving me ) rows copied whereas the data is there
0
 

Author Comment

by:faisal_n_k
ID: 12062508
0 rows copied and neither it si giving me any error
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12062566
Can you try the following..........to avoid any date formatting issues


declare @dt1 as varchar(30), @dt2 as varchar(30)

Set @dt1 = '2004/08/01'
Set @dt2 = '2004/08/10'
declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+cast(varchar(30),@dt1,101)+' and ' + cast(varchar(30),@dt2,101)  + ' " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'


exec master..xp_cmdshell @sqlquery
0
 

Author Comment

by:faisal_n_k
ID: 12062579
ram, with cast it is giving that varchar is not a recognized function and when I change cast to convert it is still saying 0 rows copied.
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12062588
Sorry..I gave you wrong syntax..

declare @dt1 as varchar(30), @dt2 as varchar(30)

Set @dt1 = '08/01/2004'
Set @dt2 = '08/10/2004'
declare @sqlquery varchar(8000)

select @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between '+@dt1+' and ' + @dt2  + ' " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'


exec master..xp_cmdshell @sqlquery
0
 

Author Comment

by:faisal_n_k
ID: 12062600
still 0 rows copied my dear friend, please help me out with this
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12062634
Are you getting any rows if you execute the following query..through query analyzer?

select * from Watch.dbo.Dgattendance where logindate between '08/01/2004' and '08/10/2004'

If it is returning rows, I haven't seen anything wrong with the BCP command.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 20 total points
ID: 12063217
I think that ram has been close to the solution, but assumes that the field LOGINDATE is indeed stored as datetime. If it is NOT (but as varchar), then the solution will be different, please tell us.

declare @dt1 as datetime, @dt2 as datetime
Set @dt1 = '2004/08/01'
Set @dt2 = '2004/08/10'
declare @sqlquery varchar(8000)

set @sqlquery = 'bcp "select * from Watch.dbo.Dgattendance where logindate between CONVERT(datetime, '''+ CONVERT( varchar(20), @dt1 , 101 ) +''', 101) and CONVERT(datetime, ''' + CONVERT(varchar(20), @dt2 , 101 ) + ''', 101) " queryout "c:\test.txt" -c -q -U"sa" -P"heesa"'
EXEC ( @sqlquery )

This should work for ANY regional setting you have.

CHeers
0
 

Author Comment

by:faisal_n_k
ID: 12071894
Thanks angel you have been really an angel for me thanks again for the help it has worked but ram has also helped me out but I am confused for the points whom should I give the points
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12072230
You can actually split the points between both of us....

Nice to see your problem solved...:)
0
 

Author Comment

by:faisal_n_k
ID: 12072307
How do we split the points
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12073023
see the link below..it helps you on this........

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/help.jsp#hi19
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question