Stored Procedure-export results to CSV file

Michael Henderson 131 Reputation points
2020-11-24T21:49:03.223+00:00

Is there a way inside of a TSQL stored procedure to export the results to a CSV file?

Thank you.

Developer technologies Transact-SQL
{count} votes

Accepted answer
  1. Ronen Ariely 15,206 Reputation points
    2020-11-24T22:37:17.083+00:00

    Good day

    Using Transact SQL We execute scripts which meant to direct the database side but SQL Server provides multiple option to execute scripts in other languages from the transaction. Here are several options you can use:

    (1) Execute command shell command using the built it procedure xp_cmdshell - this is probably the simplest solution for most users

    (2) Execute Don.Net code using SQLCLR and build the file in the .Net code

    (3) Execute Python script and create the CSV

    (4) Same as before Execute R script

    (5) Trigger external server like sending email and in the server you create event to build the CSV

    (6) You can call a method of an OLE object using sp_OAMethod

    (7) You can use OPENROWSET and INSERT the result of the query to CSV file using the Microsoft.ACE.OLEDB.12.0 provider

    (8) and there are many many many more options which can be done, if someone really want to play with the options...

    NOTE!!! Remember the sentence "when you have a hammer, all you see is nails". The fact that you can do something does not mean you should do it !!!

    For most cases all the above is not recommended. In order to find the solution which best fits your needs you need to take step back and think about what you really need and not what you want to do now. Maybe with better design this requirement will not even be needed.

    Remember that SQL Server is only a set of services and in order to execute something you use client, so maybe ity is better to create the CSV from the client directly?

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2020-11-24T22:17:31.343+00:00

    In one single word: No.

    That said, I am sure that will be people who will suggest using xp_cmdshell to invoke BCP, but then you are outside SQL Server. And xp_cmdshell comes with security considerations.

    It is much better to orchestrate this from the outside. There are plenty of options, all depending your needs. BCP, that I mentioned, can easily produce CSV files, if you don't need values to be quoted. If you need values to be quoted, it still doable, you only need a format file. But if you need values to be quoted only when needed BCP is out.

    1 person found this answer helpful.

  2. EchoLiu-MSFT 14,621 Reputation points
    2020-11-25T06:59:11.583+00:00

    Hi @Michael Henderson ,

    I found a similar problem, please refer to:
    How to produce an csv output file from stored procedure in SQL Server

    Regards
    Echo


    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues November--What can I do if my transaction log is full?
    Hot issues November--How to convert Profiler trace into a SQL Server table

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.