473,656 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UPDATE ORDER BY error

Hi I'm trying to update a db based on the select statement which has
ORDER BY in it.
And due to that I'm getting error which states that

Server: Msg 1033, Level 15, State 1, Line 13
The ORDER BY clause is invalid in views, inline functions, derived
tables, and subqueries, unless TOP is also specified

Here is my sql statement:

SELECT SUBSTRING(A.DES CR,1,10), B.SUPPORT_TEAM_ MBR, EMPLID, COUNT(*)
from PS_TEAM_CODE_TB L A, PS_TEAM_MEMBERS B, PS_MEMBER_PERSO N C
WHERE A.SUPPORT_TEAM_ CD = B.SUPPORT_TEAM_ CD
AND A.EFF_STATUS = 'A'
AND A.EFFDT >= (SELECT MAX(AX.EFFDT) FROM PS_TEAM_CODE_TB L AX
WHERE A.SETID = AX.SETID
AND A.SUPPORT_TEAM_ CD = AX.SUPPORT_TEAM _CD)
AND B.SUPPORT_TEAM_ MBR = C.SUPPORT_TEAM_ MBR
GROUP BY SUBSTRING(A.DES CR,1,10), B.SUPPORT_TEAM_ MBR, EMPLID
HAVING COUNT(*) > 1
ORDER BY SUBSTRING(A.DES CR,1,10)

What should I do to avoid this problem.

Thanks in advance for your help.

Jul 23 '05 #1
10 8781
Do you really need the order by clause if you are using this query as a
derived table? If so you can use 'top 100 percent' to appease the compiler.

Mr Tea

"sqlgoogle" <ri*********@gm ail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Hi I'm trying to update a db based on the select statement which has
ORDER BY in it.
And due to that I'm getting error which states that

Server: Msg 1033, Level 15, State 1, Line 13
The ORDER BY clause is invalid in views, inline functions, derived
tables, and subqueries, unless TOP is also specified

Here is my sql statement:

SELECT SUBSTRING(A.DES CR,1,10), B.SUPPORT_TEAM_ MBR, EMPLID, COUNT(*)
from PS_TEAM_CODE_TB L A, PS_TEAM_MEMBERS B, PS_MEMBER_PERSO N C
WHERE A.SUPPORT_TEAM_ CD = B.SUPPORT_TEAM_ CD
AND A.EFF_STATUS = 'A'
AND A.EFFDT >= (SELECT MAX(AX.EFFDT) FROM PS_TEAM_CODE_TB L AX
WHERE A.SETID = AX.SETID
AND A.SUPPORT_TEAM_ CD = AX.SUPPORT_TEAM _CD)
AND B.SUPPORT_TEAM_ MBR = C.SUPPORT_TEAM_ MBR
GROUP BY SUBSTRING(A.DES CR,1,10), B.SUPPORT_TEAM_ MBR, EMPLID
HAVING COUNT(*) > 1
ORDER BY SUBSTRING(A.DES CR,1,10)

What should I do to avoid this problem.

Thanks in advance for your help.

Jul 23 '05 #2
You say you are trying to do an UPDATE but what you posted was a SELECT
statement. I'm assuming that SELECT statement was used either as a subquery
or derived table in an UPDATE statement or as a view. In each case ORDER BY
isn't allowed. Remove the ORDER BY clause and it should work fine. You can't
have an ordered update.

--
David Portas
SQL Server MVP
--
Jul 23 '05 #3
David thanks for your response.
I using a simple update & here is the complete sql for that

UPDATE PS_MEMBER_PERSO N
SET NAME1 = 'Z' FROM PS_MEMBER_PERSO N WHERE NAME1 =
(SELECT SUBSTRING(A.DES CR,1,10), B.SUPPORT_TEAM_ MBR, EMPLID, COUNT(*)
from PS_TEAM_CODE_TB L A, PS_TEAM_MEMBERS B, PS_MEMBER_PERSO N C
WHERE A.SUPPORT_TEAM_ CD = B.SUPPORT_TEAM_ CD
AND A.EFF_STATUS = 'A'
AND A.EFFDT >= (SELECT MAX(AX.EFFDT) FROM PS_TEAM_CODE_TB L AX
WHERE A.SETID = AX.SETID
AND A.SUPPORT_TEAM_ CD = AX.SUPPORT_TEAM _CD)
AND B.SUPPORT_TEAM_ MBR = C.SUPPORT_TEAM_ MBR
GROUP BY SUBSTRING(A.DES CR,1,10), B.SUPPORT_TEAM_ MBR, EMPLID
HAVING COUNT(*) > 1
ORDER BY SUBSTRING(A.DES CR,1,10))

If I remove the ORDER BY then I'll get an error in subquery.
What do you think I should do to resolve that problem.

Thanks in advance for your help

Jul 23 '05 #4
Thanks Mr Tea for your response.

