Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Update the same column with different values using 1 query.

Posted on 2007-11-21
5
Medium Priority
?
2,807 Views
Last Modified: 2013-12-24
Hello,

I just wondered if there was a way I could update two records in the same column with different values with the same query?

I have attached a code snippet to demonstrate what I mean.

At the moment I have two seperate queries updating two records. Is there a way perhaps two just doe this as 1 query instead of two?

Thanks in advance

Chris
<cfquery name="moveRecordDown" datasource="#dsl#">
	UPDATE menus SET
	
		sortedBy = #recordSort#
 
	WHERE id = (SELECT id FROM menus WHERE menuTypeID = #recordGroup# AND sortedBy = #previousRecordSort#)
</cfquery>
 
<cfquery name="moveRecordDown" datasource="#dsl#">
	UPDATE menus SET
	
		sortedBy = #previousRecordSort#
	
	WHERE id = #recordID#
</cfquery>

Open in new window

0
Comment
Question by:chrissp26
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 18

Accepted Solution

by:
Plucka earned 2000 total points
ID: 20326529
No it's not possible. You can only update multiple records in one query to set the same colum value.

You can however put multiple SQL statements in a single CFQUERY
<cfquery name="moveRecordDown" datasource="#dsl#">
	UPDATE menus SET
	
		sortedBy = #recordSort#
 
	WHERE id = (SELECT id FROM menus WHERE menuTypeID = #recordGroup# AND sortedBy = #previousRecordSort#)
;
	UPDATE menus SET
	
		sortedBy = #previousRecordSort#
	
	WHERE id = #recordID#
</cfquery>

Open in new window

0
 
LVL 2

Author Comment

by:chrissp26
ID: 20326550
Brilliant! that helps a great deal. I take it the overhead of doing multiple statements in a single query is less than that of doing two seperate queries?

The only problem is the snippet you kindly attached doesn't seem to work. I get the following error:

Characters found after end of SQL statement.

If I remove the semi colon ; from the query qhich I assumed was causing the error then I get another error:

Syntax error (missing operator) in query expression 'id = (SELECT id FROM menus WHERE menuTypeID = 18 AND sortedBy = 8) UPDATE menus SET sortedBy = 8 WHERE id = 9'.

Thank you for your help and the very quick response
0
 
LVL 2

Author Comment

by:chrissp26
ID: 20326616
Nevermind, I just did a search and found that you cannot run multiple statements in a single query using MS Access.

Thanks for your help

Chris
0
 
LVL 52

Expert Comment

by:_agx_
ID: 20330371
> Is there a way perhaps two just doe this as 1 query instead of two?

Technicially it is possible.  You could a CASE statement (or whatever Access's equivalent is).  Use an IN (..) clause find only the two ID's you want. Then use CASE to update the value depending on the record ID.

--- omitted cfqueryparam for clarity
UPDATE  YourTable
SET    TheColumn = CASE WHEN ID = #TheFirstID# THEN #value1# ELSE #value2# END
WHERE  ID IN (#TheFirstID#, #TheSecondID#)

Though I would probably just stick with the separate queries in this case.
0
 
LVL 2

Author Comment

by:chrissp26
ID: 20333162
Cool. Thanks very much for the additional information.

Chris
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question