Unix Technical Forum

Select Max() not working

This is a discussion on Select Max() not working within the SQL Server forums, part of the Microsoft SQL Server category; --> I'm trying to run a select max() on a primary key/unique/non-identity column then + 1, all while running an ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-12-2008, 05:22 PM
Brian
 
Posts: n/a
Default Select Max() not working

I'm trying to run a select max() on a primary key/unique/non-identity
column then + 1, all while running an insert into.. select statement.
For some reason, the select max isn't quite doing the trick as I get a
primary key constraint error (MSG 2627) in SQL server!

It's probably something simple like adding quotes or parenthesis or
something, but I've tried...

Here's my code:

INSERT INTO [frameinventory]
([frameid]
,[framenumber]
,[framename]
,[colornumber]
,[a]
,[dbl]
,[templelength]
,[b]
,[ed]
,[cost]
,[retailprice]
,[upccode]
,[eyesize]
,[bridgesize]
,[groupcost]
,[colordescription]
,[lenscolor]
,[lenscolorcode]
,[circumference]
,[edangle]
,[frontprice]
,[halftemplesprice]
,[templesprice]
,[manufacturername]
,[brandname]
,[collectionname]
,[gendertype]
,[agegroup]
,[activestatus]
,[productgroupname]
,[rimtype]
,[material]
,[frameshape]
,[country]
,[yearintroduced]
,[upccode_type])
select (select max(frameid) + 1 from frameinventory), fpc, stylename,
colorcode, a, dbl, temple, b, ed, completeprice,
((framesdata.completeprice*1600)+5), upc, eye, bridge, completeprice,
colordescription, lenscolor, lenscolorcode, circumference, edangle,
frontprice, halftemplesprice, templesprice, manufacturername,
brandname, collectionname, gendertype, agegroup, 'Active',
productgroupname, rimtype, material, frameshape, country,
yearintroduced, 'UPC'
from framesdata
where not exists (select * from frameinventory where
frameinventory.upccode=framesdata.upc)


THANKS!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-12-2008, 05:22 PM
David Portas
 
Posts: n/a
Default Re: Select Max() not working

"Brian" <eyeman@gmail.com> wrote in message
news:7647a16a-07c8-4a05-ade6-76dc505f98c0@k7g2000hsd.googlegroups.com...
> I'm trying to run a select max() on a primary key/unique/non-identity
> column then + 1, all while running an insert into.. select statement.
> For some reason, the select max isn't quite doing the trick as I get a
> primary key constraint error (MSG 2627) in SQL server!
>
> It's probably something simple like adding quotes or parenthesis or
> something, but I've tried...
>
> Here's my code:
>
> INSERT INTO [frameinventory]
> ([frameid]
> ,[framenumber]
> ,[framename]
> ,[colornumber]
> ,[a]
> ,[dbl]
> ,[templelength]
> ,[b]
> ,[ed]
> ,[cost]
> ,[retailprice]
> ,[upccode]
> ,[eyesize]
> ,[bridgesize]
> ,[groupcost]
> ,[colordescription]
> ,[lenscolor]
> ,[lenscolorcode]
> ,[circumference]
> ,[edangle]
> ,[frontprice]
> ,[halftemplesprice]
> ,[templesprice]
> ,[manufacturername]
> ,[brandname]
> ,[collectionname]
> ,[gendertype]
> ,[agegroup]
> ,[activestatus]
> ,[productgroupname]
> ,[rimtype]
> ,[material]
> ,[frameshape]
> ,[country]
> ,[yearintroduced]
> ,[upccode_type])
> select (select max(frameid) + 1 from frameinventory), fpc, stylename,
> colorcode, a, dbl, temple, b, ed, completeprice,
> ((framesdata.completeprice*1600)+5), upc, eye, bridge, completeprice,
> colordescription, lenscolor, lenscolorcode, circumference, edangle,
> frontprice, halftemplesprice, templesprice, manufacturername,
> brandname, collectionname, gendertype, agegroup, 'Active',
> productgroupname, rimtype, material, frameshape, country,
> yearintroduced, 'UPC'
> from framesdata
> where not exists (select * from frameinventory where
> frameinventory.upccode=framesdata.upc)
>
>
> THANKS!