I have never used the top percent before. I couldn't find any solid
example that should explain how this works.
I guess I could try it if I know how to do it.
Do you have or know a sample or example that explain this in details.

Thanks

Jul 23 '05 #5
Your query is supposed to be a scalar subquery but it is returning more than
one column. That can't be. Also GROUP BY is pointless in a scalar subquery
because only one row must be returned. Finally, your subquery isn't
correlated with the UPDATE.

To help you properly I think we'll need more information. If you need more
help maybe you could post DDL (just the essential columns and the keys will
do), sample data INSERTs and show your required end result. See the
following article on how to do this:
http://www.aspfaq.com/etiquette.asp?id=5006

--
David Portas
SQL Server MVP
--
Jul 23 '05 #6
You can find an example in the books online although in this case it
probably means you are trying to do something maybe incorrect for what you
envisaged.

you could turn your select into a single column select and use UPDATE ...
WHERE x IN (subquery), dropping the order by clause from the subquery.
although this is just a guess as to what you are trying to acheive and you
should post the DDL as requested if you need a better review.

Regards
Mr Tea

"sqlgoogle" <ri*********@gm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Thanks Mr Tea for your response.

I have never used the top percent before. I couldn't find any solid
example that should explain how this works.
I guess I could try it if I know how to do it.
Do you have or know a sample or example that explain this in details.

Thanks

Jul 23 '05 #7
sqlgoogle (ri*********@gm ail.com) writes:
David thanks for your response.
I using a simple update & here is the complete sql for that

UPDATE PS_MEMBER_PERSO N
SET NAME1 = 'Z' FROM PS_MEMBER_PERSO N WHERE NAME1 =
(SELECT SUBSTRING(A.DES CR,1,10), B.SUPPORT_TEAM_ MBR, EMPLID, COUNT(*)
from PS_TEAM_CODE_TB L A, PS_TEAM_MEMBERS B, PS_MEMBER_PERSO N C
WHERE A.SUPPORT_TEAM_ CD = B.SUPPORT_TEAM_ CD
AND A.EFF_STATUS = 'A'
AND A.EFFDT >= (SELECT MAX(AX.EFFDT) FROM PS_TEAM_CODE_TB L AX
WHERE A.SETID = AX.SETID
AND A.SUPPORT_TEAM_ CD = AX.SUPPORT_TEAM _CD)
AND B.SUPPORT_TEAM_ MBR = C.SUPPORT_TEAM_ MBR
GROUP BY SUBSTRING(A.DES CR,1,10), B.SUPPORT_TEAM_ MBR, EMPLID
HAVING COUNT(*) > 1
ORDER BY SUBSTRING(A.DES CR,1,10))

If I remove the ORDER BY then I'll get an error in subquery.
What do you think I should do to resolve that problem.


So you got an error in the subquery, and then you thought "Ah, if
I add an ORDER BY to the subquery that error will go away!"?

Excuse me the sarcasm, but I have to confess that I can't really
understand how you could the idea that the above could work at all.
Exactly what is

