How to send mail from SQL server? - sql

I started to use SQL for a few days, so I'm just a beginner.
I want to do the following: I want to run a query in every month, which gives back the data I need, then store it in an .xls file or something else, and then send it to some recipients. Can I do this fully automatically somehow in SQL Server 2005? Could someone give me an example or a guide how to do this? I'm using Microsoft SQL Server Management Studio 2008.

You can create a SQL Server Agent job that executes the sp_send_dbmail stored procedure.
The results of the query can be included in the body of the e-mail message or attached as a file.

What you are looking for is SQL Server Reporting Services and its subscriptions. They allow you to automatically send reports (results from queries ran against your DB) via email (in a PDF, HTML, XLS, etc. formats).

You can use the below code snippet to send a mail from the SQL server.
EXEC master..xp_sendmail
#recipients = 'xxx#yyy.com',
#subject = 'Hello from SQL team',
#message = 'A new mail has been sent.'
Also more information is found on these procedures in the below link.
http://msdn.microsoft.com/en-us/library/aa155737(office.10).aspx

Related

SQL Server Profiler - Textdata On 'Prepare SQL' and 'EXEC Prepared SQL' Commands Blank

I have inherited an application that uses SQL Server as it's database.
To understand the tables accessed from the application when doing specific tasks in the closed code application I usually use SQL Profiler to identify the table and queries performed behind the scenes for each function ( for example adding a customer ).
However with this application all the SQL statements issued are 'Prepare SQL' and 'Exec Prepared SQL' both of which on this application return nothing in the 'TextData' column which usually shows the SQL commands performed.
The login events are also not shown and all SQL statements are shown in profiler attributable to the applications service account. I have therefore deduced users sign on to the application but the application communicates with the database via the service account and the SQL statements are hidden somehow.
Does anybody know how to expose/reveal the TextData column contents on the 'Prepare SQl' and 'Exec Prepared SQL' statements so I can see which tables the application is using? I know there are lots of packages out there that claim they can do this but I suspect they are just using the same information as profiler so won't show anything extra.
Select the SQL:StmtStarting under TSQL and SP:stmtStarting under Stored Procedures in Event Selection tab of the Trace properties.
This way you can make you don't miss any statements in the Text Data.

Moving data from SQL Server 2008 to remote SQL Server 2000, using sqlcmd

The setup:
I have two different machines, one with MS SQL Server 2008 which is my main machine and a secondary with MS SQL Server 2000. Newly generated data are stored in a specific table on the main server(2008).
The problem:
My main machine has limited storage, whereas my secondary one with the older SQL version(2000), doesn't have such kind of limitations.
A possible solution:
At least as a temporary solution, i could try to move some data, on a daily schedule, from the main machine to the secondary, using sqlcmd, run by a Windows Task Scheduler.
The largest portion of my data are stored on a single table so the idea is to "cut" them from the table on the main server and append them on a "backup/depot/storage" table on my secondary server.
What i have tried so far:
So far, i haven't been able to simultaneously connect to both servers from the main one, at least using sqlcmd. Is there a limitation for the sqlcmd to the connections it can create simultaneously?
Other possible ways:
Is there a suggested practice for that case? Would it be a good idea to write a vbs script to export from the main server and import to the secondary?
All corrections and suggestions are welcome. And thanks for your time.
First, link the servers. From the server you want to INSERT into, run this SQL (using any tool you want to run SQL with... SQL Server Management Studio or SQLCMD or OSQL or whatever):
EXEC sp_addlinkedServer N'remoteSqlServer\Instance', N'SQL Server'
EXEC sp_addlinkedsrvlogin #rmtsrvname = N'remoteSqlServer\Instance', #useself = 'FALSE', #rmtuser = N'user', #rmtpassword = N'password'
Replace "remoteSqlServer\Instance" with the actual host-name\instance of the SQL Server you want to copy data FROM. Also replace the "user" and "password" with appropriate login credentials to that server.
After that is done, you can execute SQL like the following against this server (from anywhere, including SQLCMD):
INSERT INTO [LocalTable]
SELECT * FROM [remoteSqlServer\Instance].[DatabaseName].[schema].[TableName]
Again, this is just an example... you'd replace all those values with values appropriate to your source and destination databases (everything in the square brackets). For instance, it might look something like this:
INSERT INTO [HistoricalData]
SELECT * FROM [DBServer\SQL2008].[ProductionDatabase].[dbo].[CurrentData]
I hope this helps.

MS sql command automatically run when server activity

I have a MS SQL database storing information of members. I wish every day automatic test data today is the birthday of members, then sends congratulatory letter.
So I will use what command? Store Procedure or triger or something else ...? And how to use?
You can you the SQL Server Agent service to schedule a job that executes a procedure that finds the correct members and Database Mail to send the mail. The links provided should have the information you need.

How can we send email with an attachment to 500 people using SQL Server (SSIS, SSRS or SQL AGENT)

I need your advice. How can we send email with an attachment to 500 users. We are using SQL Server 2008 R2 Enterprise edition in our company.
I had tried using the Send Mail task from SSIS, but seems we can send maximum of 18 recipients due to the constraint in To: Cc: Bcc:.
Is there any way we can send using SQL Agent or SSRS ??
I have another doubt regarding SSRS report subscription recipients.
To how many maximum number of recipients we can send report using subscription. The reason i am asking this second is if i can send a report to 500 users by using SSRS report subscription then i can use SSRS.
Thanks for your time!!
I would just use Database Mail feature and write your own T-SQL code to send the email using Database Mail. The sp_send_dbmail #recipients parameter you would use to pass in a delimited list is type varchar(max) so I am sure it can handle your 500 email address. I cannot find anything on MSDN that states a limit other than the data type limit.
I would probably stick with SQL Agent in this instance if it is going to be repeated.
EDIT
As suggested by Brian in the comments, you probably do want to use the #blind_copy_recipients, which has the same data type.
Actually SSRS would work quite nicely. We send out about 4000-5000 emailed reports using a SSRS every few weeks.
Create a report that will produce your attachment. Create a query providing email addresses and any report parameters. Next create a data driven subscription using your query. You should be able to scale to thousands of recipients as needed.
You can write a cursor that loops thru all the recipients and send emails one recipient at a time... Even if you have never written a cursor it is not hard to do following the example in the help...

How to run a stored procedure report and then send the result into email?

I am using SQL Server 2005. I have a report that is using stored procedure with just few lines of records and would like to send the whole recordset through an email and sets this into a schedule.
This is only an interm solution BTW till we integrate this stored procedure into reporting service + .net app.
I am appreciated your comment.
Thanks
If you don't already have Database Mail configured, you'll need to do so, including a profile that sends to a valid SMTP server. Once you do that, you can use sp_send_dbmail with the #query argument, which will embed the query results (e.g. "EXEC dbname.dbo.your_proc_name" - dbname is important) to an e-mail, or the #attach_query_result_as_file argument, which is self-explanatory.
sp_send_dbmail docs are here : http://msdn.microsoft.com/en-us/library/ms190307%28SQL.90%29.aspx