Solved

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

Posted on 2004-09-15
17
487 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 2 29
Attaching Database Failed ? 3 38
TSQL - How to declare table name 26 28
SQL SERVER 2008 R2 Could not obtain information about Windows NT group/user 5 34
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

808 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