How to create excel export using sql query? [duplicate] - sql

This question already has answers here:
Export SQL query data to Excel
(5 answers)
Closed 8 years ago.
I need help in export data from table to excel file using sql query.
and with header and footer.
I used master..xp_cmdshell with passing db name, table name, and path where I store excel file any buddy can solve this. and any other way like query through export data to excel.
I must say I want to solve problem using query only. because I require only query.

If you need column headers the best way to export to a CSV file (which EXCEL will open) is by using sqlcmd. The syntax is something like:
c:\>sqlcmd -S servername -i c:\path\yourqueryfilename.sql -o C:\OutputPath\AFilename.csv -s","
See http://msdn.microsoft.com/en-us/library/ms162773.aspx to find the complete command line options available for SQLCMD.

Related

Export a Large SQL Database via Query

I am trying to export a database within Microsoft SQL Server Management Studio 2000. My database is extremely large with over 1,300 unique tables.
I am aware of the export wizard and copy/paste options but am looking for something that would allow me to export all 1,300 tables (or at least a few hundred at once) into a single csv or xls file. Copy and pasting or selecting "save as" for each table would take far too long and the export wizard only allows a few dozen tables at a time.
Well , I would not recommended you to do this, but if you desperately need a solution in the way you have described , here it is :
First , run this query on the database :
SELECT 'sqlcmd -S . -d '+DB_NAME()+' -E -s, -W -Q "
SET NOCOUNT ON;
SELECT * FROM '+table_schema+'.'+TABLE_name+'" > "C:\Temp\'+Table_Name+'.csv"'
FROM [INFORMATION_SCHEMA].[TABLES]
You might want to change the folder name as per your convenience
Second, Copy all the rows returned into a Export.bat or Export.cmd file
Third, run the Export.bat file to get 1300 odd tables you need in separate CSV files
Fourth, open a cmd window , navigate to the folder you have your files in and use the following command :
copy *.csv Export.csv
You will have a single Export.csv file containing all your tables, along with headers for each table
Perhaps this will help you to resolve your problem.
SQL Server Management Studio 2012 - Export all tables of database as csv

Procedure to create .csv ouput [duplicate]

This question already has answers here:
Procedure to export table to multiple csv files
(2 answers)
Closed 9 years ago.
please help me to create a procedure to export data to specified path from oracle database as .csv file take study names from another table.
QUERY : select * from enrollment where study_name = 'FTY67' ;
I have another table(studies) in same database with all studynames.
is there any way to create procedure that will take study names from studies table and repeat this procedure to create .csv files for all studies?
read some articles in internet but not found anything related to this.
please help.
You should look into the spool and set command. Since the database is usually running on a remote server, it can not write files to your local computer. In order to achieve this yu must write an sql where you disable certain characteristics in the terminal, and then spool the result into a file that you can access.
Something like this might get you started:
set term off colsep ";" pause off
spool myfile
select * from x;
spool off
exit
For an overview of the options you can use with SET refer to the oracle documentation here: http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12040.htm
With the proper set commands you are able to create the CSV file.
The above set commands are just a few you might need, but you will probably need addtional parameters to make your CSV usable.
Best to write this in a file.sql and run it using sqlplus:
sqlplus user#db #file

Moving data from Excel to SQL Server table

