MS sql command automatically run when server activity - sql

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.

Related

Find what application is connected with what login to what database what table and what columns

Is there a Script which finds the current activity
from application->login->Database->Table->Column level ?
I have used
SP_who2, sp_who2'Active',Sysprocesses
Activity Monitor
Audit
Profiler
Trigger
Extended Events
and coludnt get column level data connections, i was able to get the sql statements, table name, database,instance, application, login name ...but I couldn't get Column Names
the reason I am trying to find to track all usage and re architect the Database..
any help is appreciated
SP_who2 and sp_who are the ones I have even used to get the required information. You can as well check against sys.sysprocesses to know about processes that are running on an instance of SQL Server.
If you want the columns involved in the queries then consider using SQL Server Tracing probably.

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...

create ms sql server job schedule from datetime database

I'm using microsoft SQL server management studio 2005
I'm trying to run a job at a specific time and that time is stored in a database.
I can't insert the schedule manually because it is up to the user to decide what date and time the job(s) has to be done. php collects the time and date and sends it to a database.
I thought about runing a job every min and in my execution I have an if statement that only activates when the datetime stored in the database is one minute greater or lower than the current datetime. but doing it like this would be inaccurate and very inefficient. would it be possible to create a schedule directly from a query or job?
You can create and modify both jobs and schedules programmatically using SMO or TSQL. That means you can use an external application, a stored procedure or a trigger to create or update a job schedule based on the table data. SMO is probably the best way to go if you want to build an application to manage jobs, but on the other hand it's .NET only.

How to send mail from SQL server?

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

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