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
  • 732,580
  • 175
  • 1,330
  • 1,459
Davor Zubak
  • 4,726
  • 13
  • 59
  • 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
  • 7,438
  • 14
  • 64
  • 92
2
UPDATE Cities SET County_Id = 20 WHERE Cities.Code >= 10000 AND Cities.Code < 20000
Eric H
  • 1,759
  • 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
  • 63,911
  • 12
  • 95
  • 141