Always specify what version of SQL Server you are using. Assuming 2005 or
2008 then use the ROW_NUMBER() function to generate frameid. In earlier
versions, consider using an IDENTITY column.

If your query returns more than one row then every row will get the same
frameid, which probably explains the key violation error.

--
David Portas


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-12-2008, 05:22 PM
--CELKO--
 
Posts: n/a
Default Re: Select Max() not working

On Sep 7, 1:19*am, Brian <eye...@gmail.com> wrote:
> I'm trying to run a select max() on a primary key/unique/non-identity
> column then + 1, all while running an insert into.. select statement.
> For some reason, the select max isn't quite doing the trick as I get a
> primary key constraint error (MSG 2627) in SQL server!
>
> It's probably something simple like adding quotes or parenthesis or
> something, but I've tried...
>
> Here's my code:
>
> INSERT INTO [frameinventory]
> * * * * * *([frameid]
> * * * * * *,[framenumber]
> * * * * * *,[framename]
> * * * * * *,[colornumber]
> * * * * * *,[a]
> * * * * * *,[dbl]
> * * * * * *,[templelength]
> * * * * * *,[b]
> * * * * * *,[ed]
> * * * * * *,[cost]
> * * * * * *,[retailprice]
> * * * * * *,[upccode]
> * * * * * *,[eyesize]
> * * * * * *,[bridgesize]
> * * * * * *,[groupcost]
> * * * * * *,[colordescription]
> * * * * * *,[lenscolor]
> * * * * * *,[lenscolorcode]
> * * * * * *,[circumference]
> * * * * * *,[edangle]
> * * * * * *,[frontprice]
> * * * * * *,[halftemplesprice]
> * * * * * *,[templesprice]
> * * * * * *,[manufacturername]
> * * * * * *,[brandname]
> * * * * * *,[collectionname]
> * * * * * *,[gendertype]
> * * * * * *,[agegroup]
> * * * * * *,[activestatus]
> * * * * * *,[productgroupname]
> * * * * * *,[rimtype]
> * * * * * *,[material]
> * * * * * *,[frameshape]
> * * * * * *,[country]
> * * * * * *,[yearintroduced]
> * * * * * *,[upccode_type])
> select (select max(frameid) + 1 from frameinventory), fpc, stylename,
> colorcode, a, dbl, temple, b, ed, completeprice,
> ((framesdata.completeprice*1600)+5), upc, eye, bridge, completeprice,
> colordescription, lenscolor, *lenscolorcode, circumference, edangle,
> frontprice, halftemplesprice, templesprice, manufacturername,
> brandname, collectionname, gendertype, agegroup, 'Active',
> productgroupname, rimtype, material, frameshape, country,
> yearintroduced, 'UPC'
> from framesdata
> where not exists (select * from frameinventory where
> frameinventory.upccode=framesdata.upc)
>
> THANKS!


Just looking at this, it seems that you don't need frame_id since you
have attributes called frame_number, frame_name and UPC (which you
have given two names!! Fix that; a data element has one and only one
in a schema). I am also curious about what a "gender_type" is because
gender is a property -- you say "<something>_gender" -- and not an
entity. Was it supposed to be a style classification?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-12-2008, 05:22 PM
Brian
 
Posts: n/a
Default Re: Select Max() not working

On Sep 7, 4:54*am, --CELKO-- <jcelko...@earthlink.net> wrote:
> On Sep 7, 1:19*am, Brian <eye...@gmail.com> wrote:
>
>
>
>
>
> > I'm trying to run a select max() on a primary key/unique/non-identity
> > column then + 1, all while running an insert into.. select statement.
> > For some reason, the select max isn't quite doing the trick as I get a
> > primary key constraint error (MSG 2627) in SQL server!

