1

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
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Davor Zubak
  • 4,696
  • 13
  • 56
  • 94

3 Answers3

2

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.

Community
  • 1
  • 1
Web User
  • 6,996
  • 13
  • 57
  • 86
2
UPDATE Cities SET County_Id = 20 WHERE Cities.Code >= 10000 AND Cities.Code < 20000
Eric H
  • 1,661
  • 1
  • 11
  • 14
1
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
                )
Thomas
  • 62,492
  • 11
  • 93
  • 137