Inserting SQL into the selected query

I have 2 tables.

Attendance

table with two columns and Records

table with 1 column

+----------------------+
|       Attendance     |
+----------------------+
| EmployeeID   | Hours |
+----------------------+ 

+--------------+
|   Records    |
+--------------+
| EmployeeID   |
+--------------+
|     1        |
|     2        |
|     3        |
+--------------+ 

      

I need a query that inserts all employeeID

of the tables Records

in employeeID

the table Attendance

with a value of 8 in the column Hours

.

Like this:

+----------------------+
|       Attendance     |
+----------------------+
| EmployeeID   | Hours |
|----------------------|
|      1       |   8   |
|      2       |   8   |
|      3       |   8   |
+----------------------+

      

I cannot understand the code I was looking for, so I ask :)

By the way, I am using SQL Server.

+3


source to share


2 answers


I recently made the request you want.

INSERT INTO Attendence (EmployeeID, Hours) 
    SELECT EmployeeID, 8 
    FROM Records 
    WHERE EmployeeID > 0

      

WHERE

condition for SELECT

, not for INSERT INTO

. This query will copy all tables EmployeeID

in Attendence

where EmployeeID

greater than 0

.



SELECT EmployeeID, 8 FROM Records

      

will return something like

(1,8),(2,8),(3,8)

      

+1


source


Just use:



INSERT INTO Attendance (EmployeeID, Hours) 
    SELECT EmployeeID, 8 FROM Records;

      

0


source







All Articles