June 26, 2012 at 2:11 pm
Does anyone have some t-sql to export the results of a query in pipe delimited format? Would powershell be better?
June 26, 2012 at 2:23 pm
i believe your options are to use bcp, CLR or powershell
bcp + TSQL isgoing to require opening up xp_cmdShell.
EXECUTE master.dbo.xp_cmdshell 'bcp "SELECT object_name(object_id) As TbLName,name as ColName FROM SandBox.sys.columns ORDER BY object_name(object_id), column_id" queryout C:\Data\Objects.txt -t"|" -c -T '
you can use a CLR to write to disk from TSQL, I have an example i threw on codeplex: http://sqlclrexport.codeplex.com/
that's going to require you to install a CLR, which may or may not be what you are after.
EXECUTE CLR_ExportQueryToCustomDelim @QueryCommand = 'SELECT * FROM @TableVariable',
@FilePath = 'C:\Data\',
@FileName = '\Results_export.txt',
@IncludeHeaders = 1,
@CustomDelimiter = '|'
and of course powershell, as you already suggested.
i don't have a power shell example available in my snippets yet, sorry.
Lowell
June 26, 2012 at 3:35 pm
Lowell (6/26/2012)
i believe your options are to use bcp, CLR or powershellbcp + TSQL isgoing to require opening up xp_cmdShell.
BWAA-HAAA!!!! I use xp_CmdShell to run PowerShell from T-SQL. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
June 26, 2012 at 4:15 pm
Jeff Moden (6/26/2012)
Lowell (6/26/2012)
i believe your options are to use bcp, CLR or powershellbcp + TSQL isgoing to require opening up xp_cmdShell.
BWAA-HAAA!!!! I use xp_CmdShell to run PowerShell from T-SQL. 😀
ok but can you use xp_CmdShell to run Powershell, which in turn calls bcp? 😀
That's the ticket!
Lowell
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply