Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multi Table Query

Author  Topic 

temuri
Starting Member

8 Posts

Posted - 2011-03-30 : 17:42:45
HI! I need some help with query.

I have two tables;

Tab1;(userid,orgname,date)
Tab2;(userid,orgname,date)
Tab3;(userid,orgname,date)

I need to add user from Table1 to table2 and table3 if not exist by month.
system check if new user added to Table1; and automatically add different to Table2 and Table3 but in same date(year,month)

Is it possible to make ik by sql server, with scheduler or something

can someone help me ?

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2011-03-30 : 18:08:29
Can you provide us with sample data of what you'd like to accomplish?

Sounds like you'll need to create a trigger that adds users to Table2 and Table3 whenever a new user is inserted into Table1.

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

temuri
Starting Member

8 Posts

Posted - 2011-03-30 : 18:17:20
yap I think that I need make triger in sql server but i didn't know where to do this and also need help with Query

I need just add userid,orgname and date from table 1
Go to Top of Page

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2011-03-30 : 18:30:02
This should give you an idea on what you'd have to do:


USE YourDatabaseName
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'YourTriggerName' AND type = 'TR')
DROP TRIGGER YourTriggerName
GO

CREATE TRIGGER YourTriggerName
ON Table1
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT Table2
SELECT * FROM inserted
INSERT Table3
SELECT * FROM inserted
END
GO


Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

temuri
Starting Member

8 Posts

Posted - 2011-03-30 : 18:38:34
I will try thank you;
Go to Top of Page

temuri
Starting Member

8 Posts

Posted - 2011-03-31 : 19:39:00
It's works perfect!!

with INSERTED its mean table name or inserted data only ????
Go to Top of Page
   

- Advertisement -