How to perform a mass SQL insert to one table with rows from two seperate tables

Alan Fisher

I need some T-SQL help. We have an application which tracks Training Requirements assigned to each employee (such as CPR, First Aid, etc.). There are certain minimum Training Requirements which all employees must be assigned and my HR department wants me to give them the ability to assign those minimum Training Requirements to all personnel with the click of a button. So I have created a table called TrainingRequirementsForAllEmployees which has the TrainingRequirementID's of those identified minimum TrainingRequirements.

I want to insert rows into table Employee_X_TrainingRequirements for every employee in the Employees table joined with every row from TrainingRequirementsForAllEmployees. I will add abbreviated table schema for clarity.

First table is Employees:

EmployeeNumber  PK  char(6)
EmployeeName        varchar(50)

Second Table is TrainingRequirementsForAllEmployees:

TrainingRequirementID     PK  int

Third table (the one I need to Insert Into) is Employee_X_TrainingRequirements:

TrainingRequirementID  PK  int
EmployeeNumber         PK  char(6)

I don't know what the Stored Procedure should look like to achieve the results I need. Thanks for any help.

i-one

cross join operator is suitable when cartesian product of two sets of data is needed. So in the body of your stored procedure you should have something like:

insert into Employee_X_TrainingRequirements (TrainingRequirementID, EmployeeNumber)
select r.TrainingRequirementID, e.EmployeeNumber
from Employees e
    cross join TrainingRequirementsForAllEmployees r
where not exists (
    select 1 from Employee_X_TrainingRequirements
    where TrainingRequirementID = r.TrainingRequirementID
        and EmployeeNumber = e.EmployeeNumber
    )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL INSERT INTO two tables from one table

From Dev

Insert multiple rows from two tables into one table

From Dev

How to multiply SQL columns from 2 tables and insert into a seperate column?

From Dev

How can I merge rows and columns from two sql tables into one table

From Dev

How to make one table from two tables in SQL

From Dev

SQL: How to select multiple values from one table as seperate columns

From Dev

SQL - COUNT() rows for two tables that are GROUP BY one column in one table

From Dev

updating one table with the value of another table and the two tables are in seperate database

From Dev

Insert rows into a table from another two tables separately in mysql

From Dev

MYSQL: How to insert rows in a table based on a condition of other two tables

From Dev

Insert two columns from different tables into one table

From Dev

Insert 2 values from two different tables into one table

From Dev

Insert 2 values from two different tables into one table

From Dev

How to insert one row of a table into two rows of another table

From Dev

How do I insert to two tables in the same query and also use the identity from one destination table?

From Dev

Selecting rows absent from one table against two other tables?

From Java

How to fast return and count rows from one table based on filter by two another tables

From Dev

How to perform an SQL query from a table based on values in other tables?

From Dev

SQL INSERT from two tables

From Dev

SQL insert into table from tables

From Dev

How to combine multiple rows from 4 tables into one single row in a new table in SQL?

From Java

sql two where clauses from one table into two new rows

From Dev

how to Normalize the sql table in two tables or one detailed table

From Dev

How can i use SQL Select Insert to copy rows from one table to another

From Dev

How to use SQL TRIGGER to insert rows from another table into a new one?

From Dev

How can I insert data from two tables into one?

From Dev

MySQL Insert to two seperate tables with pivot

From Dev

How to count rows from two tables in one query?

From Dev

How to count rows from two tables in one query?

Related Related

  1. 1

    SQL INSERT INTO two tables from one table

  2. 2

    Insert multiple rows from two tables into one table

  3. 3

    How to multiply SQL columns from 2 tables and insert into a seperate column?

  4. 4

    How can I merge rows and columns from two sql tables into one table

  5. 5

    How to make one table from two tables in SQL

  6. 6

    SQL: How to select multiple values from one table as seperate columns

  7. 7

    SQL - COUNT() rows for two tables that are GROUP BY one column in one table

  8. 8

    updating one table with the value of another table and the two tables are in seperate database

  9. 9

    Insert rows into a table from another two tables separately in mysql

  10. 10

    MYSQL: How to insert rows in a table based on a condition of other two tables

  11. 11

    Insert two columns from different tables into one table

  12. 12

    Insert 2 values from two different tables into one table

  13. 13

    Insert 2 values from two different tables into one table

  14. 14

    How to insert one row of a table into two rows of another table

  15. 15

    How do I insert to two tables in the same query and also use the identity from one destination table?

  16. 16

    Selecting rows absent from one table against two other tables?

  17. 17

    How to fast return and count rows from one table based on filter by two another tables

  18. 18

    How to perform an SQL query from a table based on values in other tables?

  19. 19

    SQL INSERT from two tables

  20. 20

    SQL insert into table from tables

  21. 21

    How to combine multiple rows from 4 tables into one single row in a new table in SQL?

  22. 22

    sql two where clauses from one table into two new rows

  23. 23

    how to Normalize the sql table in two tables or one detailed table

  24. 24

    How can i use SQL Select Insert to copy rows from one table to another

  25. 25

    How to use SQL TRIGGER to insert rows from another table into a new one?

  26. 26

    How can I insert data from two tables into one?

  27. 27

    MySQL Insert to two seperate tables with pivot

  28. 28

    How to count rows from two tables in one query?

  29. 29

    How to count rows from two tables in one query?

HotTag

Archive