If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Insert Statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-20-06, 16:39
macjoubert macjoubert is offline
Registered User
 
Join Date: Oct 2003
Location: Rhodesia
Posts: 28
Insert Statement

Hi,
Is there anyway to INSERT a record into the first row of a table ?
Reply With Quote
  #2 (permalink)  
Old 02-20-06, 20:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
nope

that's because there is no such thing as the first row of a table -- all the rows are equal

what is it that you are really trying to do, and why? can you give examples?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-20-06, 22:42
macjoubert macjoubert is offline
Registered User
 
Join Date: Oct 2003
Location: Rhodesia
Posts: 28
Quote:
Originally Posted by r937
nope

that's because there is no such thing as the first row of a table -- all the rows are equal

what is it that you are really trying to do, and why? can you give examples?
Hello and thanks for the response.

I am trying to send a temp table into an outfile, after inserting a header record.
The insert is plain and easy but I just want it to be at the top and not somewhere in the 33rd thousandth row.
Any ideas pls?
thanks
Reply With Quote
  #4 (permalink)  
Old 02-20-06, 22:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
use ORDER BY
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-20-06, 22:49
macjoubert macjoubert is offline
Registered User
 
Join Date: Oct 2003
Location: Rhodesia
Posts: 28
Quote:
Originally Posted by r937
use ORDER BY
Already tried that,
ORDER BY MATERIAL_NBR....
But MATERIAL_NBR may have alpha numeric characters.

A00198,
material_nbr,
P0021,
ZZZ900... AND SO ON.
Reply With Quote
  #6 (permalink)  
Old 02-21-06, 07:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
ORDER BY special_condition_for_header
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 02-21-06, 11:06
macjoubert macjoubert is offline
Registered User
 
Join Date: Oct 2003
Location: Rhodesia
Posts: 28
Quote:
Originally Posted by r937
ORDER BY special_condition_for_header
Just tried that didnt work,mysql doesnt seem to recognise 'special_condition_for header'.

Anyways i found a way out.

To add a header to the outfile the best way to do this and ensuring that its at the top is to use a UNION statement.

e.g.

SELECT 'MATERIAL_NBR', 'MATERIAL_TYPE'
UNION
SELECT MATERIAL_NBR, MATERIAL_TYPE INTO OUTFILE 'C:\TEST\TEST.TXT'
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n\r' FROM TABLE MATERIALS;

Works like a charm.
Thanks for your help..
Reply With Quote
  #8 (permalink)  
Old 02-21-06, 11:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by macjoubert
mysql doesnt seem to recognise 'special_condition_for header'.
of course not, that was for you -- you were supposed to write a special condition that recognizes the header row

i hate to break it to you, but without an ORDER BY clause, that UNION query is not guaranteed to give you the header first -- it will give you the header row first only until the one time it doesn't

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 02-21-06, 11:44
macjoubert macjoubert is offline
Registered User
 
Join Date: Oct 2003
Location: Rhodesia
Posts: 28
Lightbulb

Quote:
Originally Posted by r937
of course not, that was for you -- you were supposed to write a special condition that recognizes the header row

i hate to break it to you, but without an ORDER BY clause, that UNION query is not guaranteed to give you the header first -- it will give you the header row first only until the one time it doesn't

Aha I found a solution for that too my friend.
After the union statement if the SELECT MATERIAL_NBR etc is enclosed in parantheses ORDER BY doesnt alter the header row!!

e.g.

SELECT 'MATERIAL_NBR', 'MATERIAL_TYPE'
UNION
(SELECT MATERIAL_NBR, MATERIAL_TYPE INTO OUTFILE 'C:\TEST\TEST.TXT'
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n\r' FROM TABLE MATERIALS ORDER BY MATERIAL_NBR);

Will yield the same result!!

Last edited by macjoubert; 02-21-06 at 11:49.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On