How insert/delete/update table automatically by another table in SQL Server? - sql

I created a windows form app using visual studio 2017 and MS SQL Server 2016.
It's working correctly. SQL table contains
tblMain(HomeID,Bed,Location,Address,Town,Province)
When I insert/update/delete it's working.
I also created 2 tables
tblHome (HomeID,Bed,Location) and tblLocation (Address,Town,Province).
Now I need to insert/update/delete tblMain through my windows form app and automatically insert/update/delete tblHome and tblLocation tables`.
Is this possible?

A trigger is a special type of stored procedure that automatically runs when an event occurs in the database server. DML triggers run when a user tries to modify data through a data manipulation language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view.
https://www.aspsnippets.com/Articles/Simple-Insert-Update-and-Delete-Triggers-in-SQL-Server-with-example.aspx

Related

Is there a way to default the trigger to run on specific user for DML operations?

I have a trigger on Table A in Database A which inserts data into Table b which is in Database B, both databases are on the same server.
A user with developer access to Table A in Database A is encountering an error when they perform dml operations as his role is restricted just to use Database A. Is there a way to default a trigger to run on a specific connection?
In SQL Server, triggers (and stored procedures and functions) have the EXECUTE AS clause. This gives you control over the permissions when the code is executed.

Difference between database level trigger and server level trigger in SQL Server

Can anyone please tell me the difference between database level trigger and server level trigger in SQL Server ?
Thanks in advance.
SQL Server 2005 introduced DML Triggers that can be set to fire on your chosen DDL events such as CREATE_TABLE, ALTER_TABLE, DROP_TABLE, ALTER_DATABASE, CREATE_LOGIN etc.
DDL Triggers can be set within 2 scopes:
Server scope: Triggers created with Server scope must target server DDL events such as CREATE_DATABASE or CREATE_LOGIN
Database scope: Triggers created with database scope must target database level events such as CREATE_TABLE or ALTER_PROC.
See the full list of SQL Server DDL Trigger Events (including their scope) on msdn here.
Syntax of a DDL trigger:
CREATE TRIGGER [TriggerName]
ON [Scope (Server|Database)]
FOR [EventName...],
AS
-- code for your trigger response here
Database trigger : Database trigger has been work on table like insert, update and delete record.
Server trigger : Server trigger has been work on database like drop table and alter table. It is important of security level. If you single user access database should be not important. But Multi user access database has been important. Which user would be work on database.
Refer this link
http://blog.sqlauthority.com/2007/07/24/sql-server-2005-server-and-database-level-ddl-triggers-examples-and-explanation/

I need to check if table has been inserted a new record from a vb.net service application

I have 2 SQL DB, in one of the DB I have a table with a trigger that when a new record is inserted, insert a new record in the other Db in log table. I have running a vb.net service application and I need to know / check if the log table had a new record to make actions (example: send email). Which will be the best way to do this? I'm running MS SQL 2008 on a Windows Server 2008.
Why not make use of master database's sp_send_dbmail stored procuedure to send email from inside of the trigger body? https://msdn.microsoft.com/en-us/library/ms190307.aspx.
You will have to create a profile.
https://msdn.microsoft.com/en-us/library/ms187605.aspx

Column Constraint Sql 2008

I have been trying to find out if I can make a column Open on Insert, and closed on Update.
What I mean by that, is I need a column that I can give it a value only on Insert, but if I try to give it a value with Update, the statement would fail.
I am working with SQL 2008...
You can't do this with constraints - you need to use triggers for this.
A trigger is a special kind of stored procedure that automatically executes when an event occurs in the database server. DML triggers execute when a user tries to modify data through a data manipulation language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view.

New trigger in sql server 2005 database

The database I am using already has a 2 triggers on part number table (on insert and on update). Insert trigger updates creation date, and update trigger updates modification date.
I have to add 3 more triggers to log updates to this table (on insert, on update and on delete)
In what order they will be executed? Before existing trigger or after? Can I control that?
sp_settriggerorder will allow you to set first or last trigger.
If you have more than two and the order matters, combine them into one trigger and split the functionality over the stored procedures.