I have a very Simple excel sheet:
I am wanting to put this data into a table in SQL Server. I also wanted to add a field that contains a date.
what is the best way to do this?
Create a table in SQL server with the same number of fields that you have in the spreadsheet.
In SQL Server Management Studio Object Explorer you can right click the table you created and select "Edit top 200 rows". Highlight the data you want to copy in Excel and right click -> copy. Right click the space to the left of the first row in the SSMS edit tabe widow and paste.
After the import check the SQL table to make sure it has the same amount of rows as the spreadsheet.
You can add the date column to the SQL table after you do the data import. Or add it in the spreadsheet before you do the import.
You can first create the table in SQL Server with the added field and then use the import wizard in the Mangement Studio for importing the excel file. Or you create the table during the import task, and you add the new field later.
Option 1:
Read the data in an IDataReader, and then call a stored procedure to insert the data.
http://granadacoder.wordpress.com/2009/01/27/bulk-insert-example-using-an-idatareader-to-strong-dataset-to-sql-server-xml/
I use the above when I have ~~alot~~ of rows to import and I want to validate them outside of the database.
Option 2:
http://support.microsoft.com/kb/321686
or search for:
Select FROM OPENDATASOURCE Excel
Option N:
There are other options out there.
It depends what you like, how much time you want to put into it, is it a "one off" or you gotta do it for 333 files every day.
My solution was to convert .xlsx to .csv and then use this site to convert .csv to .sql. I then ran the sql file in sql server and generated my tables.
It can be also be done by creating a batch file.
For this you already need to have a table created on server with the same data structure as you have in excel.
Now using BCP you can import that data from the excel file and insert it into sql table.
BCP utility function
sqlcmd -S IP -d databasename -U username -P passwd -Q "SQL query mostly it should be truncate query to truncate the created table"
bcp databasename.dbo.tablename in "Excel file path from where you need to input the data" -c -t, -S Server_IP -U Username -P passwd -b provide batch size
You can refer to the link below for more options on the bcp utility:
https://msdn.microsoft.com/en-IN/library/ms162802.aspx
Open your SQL server interface software, add the date field to the table.
Go to excel, add the date column, copy the excel data.
Go to your SQL server interface software and use the functionality to import the data from the clipboard. (SQL server interface software that has this is for example Database4.net, but if you have another package with the functionality then use that.)
Alternatively use VBA with DOA or ADO to interact with the SQL server database and use SQL statements to add the field and copy the data into your table

Moving results of T-SQL query to a file without using BCP?

What I want to do is output some query results to a file. Basically, when I query the table I'm interested in, my results look like this:
HTML_ID HTML_CONTENT
1 <html>...
2 <html>...
3 <html>...
4 <html>...
5 <html>...
6 <html>...
7 <html>...
The field HTML_CONTENT is of type ntext and each record's value is around 500+ characters (that contains HTML content).
I can create a cursor to move each record's content to a temp table or whatever.
But my question is this: instead of temp table, how would I move this without using BCP?
BCP isn't an option as our sysadmin has blocked access to sys.xp_cmdshell.
Note: I want to store each record's HTML content to individual files
My version of sql is: Microsoft SQL Server 2008 (SP1) - 10.0.2531.0
You can make use of SSIS to read the table data and output the content of the table rows as files. Export column transformation available within Data Flow Task of SSIS packages might help you do that.
Here is an example, The Export Column Transformation
MSDN documentation about Export Column transformation.
This answer would have worked until you added the requirement for Individual Files.
You can run the SQL from command line and dump the output into a file. The following utilities can be used for this.
SQLCMD
OSQL
Here is an example with SQLCMD with an inline query
sqlcmd -S ServerName -E -Q "Select GetDate() CurrentDateAndTime" > output.txt
You can save the query to a file (QueryString.sql) and use -i instead
sqlcmd -S ServerName -E -i QueryString.sql> output.txt
Edit
Use SSIS
Create a package
Create a variable called RecordsToOutput of type Object at the package level
Use an EXECUTE SQL task and get the dataset back into RecordsToOutput
Use a For-Each loop to go through the RecordsToOutput dataset
In the loop, create a variable for each column in the dataset (give it the same name)
Add a Data Flow task
Use a OleDB source and use a SQL statement to create one row (with data you already have)
use a flat-file destination to write out the row.
Use expressions on the flat file connection to change the name of the destination file for each row in the loop.

import csv to sql

I have to import an csv file to SQL database table which already created (empty and has the same number of named columns). It would be great if you could suggest any tutorials or give some tips.
I assume you are using Microsoft SQL Server. Do you need to do this in a program or manually? There is a tutorial on using the bcp command for that, or alternatively a SQL command. If you need to parse the CSV file for your own code, see this previous SO question.