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
 Insert Query Help!

Author  Topic 

amodi
Yak Posting Veteran

83 Posts

Posted - 2009-06-08 : 12:46:15
Hello friends,
I have four tables EmployeeTable, DepartmentTable, MessageTable and EmployeeMessageTable.

EmployeeTable:
EmployeeId
FK_DepartmentId --Relationship with PK_DepartmentId column of DepartmentTable.

DepartmentTable:
PK_DepartmentId
DepartmentName

MessageTable:
PK_MessageId
Message

EmployeeMessageTable:
EmployeeId
Fk_MessageId – Relationship with PK_MessageId column of MessageTable.

I want to write a insert query that will take DepartmentId say “1” and MessageId say “35” as input. If department “1” contains 10 employees then query should insert 10 records in “EmployeeMessageTable” as:

Insert into EmployeeMessageTable(EmployeeId,fk_MessageId) values(“Emp_1_department_1”,35)
Insert into EmployeeMessageTable(EmployeeId,fk_MessageId) values(“Emp_2_department_1”,35)


Insert into EmployeeMessageTable(EmployeeId,fk_MessageId) values(“Emp_10_department_1”,35)

Query example would be helpful.

Thanks.

raky
Aged Yak Warrior

767 Posts

Posted - 2009-06-09 : 02:11:55
Is this is what you want ??

Declare @departmentID INT, @MessageID INT
SELECT @departmentID = 1, @MessageID = 35


Insert into EmployeeMessageTable(EmployeeId,fk_MessageId)
SELECT DISTINCT
EmployeeID,@MessageID
FROM EmployeeTable
WHERE DepartmentId = @departmentID
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-09 : 02:14:46
please do not cross post. http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=127284


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

amodi
Yak Posting Veteran

83 Posts

Posted - 2009-06-09 : 02:33:42
Thanks RakyKH.

Go to Top of Page

amodi
Yak Posting Veteran

83 Posts

Posted - 2009-06-09 : 02:34:44
quote:
Originally posted by khtan

please do not cross post. http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=127284


KH
[spoiler]Time is always against us[/spoiler]






Thanks KH.
Excuse me for posting the above topic in many sections of this forum.

Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-06-09 : 03:19:27
quote:
Originally posted by amodi

Thanks RakyKH.





Welcome...
Go to Top of Page
   

- Advertisement -