0

how to update my table, with class id from data sekolah? i have 2 table data sekolah and sekolah,

table data sekolah have field : class id, class name, and jumlah siswa

table sekolah have field : class id, medals, rank, and jumlah siswa

i want copy jumlah siswa from table sekolah to jumlah siswa table data sekolah with class id as link two table

i have tried this code, but not work

UPDATE [data sekolah]
SET [jumlah siswa] = [jumlah siswa]
FROM [sekolah]
WHERE [sekolah].[class id] = [data sekolah].[class id]
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
gufran
  • 111
  • 1
  • 14
  • 2
    You should look at [this question](http://stackoverflow.com/questions/9588423/sql-server-inner-join-when-updating) – Facundo La Rocca Sep 14 '16 at 01:14
  • Possible duplicate of [Update a table using JOIN in SQL Server?](http://stackoverflow.com/questions/1604091/update-a-table-using-join-in-sql-server) – Jeff Sep 14 '16 at 05:18

2 Answers2

5

Try using the following update join syntax:

UPDATE t1
SET [jumlah siswa] = t2.[jumlah siswa]
FROM [data sekolah] t1
INNER JOIN [sekolah] t2
    ON t1.[class id] = t2.[class id]
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
3

Try using JOIN

Like this:

UPDATE A
SET [jumlah siswa]= B.[jumlah siswa]
FROM [sekolah] A
INNER JOIN [sekolah] B
ON A.[sekolah].[class id] = B.[data sekolah].[class id];
Melchizedek
  • 1,057
  • 17
  • 29