sql server - Using BCP in a SQL query -


i have stored procedure updates several columns in table , selects out columns can save them out report run. doing exporting , saving manually though setting column size in 'query options' 333, turning off other options, putting results text , saving file out name , format. know there must way automate though export , save process though.

i looked using job vb script, simple report run on demand , not on schedule; job seemed overkill. other option found bcp, looks need. know of cmd tool though , can't quite figure out how leverage within sql query. in case needed here example of stored procedure doing. appreciated , thank in advance. using sql 2008 r2 btw.

 update table#1 set column1 = '' column1 = 'xx'  update table#1 set column2 = '' column2 = '000000'     select   column1 +            column2 +            column3    table#1 column4 = 't' 

bcp command line utility, can access through sql use of xp command shell need xp_cmdshell enabled. following parameterized example of how call inside of sql

declare @server varchar(50) = 'servername' /* source sql server */ declare @filename varchar(50) = '' declare @sourceproc varchar(50) = '' declare @sql varchar(8000) declare @rundate varchar(10) = replace(convert(date, getdate()), '-','') declare @filepath varchar(255) = '\\uncpathtooutputfile\' declare @startdate date = (select dateadd(month,datediff(month,0,getdate())-2,0))--'1/1/2013' /* example of required date parameter */ declare @enddate date = (select dateadd(month,datediff(month,0,getdate())-0,-1))--'2/1/2013' /* example of required date parameter */ --  bcp export     set @sourceproc = '[schema].[storedprocname]'     set @filename = 'someoutputfile' + @rundate + '.txt'     set @filepath = @filepath + @filename     set @sql = 'bcp "exec [database].' + @sourceproc + ' ''' + convert(varchar(10),@startdate, 101) + ''',''' + convert(varchar(10),@enddate, 101) + '''" queryout "' + @filepath + '" -c -t"|" -r\n -s' + @server + ' -t'      --print(@sql)     exec master.dbo.xp_cmdshell @sql 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -