Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 xp_cmdshell

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-02-22 : 03:36:28
how can i add in the file name together with the date?

TableName_20130221

Below query seems not working:

declare @Date varchar(max),@path varchar(max)
set @Date = replace(convert(varchar, getdate()-1,111),'/','')
EXEC master..xp_cmdshell'bcp "select * from ##A" queryout "\\...\TableName_+''@Date''.txt" -c -t"|" -T -S'

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-02-22 : 04:36:58

declare @sql varchar(4000)
declare @Date varchar(max)
set @Date = replace(convert(varchar, getdate()-1,111),'/','')
set @sql = 'bcp "SELECT * FROM DBNAME.dbo.TableNAme" queryout "D:\temp\'+ @date +'.txt" -c -t"|" -T -S'
EXEC master..xp_cmdshell @sql


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-23 : 03:29:59
also to run this you need to have xp_cmdshell enabled

EXEC sp_configure 'show advanced options', 1

GO

RECONFIGURE

GO
EXEC sp_configure 'xp_cmdshell'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-02-24 : 05:40:49
Thanks!
Is working fine now..

How can I add in the column name above?

FirstName|LastName|MiddleName
Mary|Ann|Joseph
Henry|John|Johnson
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-24 : 08:15:57
BCP does not have an option that lets you specify that column headers be included in the output. There are workarounds that people use such as creating a view that includes the header etc., that you will find if you google for it. None of those are really clean solutions though.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-24 : 23:20:59
quote:
Originally posted by peace

Thanks!
Is working fine now..

How can I add in the column name above?

FirstName|LastName|MiddleName
Mary|Ann|Joseph
Henry|John|Johnson


see

http://weblogs.sqlteam.com/brettk/archive/2005/04/13/4395.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -