SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combine queries
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

duf
Starting Member

37 Posts

Posted - 06/06/2012 :  17:27:35  Show Profile  Reply with Quote
How to join such queries in one query:
a = SELECT ID FROM CN WHERE KOD='123456';
UPDATE OPIS SET OPIS='TEXT' WHERE KOD=a;

Thanks.

tkizer
Almighty SQL Goddess

USA
35020 Posts

Posted - 06/06/2012 :  17:29:35  Show Profile  Visit tkizer's Homepage  Reply with Quote
UPDATE OPIS SET OPIS='TEXT' WHERE KOD=(SELECT ID FROM CN WHERE KOD='123456')


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

duf
Starting Member

37 Posts

Posted - 06/06/2012 :  18:27:59  Show Profile  Reply with Quote
And in this case:
a = SELECT ID FROM CN WHERE KOD=123456';
UPDATE CN SET KOD='000000' WHERE ID = a;
At the same time the value is collected and update in the same table in the same query.
UPDATE CN SET KOD='000000' WHERE ID=(SELECT ID FROM CN WHERE KOD=123456');
This query throws an error;
Go to Top of Page

vijays3
Constraint Violating Yak Guru

India
311 Posts

Posted - 06/06/2012 :  19:57:58  Show Profile  Reply with Quote
quote:
Originally posted by duf

And in this case:
a = SELECT ID FROM CN WHERE KOD=123456';
UPDATE CN SET KOD='000000' WHERE ID = a;
At the same time the value is collected and update in the same table in the same query.
UPDATE CN SET KOD='000000' WHERE ID=(SELECT ID FROM CN WHERE KOD=123456');
This query throws an error;



What error it is giving?

It seems you have missed single qoute

UPDATE CN SET KOD='000000' WHERE ID=(SELECT ID FROM CN WHERE KOD='123456');

Vijay is here to learn something from you guys.

Edited by - vijays3 on 06/06/2012 20:01:40
Go to Top of Page

duf
Starting Member

37 Posts

Posted - 06/07/2012 :  01:48:18  Show Profile  Reply with Quote
quote:
Originally posted by vijays3

quote:
Originally posted by duf

And in this case:
a = SELECT ID FROM CN WHERE KOD=123456';
UPDATE CN SET KOD='000000' WHERE ID = a;
At the same time the value is collected and update in the same table in the same query.
UPDATE CN SET KOD='000000' WHERE ID=(SELECT ID FROM CN WHERE KOD=123456');
This query throws an error;



What error it is giving?




Error: You can't specify target table 'CN' for update in FROM clause'.
Go to Top of Page

vijays3
Constraint Violating Yak Guru

India
311 Posts

Posted - 06/07/2012 :  08:13:42  Show Profile  Reply with Quote
quote:
Originally posted by duf

quote:
Originally posted by vijays3

quote:
Originally posted by duf

And in this case:
a = SELECT ID FROM CN WHERE KOD=123456';
UPDATE CN SET KOD='000000' WHERE ID = a;
At the same time the value is collected and update in the same table in the same query.
UPDATE CN SET KOD='000000' WHERE ID=(SELECT ID FROM CN WHERE KOD=123456');
This query throws an error;



What error it is giving?




Error: You can't specify target table 'CN' for update in FROM clause'.



I tried here with sample data and this working 



create table cn (id int,kod varchar(20))

insert into cn(id,kod)
select 1,'123456'
union all
select 1,null
union all 
select 1,null





UPDATE CN SET KOD='000000' WHERE ID=(SELECT ID FROM CN WHERE KOD='123456')


SELECT * FROM CN

If possible can you provide sample data of your table 
so that we can try to produce same error here.


Are you using SQL server or MYSQL ?

I guess you are using MYSQL then go through

http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/



Vijay is here to learn something from you guys.

Edited by - vijays3 on 06/07/2012 08:18:19
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.42 seconds. Powered By: Snitz Forums 2000