Solved

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

Posted on 2004-09-15
17
486 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
  • 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 142

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now