>
> > It's probably something simple like adding quotes or parenthesis or
> > something, but I've tried...

>
> > Here's my code:

>
> > INSERT INTO [frameinventory]
> > * * * * * *([frameid]
> > * * * * * *,[framenumber]
> > * * * * * *,[framename]
> > * * * * * *,[colornumber]
> > * * * * * *,[a]
> > * * * * * *,[dbl]
> > * * * * * *,[templelength]
> > * * * * * *,[b]
> > * * * * * *,[ed]
> > * * * * * *,[cost]
> > * * * * * *,[retailprice]
> > * * * * * *,[upccode]
> > * * * * * *,[eyesize]
> > * * * * * *,[bridgesize]
> > * * * * * *,[groupcost]
> > * * * * * *,[colordescription]
> > * * * * * *,[lenscolor]
> > * * * * * *,[lenscolorcode]
> > * * * * * *,[circumference]
> > * * * * * *,[edangle]
> > * * * * * *,[frontprice]
> > * * * * * *,[halftemplesprice]
> > * * * * * *,[templesprice]
> > * * * * * *,[manufacturername]
> > * * * * * *,[brandname]
> > * * * * * *,[collectionname]
> > * * * * * *,[gendertype]
> > * * * * * *,[agegroup]
> > * * * * * *,[activestatus]
> > * * * * * *,[productgroupname]
> > * * * * * *,[rimtype]
> > * * * * * *,[material]
> > * * * * * *,[frameshape]
> > * * * * * *,[country]
> > * * * * * *,[yearintroduced]
> > * * * * * *,[upccode_type])
> > select (select max(frameid) + 1 from frameinventory), fpc, stylename,
> > colorcode, a, dbl, temple, b, ed, completeprice,
> > ((framesdata.completeprice*1600)+5), upc, eye, bridge, completeprice,
> > colordescription, lenscolor, *lenscolorcode, circumference, edangle,
> > frontprice, halftemplesprice, templesprice, manufacturername,
> > brandname, collectionname, gendertype, agegroup, 'Active',
> > productgroupname, rimtype, material, frameshape, country,
> > yearintroduced, 'UPC'
> > from framesdata
> > where not exists (select * from frameinventory where
> > frameinventory.upccode=framesdata.upc)

>
> > THANKS!

>
> Just looking at this, it seems that you don't need frame_id since you
> have attributes called frame_number, frame_name and UPC (which you
> have given two names!! Fix that; a data element has one and only one
> in a schema). *I am also curious about what a "gender_type" is because
> gender is a property -- you say "<something>_gender" -- and not an
> entity. *Was it supposed to be a style classification?- Hide quoted text -
>
> - Show quoted text -


To clarify from my early AM post,

1. I'm using SQL 2005 Express at the moment.
2. The schema(e) for the two tables were designed by app developers
and I cannot change them.
3. framesdata table is a temporary import table that the application
uses to pull data from a data cd
4. The only primary key for either table is the frameid column in
frameinventory
5. All other data is pre-populated. I'm basically trying to copy
data that does not exist in frameinventory from framesdata
6. Not all information in frameinventory always comes from framesdata
(ie user-input via the app). I suspect that is why the primary key is
not tied to the framesdata data.
7. Frameinventory containes 27000 entries. framesdata contains 88000
entries.
8. The primary key in frameinventory is not the same as the row
number (row 1 has a primary key of 699), and I will double check later
today if they are sequential.

I'll fiddle with row_number() and see where we go with things.

Thanks for the input!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-12-2008, 05:22 PM
--CELKO--
 
Posts: n/a
Default Re: Select Max() not working

>> 2. The schema(e) for the two tables were designed by app developers and I cannot change them. <<

