3

I have 2 tables.

Attendance table with 2 columns and Records table with 1 column

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

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

I want a query that will insert all employeeID from Records table into EmployeeID in the Attendance table with a value of 8 in Hours column.

Like this:

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

I can't understand the code that I searched so I end up asking :)

By the way, I'm using SQL Server.

2 Answers 2

1

Recently, I have made a query like that you want.

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

WHERE condition is for SELECT, not for INSERT INTO. This query will copy all EmployeeID's to Attendence table where EmployeeID is greater than 0.

SELECT EmployeeID, 8 FROM Records

will return something like

(1,8),(2,8),(3,8)
Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand the WHERE 1. I got an error "An expression of non-boolean type specified in a context where a condition is expected, near 'SET'". But if I remove WHERE 1, I got no problem. Thanks to you anyway.
WHERE 1 means that there is no specific condition for the SELECTed rows. Of course you can delete it.
Im using MSSQL. I guess WHERE 1 is only on mySql? Correct me if i'm wrong. Thanks again.
0

Just use:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.