Sql Server BCP(Bulk Copy Process) for Save file in csv format through stored procedure

Hi Techies,
           Today i am going to provide you a solutions for copy data from database and save that data in csv or any type of format in same or another location. 

So, For this type of work i used to BCP ( Bulk copy process ) using "xp_cmdshell" command in sql server. 

For this process we need Admin rights on SQL Server and xp_cmdshell access rights. 

After taking rights we have to write bellow query in stored procedure. 



--------------------------------------------------------------------------------------------------------------
--EXEC ExportTest
Create Proc ExportTest
AS
BEGIN    
    declare @cmd varchar(1000)
    set @cmd ='bcp "SELECT * FROM databasename.[dbo].[tablename]" queryout "C:\\FileTest\test.csv" -c -t , 
-S 10.XXXXXXXX -U username-P Password-T'
   exec xp_cmdshell @cmd
END

--------------------------------------------------------------------------------------------------------------

** You can use : \\machine name\Users\Desktop\ .... for shared location for different server.
** For Same Sytem : C:\\FileTest\test.csv  .. Simply use folder location.
** -S 10.XXXXXXXX -U username-P Password-T':  If you are using network server then use -S Database ServerName, -U database Username , -P database passwrod
** For local Database : Simply use (local) instead of ip and username and password.

If you have any query kindly write on comment box.
Thanks

Sql Server BCP(Bulk Copy Process) for Save file in csv format through stored procedure Sql Server BCP(Bulk Copy Process) for Save file in csv format  through stored procedure Reviewed by Ashwani on September 10, 2019 Rating: 5

No comments:

Powered by Blogger.