Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 9th, 2003, 11:49 PM
hillsy hillsy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 6 hillsy User rank is Corporal (100 - 500 Reputation Level)hillsy User rank is Corporal (100 - 500 Reputation Level)hillsy User rank is Corporal (100 - 500 Reputation Level)hillsy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Stored Proc/Subquery or something else?

Yep, I'm still using Access 97

I have an application that writes a record with a unique, incremented ID to a database, immediately after which I need to write three records to a related table using ID as the foreign key. So I need to run a query that grabs the highest ID in one table and a text input string, and INSERTs them both into another table. This is essentially going to be a stored procedure (so far as you can do them in Access).

Anyway, here's the SQL which as far as I can see is legal:

PHP Code:
 INSERT INTO tblProfitSummary IDLineItem VALUES ((SELECT MAX(IDFROM tblDocuments WHERE CustomerName 'MyCustomer'), '<COGS>'); 


As you can see I'm trying to use a subselect to get the max ID value from one table, while the other value is just passed in normally. Right now it's a hardcoded string but will eventually be a parameter passed from the application.

Should work, yes? Access has no issues with me creating and saving the query. However when I try to run it, it crashes Access! Same error each time (Memory could not be read blah blah blah).

Any thoughts on how else I could write this query? At this rate I am going to have to pass the MAX(ID) back to the application, insert it there and then run several separate queries against the database for the INSERT. Don't really want to do that - seems like a lot of overhead.

I'm using ASP.NET by the way...

Reply With Quote
  #2  
Old December 10th, 2003, 12:13 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,689 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 16 h 12 m 15 sec
Reputation Power: 985
Code:
insert 
  into tblprofitsummary
     ( id
     , lineitem
     )
select max(id)
     , '<COGS>'
  from tbldocuments
 where customername = 'mycustomer'
i guess you don't want to hear about how using MAX(ID) is a potential data integrity disaster, right?

rudy
http://r937.com/

Reply With Quote
  #3  
Old December 10th, 2003, 12:25 PM
hillsy hillsy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 6 hillsy User rank is Corporal (100 - 500 Reputation Level)hillsy User rank is Corporal (100 - 500 Reputation Level)hillsy User rank is Corporal (100 - 500 Reputation Level)hillsy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Bingo. Funnily enough I got exactly the same answer from Dave Maxwell on Sitepoint at about the same time

Don't you hate the way self-evident answers are really hard to see until you see them?

Yep, I know all about the limitations of Max(ID). I'm not comfortable with using it, however I can't think of any other way to do it unfortunately. The ID field is set to "increment" rather than "random" while cross-referencing with the customer name should help ensure it's pulling out the correct record.

On to the next problem...

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Stored Proc/Subquery or something else?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway