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
 bcp command...help me

Author  Topic 

gmahi
Starting Member

9 Posts

Posted - 2011-07-24 : 00:04:42
HI,
I have to pass location and sql query as parameters on to bcp command so that i can get my results for my work like


set @name =( query to get stored procedure name)
set @filepath =(query to get file path)
Set @command = 'bcp "Exec @name" queryout "@filepath " -c -t, -T -Sserver1'
Exec master..xp_cmdshell @command

am getting errors by following the above scenario , please help me in getting the results by passing (@filepath and @query) parameters in bcp
or
is there any other way to achieve it. If please tell me


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-24 : 00:51:15
try like this

set @name =( query to get stored procedure name)
set @filepath =(query to get file path)
Set @command = 'bcp "Exec @name" queryout "' + @filepath +'" -c -t, -T -Sserver1'
Exec master..xp_cmdshell @command




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

Go to Top of Page

gmahi
Starting Member

9 Posts

Posted - 2011-07-24 : 08:20:39
Thnaks visakkh. Its working:)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-24 : 08:29:51
welcome

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

Go to Top of Page

gmahi
Starting Member

9 Posts

Posted - 2011-07-25 : 07:00:21
hi am able to transfer the data to a excel file using above command but the cloumn fields are not exporting, is there any way to achieve it usin bcp command
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-25 : 11:08:06
you mean get the column headers also?

see this

http://weblogs.sqlteam.com/mladenp/archive/2006/07/25/10771.aspx

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

Go to Top of Page

gmahi
Starting Member

9 Posts

Posted - 2011-07-25 : 11:52:56
Yes please
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-25 : 12:06:12
check the link it has a method

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

Go to Top of Page
   

- Advertisement -