 |
|
 |
|
Next: execute an oracle from sql server
|
| Author |
Message |
External

Since: Jan 11, 2008 Posts: 5
|
(Msg. 1) Posted: Fri Jan 11, 2008 6:40 am
Post subject: UPDATE query using a subquery Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
Hello everyone,
I have searched but couldn't find something similiar to my issue.
This is my table structure:
CREATE TABLE [CARS] (
[id] [int] NOT NULL ,
[name] [char] (10),
[brand] [char] (10),
[model] [char] (10),
CONSTRAINT [PK_CARS] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
) ON [PRIMARY]
GO
I want to perform an UPDATE on the [CARS].[brand] column. The logic
behind the update is that the brand depends on the the model of the
car.
To do the update, I first have to query another table:
SELECT A.model, B.brand
FROM ...
etc.
(the result gives 10 unique models and 3 unique brands - each model
belongs to one of the 3 brands)
How do I now incorporate this (sub-)query into the UPDATE query? I.E.
How do I update [CARS].[brand] depending on the result of the query?
Many thanks in advance! >> Stay informed about: UPDATE query using a subquery |
|
| Back to top |
|
 |  |
External

Since: Jan 10, 2008 Posts: 640
|
(Msg. 2) Posted: Fri Jan 11, 2008 6:58 am
Post subject: Re: UPDATE query using a subquery [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jan 11, 8:40 am, Jean wrote:
> Hello everyone,
>
> I have searched but couldn't find something similiar to my issue.
>
> This is my table structure:
> CREATE TABLE [CARS] (
> [id] [int] NOT NULL ,
> [name] [char] (10),
> [brand] [char] (10),
> [model] [char] (10),
> CONSTRAINT [PK_CARS] PRIMARY KEY CLUSTERED
> (
> [id]
> ) ON [PRIMARY]
> ) ON [PRIMARY]
> GO
>
> I want to perform an UPDATE on the [CARS].[brand] column. The logic
> behind the update is that the brand depends on the the model of the
> car.
>
> To do the update, I first have to query another table:
>
> SELECT A.model, B.brand
> FROM ...
> etc.
> (the result gives 10 unique models and 3 unique brands - each model
> belongs to one of the 3 brands)
>
> How do I now incorporate this (sub-)query into the UPDATE query? I.E.
> How do I update [CARS].[brand] depending on the result of the query?
>
> Many thanks in advance!
UPDATE Cars SET Brand = (SELECT Brand FROM OtherTable WHERE <your join
condition here>) >> Stay informed about: UPDATE query using a subquery |
|
| Back to top |
|
 |  |
External

Since: Jan 11, 2008 Posts: 5
|
(Msg. 3) Posted: Fri Jan 11, 2008 7:14 am
Post subject: Re: UPDATE query using a subquery [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jan 11, 3:58 pm, Alex Kuznetsov wrote:
> On Jan 11, 8:40 am, Jean wrote:
>
>
>
>
>
> > Hello everyone,
>
> > I have searched but couldn't find something similiar to my issue.
>
> > This is my table structure:
> > CREATE TABLE [CARS] (
> > [id] [int] NOT NULL ,
> > [name] [char] (10),
> > [brand] [char] (10),
> > [model] [char] (10),
> > CONSTRAINT [PK_CARS] PRIMARY KEY CLUSTERED
> > (
> > [id]
> > ) ON [PRIMARY]
> > ) ON [PRIMARY]
> > GO
>
> > I want to perform an UPDATE on the [CARS].[brand] column. The logic
> > behind the update is that the brand depends on the the model of the
> > car.
>
> > To do the update, I first have to query another table:
>
> > SELECT A.model, B.brand
> > FROM ...
> > etc.
> > (the result gives 10 unique models and 3 unique brands - each model
> > belongs to one of the 3 brands)
>
> > How do I now incorporate this (sub-)query into the UPDATE query? I.E.
> > How do I update [CARS].[brand] depending on the result of the query?
>
> > Many thanks in advance!
>
> UPDATE Cars SET Brand = (SELECT Brand FROM OtherTable WHERE <your join
> condition here>)- Hide quoted text -
>
> - Show quoted text -
Sorry, but this is not what I am looking for. I should not have to
modify the subquery - it contains the results that I want already.
The subquery (say SUBQ) results look like this:
MODEL BRAND
----------- ----------
3 BMW
5 BMW
7 BMW
C Mercedes
S Mercedes
A Mercedes
A4 Audi
A6 Audi
A8 Audi
911 Porsche
I need to update [CARS].[brand] by comparing at [CARS].[model] with
[SUBQ].[MODEL] and getting the value for [SUBQ].[BRAND]
Any ideas? It seems so simple but i just don't get it! >> Stay informed about: UPDATE query using a subquery |
|
| Back to top |
|
 |  |
External

Since: Jan 11, 2008 Posts: 3
|
(Msg. 4) Posted: Fri Jan 11, 2008 7:44 pm
Post subject: Re: UPDATE query using a subquery [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Hi Jean,
Alex's solution is short, and should work well:
UPDATE cars SET cars.brand = (
SELECT ot.brand
FROM other_table ot
WHERE ot.model = cars.model
)
If you really must preserve your subquery, you could do an inner join:
UPDATE c
SET c1.brand = c2.brand
FROM cars c1, (<your subquery>) c2
WHERE c1.model = c2.model
Regards,
=========
Steve
www.stkomp.com
Jean wrote:
> On Jan 11, 3:58�pm, Alex Kuznetsov wrote:
> > On Jan 11, 8:40 am, Jean wrote:
> >
> >
> >
> >
> >
> > > Hello everyone,
> >
> > > I have searched but couldn't find something similiar to my issue.
> >
> > > This is my table structure:
> > > CREATE TABLE [CARS] (
> > > [id] [int] NOT NULL ,
> > > [name] [char] (10),
> > > [brand] [char] (10),
> > > [model] [char] (10),
> > > CONSTRAINT [PK_CARS] PRIMARY KEY CLUSTERED
> > > (
> > > [id]
> > > )ON [PRIMARY]
> > > ) ON [PRIMARY]
> > > GO
> >
> > > I want to perform an UPDATE on the [CARS].[brand] column. The logic
> > > behind the update is that the brand depends on the the model of the
> > > car.
> >
> > > To do the update, I first have to query another table:
> >
> > > SELECT A.model, B.brand
> > > FROM ...
> > > etc.
> > > (the result gives 10 unique models and 3 unique brands - each model
> > > belongs to one of the 3 brands)
> >
> > > How do I now incorporate this (sub-)query into the UPDATE query? I.E.
> > > How do I update [CARS].[brand] depending on the result of the query?
> >
> > > Many thanks in advance!
> >
> > UPDATE Cars SET Brand = (SELECT Brand FROM OtherTable WHERE <your join
> > condition here>)- Hide quoted text -
> >
> > - Show quoted text -
>
> Sorry, but this is not what I am looking for. I should not have to
> modify the subquery - it contains the results that I want already.
>
> The subquery (say SUBQ) results look like this:
> MODEL BRAND
> ----------- ----------
> 3 BMW
> 5 BMW
> 7 BMW
> C Mercedes
> S Mercedes
> A Mercedes
> A4 Audi
> A6 Audi
> A8 Audi
> 911 Porsche
>
> I need to update [CARS].[brand] by comparing at [CARS].[model] with
> [SUBQ].[MODEL] and getting the value for [SUBQ].[BRAND]
>
> Any ideas? It seems so simple but i just don't get it! >> Stay informed about: UPDATE query using a subquery |
|
| Back to top |
|
 |  |
External

Since: Jan 11, 2008 Posts: 5
|
(Msg. 5) Posted: Tue Jan 15, 2008 5:17 am
Post subject: Re: UPDATE query using a subquery [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jan 12, 4:44Â am, wisc....RemoveThis@googlemail.com wrote:
> Hi Jean,
>
> Alex's solution is short, and should work well:
>
> UPDATE cars SET cars.brand = (
> Â SELECT ot.brand
> Â FROM other_table ot
> Â WHERE ot.model = cars.model
> )
>
> If you really must preserve your subquery, you could do an inner join:
>
> UPDATE c
> SET c1.brand = c2.brand
> FROM cars c1, (<your subquery>) c2
> WHERE c1.model = c2.model
>
> Regards,
> =========
> Stevewww.stkomp.com
>
>
>
> Jean wrote:
> > On Jan 11, 3:58�pm, Alex Kuznetsov wrote:
> > > On Jan 11, 8:40 am, Jean wrote:
>
> > > > Hello everyone,
>
> > > > I have searched but couldn't find something similiar to my issue.
>
> > > > This is my table structure:
> > > > CREATE TABLE [CARS] (
> > > > Â [id] [int] NOT NULL ,
> > > > Â [name] [char] (10),
> > > > Â [brand] [char] (10),
> > > > Â [model] [char] (10),
> > > > Â CONSTRAINT [PK_CARS] PRIMARY KEY CLUSTERED
> > > > Â (
> > > > Â [id]
> > > > Â )ON [PRIMARY]
> > > > ) ON [PRIMARY]
> > > > GO
>
> > > > I want to perform an UPDATE on the [CARS].[brand] column. The logic
> > > > behind the update is that the brand depends on the the model of the
> > > > car.
>
> > > > To do the update, I first have to query another table:
>
> > > > SELECT A.model, B.brand
> > > > FROM ...
> > > > etc.
> > > > (the result gives 10 unique models and 3 unique brands - each model
> > > > belongs to one of the 3 brands)
>
> > > > How do I now incorporate this (sub-)query into the UPDATE query? I.E..
> > > > How do I update [CARS].[brand] depending on the result of the query?
>
> > > > Many thanks in advance!
>
> > > UPDATE Cars SET Brand = (SELECT Brand FROM OtherTable WHERE <your join
> > > condition here>)- Hide quoted text -
>
> > > - Show quoted text -
>
> > Sorry, but this is not what I am looking for. I should not have to
> > modify the subquery - it contains the results that I want already.
>
> > The subquery (say SUBQ) results look like this:
> > MODEL Â Â BRAND
> > ----------- Â Â ----------
> > 3 Â Â Â Â Â Â BMW
> > 5 Â Â Â Â Â Â BMW
> > 7 Â Â Â Â Â Â BMW
> > C Â Â Â Â Â Â Mercedes
> > S Â Â Â Â Â Â Mercedes
> > A Â Â Â Â Â Â Mercedes
> > A4 Â Â Â Â Â Audi
> > A6 Â Â Â Â Â Audi
> > A8 Â Â Â Â Â Audi
> > 911 Â Â Â Â Porsche
>
> > I need to update [CARS].[brand] by comparing at [CARS].[model] with
> > [SUBQ].[MODEL] and getting the value for [SUBQ].[BRAND]
>
> > Any ideas? It seems so simple but i just don't get it!- Hide quoted text -
>
> - Show quoted text -
Thanks, I got it now! >> Stay informed about: UPDATE query using a subquery |
|
| Back to top |
|
 |  |
External

Since: Jul 21, 2011 Posts: 1
|
(Msg. 6) Posted: Thu Jul 21, 2011 8:25 pm
Post subject: Re: Re: UPDATE query using a subquery [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
serveur generer cle mercedes
> On Saturday, January 12, 2008 7:56 AM Jean wrote:
> Hello everyone,
>
> I have searched but couldn't find something similiar to my issue.
>
> This is my table structure:
> CREATE TABLE [CARS] (
> [id] [int] NOT NULL ,
> [name] [char] (10),
> [brand] [char] (10),
> [model] [char] (10),
> CONSTRAINT [PK_CARS] PRIMARY KEY CLUSTERED
> (
> [id]
> ) ON [PRIMARY]
> ) ON [PRIMARY]
> GO
>
> I want to perform an UPDATE on the [CARS].[brand] column. The logic
> behind the update is that the brand depends on the the model of the
> car.
>
> To do the update, I first have to query another table:
>
> SELECT A.model, B.brand
> FROM ...
> etc.
> (the result gives 10 unique models and 3 unique brands - each model
> belongs to one of the 3 brands)
>
> How do I now incorporate this (sub-)query into the UPDATE query? I.E.
> How do I update [CARS].[brand] depending on the result of the query?
>
> Many thanks in advance!
>> On Saturday, January 12, 2008 7:56 AM Alex Kuznetsov wrote:
>> UPDATE Cars SET Brand = (SELECT Brand FROM OtherTable WHERE <your join
>> condition here>)
>>> On Saturday, January 12, 2008 7:56 AM Jean wrote:
>>> On Jan 11, 3:58=A0pm, Alex Kuznetsov wrote:
>>>
>>> Sorry, but this is not what I am looking for. I should not have to
>>> modify the subquery - it contains the results that I want already.
>>>
>>> The subquery (say SUBQ) results look like this:
>>> MODEL BRAND
>>> ----------- ----------
>>> 3 BMW
>>> 5 BMW
>>> 7 BMW
>>> C Mercedes
>>> S Mercedes
>>> A Mercedes
>>> A4 Audi
>>> A6 Audi
>>> A8 Audi
>>> 911 Porsche
>>>
>>> I need to update [CARS].[brand] by comparing at [CARS].[model] with
>>> [SUBQ].[MODEL] and getting the value for [SUBQ].[BRAND]
>>>
>>> Any ideas? It seems so simple but i just don't get it!
>>>> On Saturday, January 12, 2008 7:57 AM wiscca wrote:
>>>> Hi Jean,
>>>>
>>>> Alex's solution is short, and should work well:
>>>>
>>>> UPDATE cars SET cars.brand =3D (
>>>> SELECT ot.brand
>>>> FROM other_table ot
>>>> WHERE ot.model =3D cars.model
>>>> )
>>>>
>>>> If you really must preserve your subquery, you could do an inner join:
>>>>
>>>> UPDATE c
>>>> SET c1.brand =3D c2.brand
>>>> FROM cars c1, (<your subquery>) c2
>>>> WHERE c1.model =3D c2.model
>>>>
>>>> Regards,
>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D
>>>> Steve
>>>> www.stkomp.com
>>>>
>>>> Jean wrote:
>>>>> On Tuesday, January 15, 2008 10:53 PM Jean wrote:
>>>>> .
>>>>>
>>>>> in
>>>>> -
>>>>>
>>>>> Thanks, I got it now! >> Stay informed about: UPDATE query using a subquery |
|
| Back to top |
|
 |  |
| Related Topics: | Update with Subquery - I have a table with columns Phone1 and Phone2. I want to update all Phone2 values with the value in Phone1 for all records where Phone1 is not null or Phone2 value is null. Begin Tran Update TABLE Set Phone2 = Phone1 Where Phone1 is not null and..
Update query help - HI All, I'm stuck on an update query and am hoping someone can help me out. Here's what I'd like it to do: UPDATE table1 set table1.field1 = table1.field1+' - '+table1.field2 where count(table1.field1)>1 Thanks for any help you can..
Update query - Im trying to run an Update query that has a Criteria in it such as (tblZHoldOrderDetails_View.LOCATION IS NOT NULL) but what Im having a problem with is that I would like to make sure that the same field is not blank. For example in MS Access I could..
Update Query Help - Hi! I've got a SELECT query which I need to convert to UPDATE statement. I'm need to update a set of data which is select below in table CatalogueItem for field BaseProductId with value selected in [Base_2008]. And I'm not sure how to do it! CatItem2007...
help for update query - Hi, I'm trying to do an update on a table, but I'm confronted to a difficult query. My tables: [Flag] { FlagID, Description } [FlagData] { FlagDataID, FlagID, DataDesc, DataValueStr} An example : Flag : FlagID = 104 Description = 'Martin Smyth'... |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|