Those are very scary words. Tables should be designed by database
people and not developers. Thatg is how you get sloppy schemas like
this. Might want to find out who your DB person is and get them to
fix things.

>> I'll fiddle with row_number() and see where we go with things. <<


You might want to look at the UPC and find out how many different
things you have with the same UPC.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-12-2008, 05:22 PM
Brian
 
Posts: n/a
Default Re: Select Max() not working


On Sep 7, 7:04*pm, --CELKO-- <jcelko...@earthlink.net> wrote:
> >> 2. *The schema(e) for the two tables were designed by app developersand I cannot change them. <<

>
> Those are very scary words. *Tables should be designed by database
> people and not developers. *Thatg is how you get sloppy schemas like
> this. *Might want to find out who your DB person is and get them to
> fix things.



The app is a frontend for their table setup - if I change it, the app
dies. I'm simply bypassing an inefficiency in the app by importing
the data myself (whereas they display it all to the screen
simultaneously)

>
> >> I'll fiddle with row_number() and see where we go with things. <<

>
> You might want to look at the UPC and find out how many different
> things you have with the same UPC.


Again, I'm stuck with a semi-sequential FrameID primary key. There
are approximately 80k unique UPC's in the table, and it would make
more sense to use them...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-12-2008, 05:22 PM
shuurai11@gmail.com
 
Posts: n/a
Default Re: Select Max() not working


> Those are very scary words. *Tables should be designed by database
> people and not developers. *Thatg is how you get sloppy schemas like
> this. *Might want to find out who your DB person is and get them to
> fix things.


Unfortunately in the real world, a lot of companies use systems that
are designed by developers. Sometimes those developers come up with
schemas that are less than optimal, and in some cases even downright
stupid.

But the reality is, you generally can't just have your DBA fix the
schema without rewriting the application code; and few companies are
going to put the time and resources into reworking their rather
expensive system just because some guy on the internet doesn't like
the schema the vendor came up with, no matter how big that guy's ego
happens to be.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-12-2008, 05:22 PM
--CELKO--
 
Posts: n/a
Default Re: Select Max() not working

>> The app is a front end for their table setup - if I change it, the app dies. *I'm simply bypassing an inefficiency in the app by importing the data myself (whereas they display it all to the screen simultaneously) <<

Since I make some of my living fixing things like this, I tell you
that you are not the Lone Ranger. Modern application developers are
too damn busy being "agile" or "extreme" (aka "Git'er done!" in the
words of Larry the cable guy) that they never bothered with basic
software engineering concepts like coupling and cohesion.

That ignorance always leads to tight coupling, where the DB is used as
if it were a file in their procedural language. Some of the time it
also leads to low cohesion, but the tight coupling then prevents any
chance of a robust program.

Rant, rant, rant. Not that this helps you, but I feel better...

>> Again, *I'm stuck with a semi-sequential FrameID primary key. *Thereare approximately 80k unique UPC's in the *table, and it would make moresense to use them... <<


Amen. You would get an interface to the POS system, external
validation and verification, access to industry sales data, etc. But,
hey, the cowboy coders might have to research and think; their screen
might not paint as fast, etc. So let's pretend that the entire world
revolves around painting screens and keeping their code as 1950's as
possible.

Opps, ranting again ..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads for: Select Max() not working

Thread Thread Starter Forum Replies Last Post
SELECT WHERE NOT, is not working MargaretGillon@chromalloy.com Pgsql General 4 04-08-2008 07:41 PM
Re: SELECT WHERE NOT, is not working MargaretGillon@chromalloy.com Pgsql General 0 04-08-2008 07:41 PM
Re: SELECT WHERE NOT, is not working Steve Crawford Pgsql General 0 04-08-2008 07:41 PM
SELECT not working??? JimJx MySQL 1 02-28-2008 11:28 AM
select with like not working... Jason Pruim MySQL General forum 3 02-28-2008 07:28 AM


All times are GMT. The time now is 05:09 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com