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:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 27th, 2008, 12:16 AM
Double_Helix Double_Helix is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Double_Helix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 44 sec
Reputation Power: 0
Insert then select

Hi guys

I've got a problem with an SQL statement for my assignment(i think this is the right section for this).

The exact question is: Create a new support ticket for a customer with respect to a given product; provide the customer with the ticket ID for future reference.

Currently i'm assuming that i've got to create a new ticket and display the inserted customer ID along with the ticket ID.

Below is the given format of the table:

ID INTEGER, (this is the ticket ID)
Problem VARCHAR(1000),
Status VARCHAR(20),
Priority INTEGER,
LoggedTime TIMESTAMP,
CustomerID INTEGER,
ProductID INTEGER

I can easily insert a new ticket, I can also display CustomerID and ID seperatly from the insert. But, I can't for the life in me get it to work as a single line statement with INSERT and SELECT, as I'm aware using INSERT then SELECT is used to insert data in differently rather than writing INSERT INTO <table> VALUES ... ...

So alas, if any can be of assistance I would be most grateful, and if anymore information is needed I'll try my best to tell you as much as I know. Thanks for your time non-the-less.

DH

Reply With Quote
  #2  
Old April 27th, 2008, 02:06 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 30 m 33 sec
Reputation Power: 891
presumably ID is an auto_incrementing number

when you insert a new row, you supply values for all the columns except the ID, and the ID gets the next sequential number

thus, you need to find a way to determine what that number that just got assigned is

in many database systems, there is a special function for this -- e.g. SCOPE_IDENTITY in SQL Server

failing that, you could easily determine the value by simply querying back the row that was just inserted using all the same values
Code:
SELECT ID
  FROM tickets
 WHERE Problem ='dasdfas...'
   AND Status = 'rqreger...'
   AND Priority = 9
   AND CustomerID = 937
   AND ProductID = 101
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old April 27th, 2008, 03:21 PM
Double_Helix Double_Helix is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Double_Helix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 44 sec
Reputation Power: 0
But in theory isn't using this just declaring another statement after the initial INSERT statement? I can see where your coming from, and unfortunatly my SQL knowledge is somewhat lacking.

From what you said, is there a way I can just call the last row then?

Reply With Quote
  #4  
Old April 27th, 2008, 03:31 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 30 m 33 sec
Reputation Power: 891
you might like to mention which DBMS you're using

Reply With Quote
  #5  
Old April 27th, 2008, 03:42 PM
Double_Helix Double_Helix is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Double_Helix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 44 sec
Reputation Power: 0
well i don't have to like it, but it is reconmended of me isn't it? hehe

Yea im using IBM DB2 and MySQL, I believe the version is 8.2, but for some reason I can't connect to it from home, even the alleged techs at my uni cannot understand why, and hence im testing the statements in MySQL at home, but it will be used in DB2 in the end

Reply With Quote
  #6  
Old April 27th, 2008, 05:13 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 30 m 33 sec
Reputation Power: 891
well i've never done this in DB2 but apparently you use the IDENTITY_VAL_LOCAL() function -- see http://www.craigsmullins.com/zjdp_006.htm

in mysql, you would use the LAST_INSERT_ID() function

Reply With Quote
  #7  
Old April 28th, 2008, 12:17 AM
Double_Helix Double_Helix is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 Double_Helix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 44 sec
Reputation Power: 0
It seems to work out ok

Thank you so much, if it wern't for the fact it'll cost me more for the flight than a pint I would actually buy you one!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Insert then select


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 5 hosted by Hostway