Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hello All,

Am relatively new to this SQL and one of the fellow CodeProjecteer suggested me to write a trigger in order to automatically insert a data into the TABLE2 when a data is inserted in TABLE1.

I have been looking into many ways to do it. Like: Creating a button in asp.net where i write a SQL command, so when user clicks on the button the SQL statement executes. But the main issue being the SQL commands are huge in complexities.

Can anyone please help me out to create triggers or any other way to execute my statements.

Thanks
Saikrishna

What I have tried:

I have a following SQL statements to be executed through TRIGGER

INSERT INTO Table3
SELECT Capacity.[SPOC-Partner],
COUNT(PMKVY.CentreID) as Candidate,
COUNT(case when [Aadhar Validation-Aadhar Number Validated] = 'Yes' then 1 end) as Validated,
COUNT(case when [Result Approved by SSC] = 'Pass' then 1 end) as Results_Pass,
COUNT(case when [Result Approved by SSC] = 'Fail' then 1 end) as Results_Fail,
COUNT(case when [Result Approved by SSC] = 'No Value' then 1 end) as Results_NA,
SUM(case when [BankAcountNumber] IS NULL then 0 else 1 end) as Bank,
COUNT(case when [Certified] = 'Yes' then 1 end) as Certified,
COUNT(case when [Monetary Reward Tracker-NSDC Processed] = 'Yes' then 1 end) as NSDC_Certified,
SUM(case when [Monetary Reward Tracker-Disbursement List] = '0' then 0 else 1 end) as DL,
Capacity.[Total Target]
FROM PMKVY
INNER JOIN Capacity ON PMKVY.CentreID=Capacity.[Training Centre ID]
GROUP BY Capacity.[SPOC-Partner],Capacity.[Total Target]
Posted
Updated 28-Feb-16 7:06am
v2

A trigger is on the table itself, not a button to wire up on a form.

You create a trigger on the table using SQL commands.

You can look for inserts, updates and deletes from the table and write SQL to do things like log or synchronize with another table.

Here is a link to MSDN

CREATE TRIGGER (Transact-SQL)[^]

This example from the page is a good demonstration:

SQL
CREATE TRIGGER reminder2
ON Sales.Customer
AFTER INSERT, UPDATE, DELETE 
AS
   EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'AdventureWorks2012 Administrator',
        @recipients = 'danw@Adventure-Works.com',
        @body = 'Don''t forget to print a report for the sales force.',
        @subject = 'Reminder';
GO
 
Share this answer
 
Trigger: It is a database object which executes any DML(Insert, Update, Delete) operation occurs on a table. There are two types of triggers: Instead of trigger and After trigger. Here we will use after trigger means it executes when server completes an action. One more interesting point is magic tables: inserted and deleted. When any DML operation on a table(Table2) happens it executes a trigger. Then inside trigger you want to fetch what are the data inserted, here inserted table plays vital role likewise for deleted(when record deletes).

Following trigger object created on Table2. Means when a record will insert on Table2 then below object will execute.
SQL
-- Create trigger on table Table2 for Insert statement
CREATE TRIGGER trgAfterInsert on Table2
FOR INSERT
AS 

-- Get values from inserted table. Here Table2 is inserted table and you need to change as per your column name.
declare @empid int, @empname varchar(55);
select @empid=i.Column1 from inserted i;
select @empname=i.Column2 from inserted i;

INSERT INTO Table3
SELECT Capacity.[SPOC-Partner], 
COUNT(PMKVY.CentreID) as Candidate,
COUNT(case when [Aadhar Validation-Aadhar Number Validated] = 'Yes' then 1 end) as Validated,
COUNT(case when [Result Approved by SSC] = 'Pass' then 1 end) as Results_Pass,
COUNT(case when [Result Approved by SSC] = 'Fail' then 1 end) as Results_Fail,
COUNT(case when [Result Approved by SSC] = 'No Value' then 1 end) as Results_NA,
SUM(case when [BankAcountNumber] IS NULL then 0 else 1 end) as Bank,
COUNT(case when [Certified] = 'Yes' then 1 end) as Certified,
COUNT(case when [Monetary Reward Tracker-NSDC Processed] = 'Yes' then 1 end) as NSDC_Certified,
SUM(case when [Monetary Reward Tracker-Disbursement List] = '0' then 0 else 1 end) as DL,
Capacity.[Total Target]
FROM PMKVY
INNER JOIN Capacity ON PMKVY.CentreID=Capacity.[Training Centre ID]
GROUP BY Capacity.[SPOC-Partner],Capacity.[Total Target]

Suggestion: If you are not performing any data with Table2 inside trigger then you can write same code inside a stored procedure. Then call it when you are performing insert operation with Table2 table.
 
Share this answer
 
Comments
Member 10376341 29-Feb-16 2:56am    
Hey! thank you so much Manas Kumar, your code worked for me.
Cheers!

Thanks
Saiekrishna
[no name] 29-Feb-16 3:48am    
Welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900