NAME1 =
(SELECT SUBSTRING(A.DES CR,1,10), B.SUPPORT_TEAM_ MBR, EMPLID, COUNT(*)

intended to mean? You can't compare NAME1 to all four values at a
time anyway.

It seems that you are into some handling of duplicates, but I don't
feel like going into wild guesses of what you are doing. So I suggest
that you rewind, and gives us more information of the problem you
are trying to solve. The standard recommendation is to include:

o CREATE TABLE statements of the involved tables. Don't forget to
include information about keys.
o INSERT statements with sample data.
o The desired result given the sample.

Apparently quite a few tables are involved in your case, so it might be
a good idea to simplify the problem somewhat.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #8
Hi David thanks for your tips.
I don't have access to Ent. Mgr (due to rights) but I hope this will
help

Name Owner
Type

-----------------------------------------------------------------------------------------------------------------
------------------------------- --------------------------
PS_TEAM_CODE_TB L dbo user
table

Column_name Type
FixedLenNullInS ource

--------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------
SETID char
no
SUPPORT_TEAM_CD char no
EFFDT PSDATE
(n/a)
EFF_STATUS char
no
DESCR char
no
DESCRSHORT char
no
Identity Seed
Increment Not For Replication
--------------------------------------------------------------------------------------------------------------------------------
---------------------------------------- -- No identity column defined.
NULL NULL
NULL
RowGuidCol

--------------------------------------------------------------------------------------------------------------------------------

No rowguidcol column defined.
Data_located_on _filegroup

--------------------------------------------------------------------------------------------------------------------------------

PRIMARY

Index_keys
DESCR, SETID, SUPPORT_TEAM_CD , EFFDT
SETID, SUPPORT_TEAM_CD , EFFDT
No constraints have been defined for this object.

No foreign keys reference this table.
No views with schema binding reference this table.
Name Owner
Created_datetim e
--------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------
PS_TEAM_MEMBERS dbo
user table

Column_name Type Length
FixedLenNullInS ource
--------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------
SETID char 5 no
SUPPORT_TEAM_CD char 6 no
EFFDT PSDATE 8 no
SUPPORT_TEAM_MB R char 8 no
MANAGER char 1 no

PRIORITY smallint 2 (n/a)
Identity Seed
Increment Not For
Replication
--------------------------------------------------------------------------------------------------------------------------------
------------------------------------------- No identity column defined.
NULL NULL
NULL
RowGuidCol

--------------------------------------------------------------------------------------------------------------------------------

No rowguidcol column defined.
Data_located_on _filegroup

--------------------------------------------------------------------------------------------------------------------------------

PRIMARY
index_keys
PS_TEAM_MEMBERS

Name Owner Type

--------------------------------------------------------------------------------------------------------------------------------
------------------------------------------
PS_MEMBER_PERSO N dbo user table

Column_name Type Length
FixedLenNullInS ource
--------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------
SETID char
5 no
SUPPORT_TEAM_MB R char 8
no
EMPLID char 11
no
NAME1 char 40
no
TEAM_MEMBER_TYP E char 6
no
EFF_STATUS char 1
no
VENDOR_ID char 10
no
REGION_CD char 10
no
LANGUAGE_CD char 3
no
DEF_COMPCT_SRC char 4
no
QUOTA_TYPE char 1
no
COMM_PCT decimal 5
(n/a)
COMM_BKS decimal 5
(n/a)
COMM_SHP decimal 5
(n/a)
COMM_INV decimal 5
(n/a)
COMM_CASH decimal 5
(n/a)
EXT_EMAIL_ADDR char 70
no
CURRENCY_CD char 3
no
RowGuidCol

--------------------------------------------------------------------------------------------------------------------------------

No rowguidcol column defined.
Data_located_on _filegroup

--------------------------------------------------------------------------------------------------------------------------------

PRIMARY
I'm trying to put the sample data together & I'll post that later.

Thanks

Jul 23 '05 #9
sqlgoogle (ri*********@gm ail.com) writes:
Thanks for the tip Erland. I've tried that already & I'm getting login
error.
Which I'm guessing due to my access rights.

Any other way to get around or any other tip.


OK, it's only to start typing then!
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3263
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old scripts working on a new server with the most recent version of PHP. I've pretty much taken care of all the various errors that were popping up. Most only pointed out out non-fatal undefined or assumed variables. I've been able to cure most of...
4
10229
by: Surendra | last post by:
I have this query that I need to use in an Update statement to populate a field in the table by the value of Sq ---------------------------------------------------------------------------- Inline View Query: Select Sq from ( Select substr(to_date(End_Date,"DD-MON-YYYY"),4), End_Date, Rank() Over (Partition by substr(to_date(End_Date,"DD-MON-YYYY"),4) Order by End_Date) As Sq
6
61564
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the same structure as follows Keycolumns ========== Material
2
54456
by: champ.supernova | last post by:
Hi, I was hoping someone could help me with what I'm sure is a very simple problem...I just can't seem to find the syntax! I'm wanting to update the rows in 'tbl_consolidate' from 'tbl_hold', but working through the records in 'tbl_hold' in the order of dates in a date field, rather than the order that the rows are necessarily in. I came up with the following code to do this:
3
3441
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all the necessary records in it when testing it. I get the error "No value given for one or more required parameters." when I try to update the database. Can you tell me what am I doing wrong?
2
6544
by: D. Dante Lorenso | last post by:
I'm trying to build a table that will store a history of records by enumerating the records. I want the newest record to always be number ZERO, so I created a trigger on my table to handle the assignment of version numbers: CREATE TRIGGER "trg_audio_file_insert" BEFORE INSERT ON "public"."audio_file" FOR EACH ROW EXECUTE PROCEDURE "public"."trg_audio_file_insert"(); My trigger function looks like this...
3
2693
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Here is my loop and it runs fine: ---------------------------------------------------- sSQL = "SELECT * FROM STORE_ITEMS" Set DataRec = DB.execute(sSQL) if not DataRec.EOF then do while not DataRec.EOF SKU = trim (DataRec("SKU")) ITEM_ID = trim(DataRec("ITEM_ID")) ...
1
2456
by: teenagelcruise | last post by:
hi, i have a problem with my code which is i cannot update and addnew data into the database but i can delete the data.plz give me an idea.this is my code that i wrote. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Order Record</title> <meta name="Microsoft Border" content="tlb, default"> </head>
2
2876
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of various module that I found it useful. Here is the 1st problem I encounter: I had a function to edit a event row form the database which is fine with me, than I pass on the code to a function that save(update) the data to the database.
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.