SQL Trigger help

Im new to writing triggers... I need help writing a trigger on a table called Login.. If a new login is created(incrementing field called loginID is created when a new record is inserted) then a table called action is checked to see if that login exist.. If that login does not exist then it will insert a record into the table called Action with the new LoginID and action(I can get this part)
LVL 1
cheryl9063Asked:
Who is Participating?
 
wdosanjosConnect With a Mentor Commented:
It should be something like this:

CREATE TRIGGER LoginTrigger 
   ON  [LoginTable]
   AFTER INSERT
AS 
BEGIN
	SET NOCOUNT ON;

	IF NOT EXISTS (SELECT 1 FROM [ACTION] INNER JOIN INSERTED ON [ACTION].LOGINCOLUMN = INSERTED.LOGINCOLUMN) BEGIN
		INSERT INTO [ACTION] (LOGINID, LOGINCOLUMN) 
			SELECT LOGINID, LOGINCOLUMN FROM INSERTED
	END
END

Open in new window

0
 
Ephraim WangoyaCommented:

I'm not sure I understand you totally

<If a new login is created(incrementing field called loginID is created when a new record is inserted) then a table called action is checked to see if that login exist>

If its a new login, how can the data already exist in the action table
0
 
Ephraim WangoyaCommented:

try this, you can modify it to fit your columns
CREATE TRIGGER trigLogin ON Login
FOR INSERT 
AS
DECLARE @NewLoginID int
SELECT @NewLoginID = (SELECT LoginID FROM INSERTED)
insert into [action](LoginID, LoginAction)
values(@NewLoginID, 'your action')

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
cheryl9063Author Commented:
from another process.. That is not important.. I need  help writing a trigger that inserts a new record into table Action when a table Login gets a new record
0
 
Alpesh PatelConnect With a Mentor Assistant ConsultantCommented:
Create Trigger TRriggerName on Table name for Insert
AS
Begin

if not exists (Select 1 from inserted)
select col1, col2 into table2 from inserted.

End
0
 
cheryl9063Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.