Mark Edwards
Mark Edwards

Reputation: 63

How to insert data from one column into another column in a different table

I have two tables LESSON & SPORT.

LESSONNO | SPORTNO | INSTRUCTORNO | DATE | PRICE |

SPORTNO | SPORT NAME | SPORTDESCRIPTION |

I need to add column SPORTNAME to the lesson table, and for its data to match the SPORTNO in lesson table as it does in sport table.

Thanks in advance!

Upvotes: 1

Views: 43

Answers (2)

Guillermo Zooby
Guillermo Zooby

Reputation: 612

This worked for me in SQL Server

 ALTER TABLE LESSON ADD SPORTNAME VARCHAR(50)

  UPDATE LESSON
  SET SPORTNAME = S.SPORTNAME
  FROM dbo.LESSON AS L
  INNER JOIN dbo.SPORT AS S
  ON L.SPORTNO = S.SPORTNO

Hope it helps!

Upvotes: 1

Sasha Pachev
Sasha Pachev

Reputation: 5336

Does this work for you:

ALTER TABLE LESSON ADD SPORTNAME VARCHAR(30); // or whatever type it is
UPDATE LESSON l JOIN SPORT s USING(SPORTNO) SET l.SPORTNAME = s.SPORTNAME;

?

Upvotes: 1

Related Questions