SQL - data from select in excel file - sql-server

i have next query
select no,
item
from myTable
and I get next table
no item
1 a
2 b
3 c
4 d
I use tsql, Microsoft SQL Server Management Studio.
Is there any way to write here some code to export table from my select in excel...

I am not aware of any way to write a query in SSMS and then export the results to excel, but you can connect an excel sheet to SQL Server and run queries into an data table from there using the From Other Sources - From SQL Server menu in the Data tab on the ribbon:
This is good if you want to build on that data within Excel and you need to refresh the data but don't want to update your formulas. There are obviously security implications with this as users of the Worksheet may be able to access other data on your server.
If you want to simply export an Excel file with data in to a file store, you would need to use SSRS and a report subscription. Alternatively, if you simply want to save the results of your query to a file, you can either copy and paste manually or choose to Save Your Results To Text in SSMS:

from your excel sheet you can create a dynamic connection between a SQL Server database and your Excel workbook.
Follow the steps in below link.
Steps to Connect a SQL Server database to your workbook

Related

Excel: Best way to refresh Excel tables from SQL Server and in the Cloud?

I have an Excel workbook that has two worksheets. Each worksheet has a table reference that I use in a PowerQuery, to connect to a SQL Server database.
Users are able to RIGHT-CLICK -> REFRESH each table and update the data from SQL Server.
I want to be able to place this workbook in Sharepoint Online (Office 365), so more than one person can open it, at the same time, and get the same data - also at the same time.
Is all this possible please?

How to export data from SQL without using the import/export wizard?

I need to export data from SQL to csv or Excel without using the wizard. It must be done through query. I looked a lot online, but could not find a solution. The best solution I found was to use a command code to export the data, but this returns the data without headers. Please help !
Edit:
I am new to SQL, and really been researching this for a while. I therefore would be grateful if you can give me the code to do this in SQL management studio (if possible). Many thanks !!!
Rather than pushing the data out from SQL Management Studio, you could create a connection to the database within Excel and pull the data in.
In Excel, choose Data > From Other Sources > From SQL Server (or Data > New Query > From Database > From SQL Database)
Select the database, database table etc.
If you need to filter or otherwise manipulate the data rather than pull in the whole table, then you can go to the Connection Properties dialog afterwards and enter whatever SQL you want into the Definition tab. For instance select * from <table> where <field> = "value".

Link Excel sheet to SQL Server

Is it possible to link an Excel sheet to a SQL Server database? I want to be able to join the Excel columns to the tables in SQL Server and run queries. I can find plenty of examples to link the tables from SQL Server to Excel, but I want the other way around.
Thank you in advance for any suggestions.
Use SQL Server Import and Export Wizard.
Set your worksheet as a Data Source
Excel file path
Specify the path and file name for the spreadsheet
from which to import the data. For example, C:\MyData.xlsx or
\Sales\Database\Northwind.xlsx. Or, click Browse.
Browse
Locate the spreadsheet by using the Open dialog box.
Excel version Select the version of Excel that is used by the source
workbook.

SSIS Export to Excel Succeeds Once Only

I've created an SSIS package with the purpose of deleting and then recreating an Excel sheet, and then inserting data from a SQL database into that Excel sheet.
What I currenty have is:
1.) An Execute SQL task to delete 'Sheet1' from a local Excel file.
2.) An Execute SQL task to recreate 'Sheet1' in the same Excel file.
3.) A Data Flow task that uses an OLE DB Source to a 2005 SQL Server database.
4.) An Excel Destinaton (using an Excel Connection Manager) which points at / maps the SQL Server columns to the local Excel spreadsheet.
So essentially I'm deleting the Excel Sheet to remove old data, recreating it, and then trying to import the SQL data into it.
This worked well when I ran it the first time. When I try to rerun it however, all the tasks run successfully, the correct number of rows are shown to move to the Excel destination on the data flow tab, but the Excel spreadsheets are blank.
Can anyone suggest why this might be happening?
Thanks, Gavin
Is this the only sheet in your Excel file? If so, it might be easier to use the File System Task component to delete the entire file before creating your sheet (and therefore file) in your Execute SQL statement.

Import CSV into SQL Azure Database using SSMS

I have a set of large CSV files with many columns each that I need to import into a SQL Azure database. Ordinarily I would use the import wizard in SQL Server Management Studio. However, the wizard does not appear to be an option when connecting to SQL Azure in SSMS. Is that correct? And if so, what is the recommended tool for accomplishing this task? I'm looking for a tool that will infer from the data what the columns should be allowing me to override the data type as needed. Since I have a lot of columns in each of the files I'd like to avoid the tedious work of manually writing the SQL code to generate the tables.
This worked for me:
Open SQL Server Management Studio
Connect to Azure
Right-click the database
Go to Tasks > Import Data
Select your flat file(s)
Upload to Azure SQL and create an SSIS package based on this workflow
I sometimes get errors with CSV files this way, but either using an Excel file or inspecting the options of the CSV data columns in the Import Wizard should suffice.
Make sure you have appropriate permissions assigned to your user account.
They could've / should've made this easier, like a SFTP + insert or a GUI import directly to Azure SQL like in Hue.
When you are transferring any data to SQL Database, the data should be structured. The proces will be to convert your CSV to a table structure and then migrate it directly to SQL Azure. Actually you can write a stored procedure in SSMS to do it all in one.
Because CSV file could be tab, comma, or any other character delimited, you can do bulk insert in local DB first as described here and then sync the table to SQL Azure.

Resources