Create trigger and send emails - db2

I have to create a db2 triggers and I need to check one of the inserted values and base on this checking i need to take a send email action
let us consider the below table for example
ID NAME TransactionID needapproval
i need to create trigger to read the needapproval values in the inserted row and check if it is yes then send an email to the senior
how can i do this please
db2 connect to mydb -- or whatever your database name is.
db2 update db cfg using smtp_server ':'

Starting with DB2 LUW 9.7 you can use the UTL_MAIL system module to send emails, once you have set the SMTP_SERVER database configuration parameter. Check out samples in the manual, where it describes the UTL_MAIL.SEND() procedure.

Related

use a db2 trigger to identify the user initiating

When creating a DB2 trigger (db2 version 10.1 LUW), I am looking to capture the userid that initiating the trigger.
For example, if a user inserts data, the after insert trigger should write to a log on who inserted the data. This not meant for production purposes - just is to identify who is updating / inserting test data.
You can obtain the value of the SESSION_USER special registry variable. As an alternative look at the SYSTEM_USER registry. There are differences if you use features like SET SESSION AUTHORIZATION or use TRUSTED CONTEXTS.
Try this as a quick test:
select session_user from sysibm.sysdummy1;
select system_user from sysibm.sysdummy1;

Triggering Code when a specific value is inserted into a table column in an Azure SQL Table

I am seeking suggestions on methods to trigger the running of code based on specific event occurring.
Basically I need to monitor all inserts into a table and compare a column value against a parameter set in another table.
For example, when a new record is added to the table and the column [Temperature] is greater than 30 (which is a value set in another table). Send an alert email to notify of this situation.
You can create a trigger (special type of stored procedure) that is automatically executed after an insert happened. Documentation for triggers is here: https://technet.microsoft.com/en-us/library/ms189799(v=sql.120).aspx
You will not be able to send an email out of SQL Database though.
Depending on how quick you need the notification after the insert, maybe you could make an insert into yet another table from within the trigger and query this new table periodically (e.g. using a script in Azure automation) and have the email logic outside the database.

T-SQL Select a bunch of data from another DB and Copy to DB2

H all,
Fist of all, thanks for reading this.
My Question is, how can I select a bunch of data from ANOTHER database and insert to my own database with same coloum name and field?
I just can think of is using select from DB1 and then insert into DB2.
I plan to written this process inside a stored procedures.
Is there a better way to do so?
Development enviroment :Sql server 2008 and VS2010(using .net C# to excecute Stored prod)
Thank you, Appreciate its lot.
And Please don't hesitate to voice out my error or mistake.I wish to learn from mistake
LiangCk
You can do the following if its the same database server
INSERT INTO [DB].[UserName].[TableName]
Select * from [DB2].[UserName].[TableName]
[DB] is the name of database 1
[DB2] is the name of database 2
[UserName] is your sql server username (dbo,....)
[TableName] is, off course, your table
If you have different sql servers you can connect both servers
using linked server.
http://msdn.microsoft.com/en-us/library/ff772782.aspx

Advantage database how to retieve connection name

I am using Sybase Advantage, I have 2 tables:
The first table has the data records
The second table stores a history of the first
The first table has triggers to populate records in the second table depending on which fields get changed.
I would like to store the connection name (PC which made the request), the name that is displayed in the active queries page (Server Info dialog) and not the username. Does anyone know if this is possible?
Thanks
The following SQL statement can be used to retrieve the computer name instead of the user name.
SELECT *
FROM
( EXECUTE PROCEDURE sp_mgGetConnectedUsers() ) ConnUsers
WHERE
ConnUsers.DictionaryUser = USER();
The stored procedure sp_mgGetConnectedUsers is documented here.

Specify max number of databases a user can own

Is this possible?
I would like to have a limit for a user, lets say 5 databases.
So when he tries to issue a CREATE query to create a 6th an exception is thrown.
No, you cannot do this - at least not in a declarative way (just simply specify the max number of databases owned for each user).
The closest you could get with standard SQL Server functionality would be to create a DDL trigger for CREATE DATABASE and in that trigger, check to see if the current user already owns five databases, and in that case, let the trigger fail the operation.
Something along the lines of this (taken from TechNet sample):
CREATE TRIGGER ddl_trig_database
ON ALL SERVER
FOR CREATE_DATABASE
AS
-- here, check to see if current user already owns five databases
-- and if so, fail the trigger by using RAISERROR
GO
look into DDL triggers, with a trigger like this one below you can trap the CREATE DATABASE statement
CREATE TRIGGER ddl_trig_database
ON ALL SERVER
FOR CREATE_DATABASE
AS
--do something here
-- select count(*)from sys.sysdatabases where sid = ???
GO