How can I insert values into a SQL Server table, into column X, where column Y is Z?
INSERT INTO dbo.Cities (County_Id)
WHERE Code >= 10000 AND Code < 20000
VALUE 20
How can I insert values into a SQL Server table, into column X, where column Y is Z?
INSERT INTO dbo.Cities (County_Id)
WHERE Code >= 10000 AND Code < 20000
VALUE 20
First of all, it sounds like you are trying to do an UPDATE
, as INSERT
is used to add a whole new record in the table as opposed to updating one or more existing record(s).
You really should be using UPDATE-JOIN
instead of INSERT-SELECT
, if I understand your requirement correctly. This StackOverflow thread provides a good example/explanation.
UPDATE Cities SET County_Id = 20 WHERE Cities.Code >= 10000 AND Cities.Code < 20000
Insert dbo.Cities( County_Id )
Select Name
From dbo.Counties
Where Counties.Id = 20
And Exists (
Select 1
From dbo.Cities As C1
Where C1.County_Id = Counties.Name
And C1.Code >= 10000
And C1.Code < 20000
)