开发者

How to join tables easily Microsoft SQL Server?

开发者 https://www.devze.com 2023-01-09 03:36 出处:网络
I have two separate tables used for categories. One is Categories(ID, Title, Description), and the other is SubCategories(ID, UpperID, Title, Description)

I have two separate tables used for categories.

One is Categories(ID, Title, Description), and the other is SubCategories(ID, UpperID, Title, Description)

I开发者_高级运维 want to insert the records from categories to SubCategories with upperID=0. I've looked at SQL SELECT INTO but don't know how to use it for existing tables.


Insert Into dbo.SubCategories (UpperId, Title, Description)

Select 0, Title, Description
From dbo.Categories

This assumes that the ID column in both tables is an Identity column and that the ID in Categories should not be transferred to the SubCategories table


INSERT INTO SubCategories(ID, UpperID, Title, Description)

SELECT ID, 0, Title, Description FROM Categories

assuming that Id is not Identity field.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号