[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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)
0
cheryl9063
Asked:
cheryl9063
2 Solutions
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
wdosanjosCommented:
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
 
Alpesh PatelAssistant 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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now