April 4, 2008 at 8:53 am
I am trying to write an update query with this as the basic concept:
UPDATE Table1
SET FlagEntity = 0
WHERE (ID = 1) or (ID =2)
I get an Execution error stating that the subquery cannot return more than one value if I use =. How do I get it to allow for this? I also tried putting in a subquery with exists.
April 4, 2008 at 9:00 am
What subquery? The code you posted doesn't have a sub-query, and an UPDATE statement cannot be a subquery.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
April 4, 2008 at 9:01 am
If you are using a sub query you want to use IN like this:
Update TableA
Set ColumnA = 'Test'
Where
ColumnB In (Select ColumnB From TableB Where ColumnA = 1)
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
April 4, 2008 at 12:50 pm
Thanks.
How about if I want to do that for multiple records in one update statement. Will I need a loop or can I use an OR?
April 4, 2008 at 12:52 pm
I know that code doesn't have a subquery, I stated that I also tried a subquery using where exists. I have done something like this in Oracle, but never with an Or in the statement.
What I really want to know is if it is even possible to run one update query to update multiple distinct rows or if I need to do a loop.
April 4, 2008 at 12:58 pm
Could you post some schema info so we can be little clearer on what you want?
You can certainly do:
Update TableA
Set columnA = 1
Where
ColumnB In (Select ColumnA From TableB) Or
ColumnC In (Select ColumnA From TableC)
If that is what you are asking. BTW-ColumnC could just as easily be another criteria on ColumnB.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
April 4, 2008 at 1:18 pm
lersner (4/4/2008)
Thanks.How about if I want to do that for multiple records in one update statement. Will I need a loop or can I use an OR?
A single update query can update all of the rows in a table if you so desire. The fact that you're getting errors as to sub-queries is simply telling that a. you're using them, and b. you're using them in such a way that they would require a single value.
Examples:
update table
set datecol=getdate()
would update EVERY row in the table to the current date. If you wanted to be more specific in your update:
update table
set datecol=getdate()
where ID in (1,2,3)
or
update table
set datecol=getdate()
where ID in (select ID from MyOtherTable where dateprocessed='01/01/2006'
Multiple rows isn't an issue.
How about you post what you've tried so far so that we can help you with the specific problem?
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
April 5, 2008 at 7:32 am
Thanks I am going to try the where id in (1,2,3).
April 5, 2008 at 7:40 am
Thanks Matt - the Where in clause worked perfectly! I really appreciate your help.
April 5, 2008 at 11:00 am
Something is very wrong here... the original query posted by the OP works just fine.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 5, 2008 at 11:57 am
Jeff Moden (4/5/2008)
Something is very wrong here... the original query posted by the OP works just fine.
Agreed - it seems that we never got to see the actual offending code. But hey - something has apparently helped the OP....
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
April 6, 2008 at 8:59 am
When both id's are found the code does not work. SQL Server showed an error when both conditions were true. So the code did not work if there was both an id of 1 and an id of 2. That is the actual offending code.
April 6, 2008 at 9:32 am
Since it worked fine for both Matt and I, I've got to ask... are you actually using SQL Server or are you using some other RDBMS?
--Jeff Moden
Change is inevitable... Change for the better is not.
April 9, 2008 at 5:31 am
lersner (4/4/2008)
I am trying to write an update query with this as the basic concept:UPDATE Table1
SET FlagEntity = 0
WHERE (ID = 1) or (ID =2)
I get an Execution error stating that the subquery cannot return more than one value if I use =. How do I get it to allow for this? I also tried putting in a subquery with exists.
hi
You can try this
UPDATE Table1
SET FlagEntity = 0
WHERE ID IN (1,2)
even your code also work fine. as others asked you provide clear requirement.
April 9, 2008 at 6:08 am
I am using SQL Server 2005.
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply