How to export data from SQL Server table to Excel file - sql

I have a table in a SQL Server database. I want to send the Excel sheet via Email attachment. I have a code to send the mail but i need to generate an excel sheet of a given table.

Why don't you have a look at this link from Mr. P Dave
http://blog.sqlauthority.com/2013/11/20/sql-server-how-to-export-data-from-sql-server-to-excel-or-any-other-format/

Related

I have list of emails in Excel sheet. What is the simplest way to search records from database which contains the same emails from the Excel file?

In an Excel sheet, it contains information of people and also their email similar to this.
S.N Email
1 short#abc.com
2 medium#xyzabc.com
3 abc#def.com
4 veryverylongemail#abcdefghijkl.com
5 averagesizemail#abc.com
...................................
// there are hundreds of more emails like these
In the database, suppose there is a table named Persons which contains all information about people. Now I want to get only those records in which UserEmail column contains the above list of emails.
Although I can copy paste all emails, and search in database using query something like this:
SELECT * FROM PERSONS WHERE USEREMAIL IN (
'short#abc.com',
'medium#xyzabc.com',
'veryverylongemail#abcdefghijkl.com'
..... )
Since there are hundreds of emails, so this will be time consuming because I will have to insert quotation in each email front and back along and also comma. I am looking for a simpler solution.
What is the simplest way to search those records ? I just want to search the records contains those emails.
You should import an excel file in SQL server using the import data option.
Go to SQL server management studio (SSMS)
Right-click on your Database -> Click Tasks -> Click Import data
Once data is imported in the SQL Server table.
You can write query like this -
SELECT * FROM PERSONS WHERE USEREMAIL IN ( Select Email from <YourExcelImportedDataTableName> Where Email is Not null)

I have a DB table with a comment column with multi line data

I have a DB table with a comment column. Users input there comments as multi lines.So while exporting this result to an excel, the comments are getting added as a new row.
How can I copy the data from SQL to an excel correctly.
I am using Teradata SQL assistant and there is no option to directly export details to excel. Exported data to txt & htm and imported that to excel failed because of the comment text format issue.
PLEASE SUGGEST A SOLUTION.

SSIS copy data from SQL db to multiple tabs on the same excel destination

I've got an SSIS package that loads data from a flat file source into a table within a SQL Server Database i then have a SQL task to get specific data and load into an excel destination, i want to add another SQL task to load different data into the same excel workbook but on a different tab is this possible?
Yes it's possible. You can use same excel file to save diffrent data sets in different worksheets. In Excel destination editor for Excel Destination component press [New] button and change name of the table in CREATE TABLE statment to name of the worksheet you want to use. After that select new it as a worksheet name.

How to use SQL Server to email an Excel workbook containing the results of a query

I need to send an email that has the results of a query formatted as an Excel spreadsheet using SQL Server.
For example, every Monday, run a query such as:
SELECT * FROM dbo.Table
Save the results in an xlsx Excel workbook and email it.
You have three choices:
Use SSIS to create the xlsx file and email it out.
Send a CSV File
as an attachment using sp_send_dbmail
(https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql)
Write a .Net based CLR procedure to create the xlsx file - for
example see this link where someone had issues and a solution with
this:
(Create an SQL CLR UDF with C#)
As it is, your question is way too broad for Stack Overflow and all of these solutions require not only a lot more input from yourself but will be subject to opinion as well.

Cell format in SQL Data Connection within Excel Workbook

My job has tools that require us to mass upload data through Excel workbooks, so I'm trying to embed my SQL results into my files to save me a step. I've created the data connection, but haven't quite figured out the code format in the 'Definition' box yet.
Right now I have:
select custcontnum from "CONTACT"."dbo"."Incont" where custcode='SNH' and username = '[Custcontnums$G2]'
But it's pulling back blank. (If I define the username criteria itself between single quotes, it does work) Eventually I would like to create an "in" statement for the username criteria, so something like:
select custcontnum from "CONTACT"."dbo"."Incont" where custcode='SNH' and username in ('[Custcontnums$G2]','[Custcontnums$G3]','[Custcontnums$G4]')
What is the correct format for an excel reference in the data connection feature for Excel?