Change the script to export to excel

chan patrick 1 Reputation point
2022-09-16T01:10:51.117+00:00

Hi All ,

my request is quite straightforward , I have a script similar as below , it can output what I want to see on sql server , what I would like to do is schedule to script and output to a excel file , would advise how to change the script to make it work ? thanks

select * from user_table ;

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,757 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,508 questions
{count} votes

3 answers

Sort by: Oldest
  1. Dillon Silzer 54,646 Reputation points
    2022-09-16T04:46:21.873+00:00

    Hi @chan patrick

    There is no way to export from a SQL statement directly to CSV. You can either use the first option or other options available on the website seen below.

    1) The easiest way to do this is to use the BCP Tool (via command line):

    bcp user_table out C:\Users\user\Downloadsdata.csv  
    -S SERVER\MSSQLSERVER,1433 -c -t"," -T  
    

    2) Other options are available below:

    https://learnsql.com/blog/how-to-export-csv-from-sql-query/

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

    If this is helpful please accept answer.

    0 comments No comments

  2. Olaf Helper 40,901 Reputation points
    2022-09-16T04:59:14.21+00:00

    and output to a excel file

    With plain T-SQL that's more then difficult, better use a ETL tool like SSIS = SQL Server Integration Services.
    See Import data from Excel or export data to Excel with SQL Server Integration Services (SSIS)

    0 comments No comments

  3. Seeya Xi-MSFT 16,441 Reputation points
    2022-09-16T06:24:14.477+00:00

    Hi @chan patrick ,

    Welcome to Microsoft Q&A!
    Please also refer to this blog: https://solutioncenter.apexsql.com/how-to-import-and-export-sql-server-data-to-an-excel-file/
    This blog contains some screenshots which may help you get some ideas.

    Best regards,
    Seeya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.

    1 person found this answer helpful.
    0 comments No comments