473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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!
Sep 7 '08 #1
7 2958
"Brian" <ey****@gmail.comwrote in message
news:76**********************************@k7g2000h sd.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
Sep 7 '08 #2
On Sep 7, 1:19*am, Brian <eye...@gmail.comwrote:
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?
Sep 7 '08 #3
On Sep 7, 4:54*am, --CELKO-- <jcelko...@earthlink.netwrote:
On Sep 7, 1:19*am, Brian <eye...@gmail.comwrote:


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!!
Sep 7 '08 #4
>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.
Sep 7 '08 #5

On Sep 7, 7:04*pm, --CELKO-- <jcelko...@earthlink.netwrote:
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...
Sep 8 '08 #6
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.
Sep 8 '08 #7
>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 ..
Sep 8 '08 #8

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

Similar topics

3
by: J. Muenchbourg | last post by:
I'm trying to pull the highest 'articleid' value out of an sql table, but the below is giving me an Incorrect syntax near 'articleid' error: dim sqlid sqlid = "SELECT max articleid from...
0
by: Tanamon | last post by:
Hello All, I am a MySQL newbie trying to write a query that selects file_name records possessing the highest numbered version for that unique file_name. I show sample data and two trial queries...
0
by: Fatt Shin | last post by:
Hi, I'm running MySQL 4.0.13, connecting from PowerBuilder 9 using ODCB Connector 3.51. I'm facing a problem where whenever I issue a SELECT COUNT(*) statement from PowerBuilder, I always get SQL...
4
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | ...
3
by: Ker | last post by:
I have a query that works great. It gives me the min for multiple fields. Within this query, I also need to get the max of some fields too. I currently have output of Date Name ...
2
by: Yang Lee | last post by:
Hi all, I am using access and in that I link the tables from oracle. I create a form and in that i have a text field e.g. emp_no this emp_no field should take the max value from table emp while...
4
by: Sandy | last post by:
Hi, I have a table A (ID, time,...) first I want to select rows with max value of time. Then from these rows I want the row with max ID value. i am doing the following but its giving me the...
3
by: otac0n | last post by:
How can I select the maximum value from a field, but make sure that the value i want stays greater than a cetrain value? I currently have this: SELECT A.rating, B.rating,...
1
by: Matik | last post by:
Hello Everybody, I have a problem, with select stmt: SELECT TOP 15 * FROM oaVIEW_MainData AS TOP_VIEW, oaLanguageData_TAB AS RwQualifierJoin with (nolock) WHERE (c_dateTime>='2007.01.10...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.