I have a database view that gathers about 10 member names a day, based on when they joined.
I want to setup something where these people will automatically be emailed an html page
i have made.
and repeat this daily, at first i was going to build an asp.net page
where i would pull the view and loop through the email addresses, but i wanted to see if it was possible to do this soley in sql server 2005
Could i use a trigger or a stored procedure or i havent really used sql server agent
sp_send_dbmail as the others said is what you use to send the E-mail
To set it up to E-mail daily you can use a SQL Server Agent Job
In SSMS, connect to your server, then open SQL Server Agen->Jobs
In here you can set up the steps for your job and the frequency with which it runs.
You can use sp_send_dbmail to send email directly from SQL Server, assuming you've set up Database Mail.
You could use an INSERT trigger and send them an email on the spot as soon as they join.
See: sp_send_dbmail at http://msdn.microsoft.com/en-us/library/ms190307.aspx
Trigger example: http://www.codeproject.com/KB/database/TriggersSqlServer.aspx
Related
I am attempting to create one single database to store all login errors.
insert into [dbo].[SQL_ErrorLog]
exec sp_readerrorlog 0, 1, 'error'
The above code gets me the information that I need for the current long and I understand that changing the 0 to a 1,2....etc will get me the previous days logs.
I have 4 different environments and instead of setting this same job up on each environment, I would like to control it all from 1 single job. I intend to add a field to determine which environment the log information is coming from.
I know that I could also set up staging tables on each environment and then run a select statement to pull in data from each staging table to the final table, however again I am trying to complete all the work from one environment if possible.
I have linked the other environments using the linked servers and can select data from any of them without a problem.
My question is more related on how I can run the exec sp_readerror stored procedure on the other server and insert that data into my master table.
An example would be:
Env0 - This is where the master table would be and where I would like to set everything up
Env1
Env2
Env3
I would like to be able to pull sp_readerror 0, 1, 'error' information from Env1, Env2, and Env3 and populate it on Env0 without using staging tables on each individual environment if possible.
Please let me know if this is not 100% clear. It makes sense in my head, however that does not always come out in text form. :)
Thanks in Advance.
If you are using linked servers it seems like you could link together multiple calls using go from the main source server. This will work assuming your linked servers are linked off one server.
INSERT INTO [Linked Server Name]. [some database name].[dbo].[SQL_ErrorLog]
EXEC [Linked Server Name].[some database name].[dbo].sp_readerrorlog
GO
INSERT INTO [Linked Server Name2]. [some database name].[dbo].[SQL_ErrorLog]
EXEC [Linked Server Name2].[some database name].[dbo].sp_readerrorlog
GO
INSERT INTO [Linked Server Name3]. [some database name].[dbo].[SQL_ErrorLog]
EXEC [Linked Server Name3].[some database name].[dbo].sp_readerrorlog
GO
INSERT INTO [Linked Server Name4]. [some database name].[dbo].[SQL_ErrorLog]
EXEC [Linked Server Name4].[some database name].[dbo].sp_readerrorlog
I think this will be your best bet. You can use the agent and then put all of these into the agent job and run the job. They will need to be fully qualified in order to run on the correct linked server.
I am frequently testing certain areas on a development server and so running a pre-defined SQL statement to truncate the tables in question before testing again. It would only be a slip of a key to switch to the live server.
I'm looking for an IF statement or similar to prevent that.
Either to check the server name, database name, or even that a certain record in a different table exists before running the query.
Any help appreciated
For such cases I use stored procedures. I'd call them TestTruncateTables, etc.
Then instead of calling TRUNCATE TABLE you should CALL TestTruncateTables.
Just make sure that the procedures are not created on the live server. If by any chance you happen to run CALL TestTruncateTables on the live server you only get an error about non-existing proc.
I need to identify and be notified when records in my SQL Server 2012 database table meet a specific condition. For example, when a record in the trans_record table has any entries in the “status” column of “error”.
The query is simple enough and I have setup an SSIS package to query the table with the appropriate “where” clause. I followed this with a SQL Agent job to run the SSIS package daily at 7:00am and export the offending records to a flat text file.
What I need to understand is how I can be emailed only when a record is found with the query performed by the SSIS package. It appears that the SSIS package will always create a file regardless if any records are found.
I see that SQL Server supports Alerts and defined Operators to be notified, but I don’t understand how to be notified only if my query returns a valid record.
How can I set this up in SQL Server?
Thanks
If I understand correctly, you would like to be emailed by SSIS only when there is a record in the table with a status of error. If this is correct then you can achieve this from SSIS package.
If you would like to query the database and send an email, then you will require two Tasks and two connections:
Execute SQL task
Send Email Task
Database Connection
Email connection
Configure connections with relevant database connection details and exchange details for email connection.
Add the connections to the tasks, create Precedence Constraint from the Exec SQL to Send Email task.
In the Exec SQL create query or use stored procedure to return false or true if the record exists, or if you prefer count of the records in the table.
Return the outcome of the query to the variable (SSIS variable, you will have to add it in the package). Change the settings on the Precedence Constraint to add evaluation of expression and use your variable i.e. if you set the variable to be #recordPresent then the evaluation will be #recordPresent==true (if the value returned is false or true) or #recordPresent > 0 (if the variables contains the count of records).
If the expression evaluates to true and constraint is of the type requiered i.e. success, completion, failed the control will be passed to the next task in this case Email. If Send an email task is set correctly (from, to, subject and Body of the email as well as exchange connection string is correct), email will only be sent when there is a record in the table.
I hope this helps.
My IT department has a healthy disdain for cursors, yet I need to iterate through a data set to send emails from SQL Server 2014. What is best practice for doing this using set-based logic?
Presumably you are using Database Mail to send the emails out via SQL Server, in which case, emails are sent by calling the sp_send_dbmail stored procedure.
With the exception of emails with the same content that need to be sent to multiple recipients whereby you can just add multiple recipients to the #recipients, #copy_recipients/#blind_copy_recipients, in order to send multiple emails you have to call sp_send_dbmail once per email. Therefore, you need to call it within some form of loop as you can't call the sproc within a set-based query.
Technically, you could use a set-based query to generate some sql into a variable which would contain one call to sp_send_dbmail per email to send, and then execute that statement, but that could get a bit messy/less readable.
I'm using SQL Server 2008 here. I inherited an old web app that is dying, and being replaced by a totally new web app. The new project is up and running but the old one will exist for the next month and a half for the transition period.
Here's the problem: action needs to be taken when someone adds a new record to a table in SQL Server using this app. The old source code is pretty hosed (seriously, no version control before my arrival) and I can't afford to take the time to hobble something together just so I can get an email notification using the old app.
My thought - use a SQL Server trigger to send an email AFTER INSERT. Really this is all I want: whenever a new record (and it's always one, not dozens) is entered into a table, I want to send myself and another lucky person an email. I've never done this in SQL Server but it seems doable.
Here's my SQL script as it currently stands:
CREATE TRIGGER NotificationMail
ON OldJunk.[dbo].[JunkTable]
AFTER INSERT
AS
BEGIN
EXEC msdb.dbo.sp_send_dbmail --QUESTION: I HAVE NO IDEA WHAT TO PUT HERE, WHAT FOLLOWS
-- IS JUST COPYPASTA FROM A FORUM
#recipients = 'shubniggurath#email.com, someoneelse#email.com',
#subject = 'Old Registration Request - New Record',
#body = 'Somebody decided to register using the old system.'
END
GO
I'm getting this error when I try to execute this create statement:
Cannot create trigger on 'OldJunk.dbo.JunkTable' as the target is not in the current database.
Thanks in advance for your help.
You have to be in the OldJunk database (by using the USE .... command in SQL Server Management Studio), and then create the trigger using these SQL statements:
USE OldJunk;
GO
CREATE TRIGGER NotificationMail ON [dbo].[JunkTable]
.....
You cannot use the three-part (database).(schema).(object) notation in the trigger definition.
If that doesn't work - then you probably don't have such a table - is there a typo? Or is this not really a table?