dbTalk Databases Forums  

Current row number whenm using INSERT...SELECT?

comp.databases.mysql comp.databases.mysql


Discuss Current row number whenm using INSERT...SELECT? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ignoramus12411
 
Posts: n/a

Default Current row number whenm using INSERT...SELECT? - 01-04-2007 , 12:02 PM






We have an app that, for certain legacy reasons, does not use
AUTO_INCREMENT feature.

We have a table with id column that is supposed to be auto
incremented, and need to insert data there via INSERT...SELECT
statement.

@max = SELECT MAX( id ) from mytable
INSERT INTO mytable SELECT (@max + current_row_in_resultset() + 1, foo, bar)

what I would like is to find a function that I tentatively named
current_row_in_resultset, that returns number of current row in a
resultset. First row would have a number of (say) 0, the next row 1, etc.

Am I clear? Is there such a function?

i

Reply With Quote
  #2  
Old   
Paul Lautman
 
Posts: n/a

Default Re: Current row number whenm using INSERT...SELECT? - 01-04-2007 , 02:58 PM






Ignoramus12411 wrote:
Quote:
We have an app that, for certain legacy reasons, does not use
AUTO_INCREMENT feature.

We have a table with id column that is supposed to be auto
incremented, and need to insert data there via INSERT...SELECT
statement.

@max = SELECT MAX( id ) from mytable
INSERT INTO mytable SELECT (@max + current_row_in_resultset() + 1,
foo, bar)

what I would like is to find a function that I tentatively named
current_row_in_resultset, that returns number of current row in a
resultset. First row would have a number of (say) 0, the next row 1,
etc.

Am I clear? Is there such a function?

i
Wouldn't the first row in the results set be number 1, so the formula would
be (@max + current_row_in_resultset())




Reply With Quote
  #3  
Old   
Paul Lautman
 
Posts: n/a

Default Re: Current row number whenm using INSERT...SELECT? - 01-04-2007 , 03:12 PM



Paul Lautman wrote:
Quote:
Ignoramus12411 wrote:
We have an app that, for certain legacy reasons, does not use
AUTO_INCREMENT feature.

We have a table with id column that is supposed to be auto
incremented, and need to insert data there via INSERT...SELECT
statement.

@max = SELECT MAX( id ) from mytable
INSERT INTO mytable SELECT (@max + current_row_in_resultset() + 1,
foo, bar)

what I would like is to find a function that I tentatively named
current_row_in_resultset, that returns number of current row in a
resultset. First row would have a number of (say) 0, the next row 1,
etc.

Am I clear? Is there such a function?

i

Wouldn't the first row in the results set be number 1, so the formula
would be (@max + current_row_in_resultset())
Oh and I forgot to give you the answer!

SELECT @max:=MAX(id) from mytable;
INSERT INTO mytable SELECT @max:=@max+1, foo, bar FROM foobartable

Although the actual INSERT statement is untested.




Reply With Quote
  #4  
Old   
Ignoramus30651
 
Posts: n/a

Default Re: Current row number whenm using INSERT...SELECT? - 01-05-2007 , 09:39 AM



On Thu, 4 Jan 2007 20:58:07 -0000, Paul Lautman <paul.lautman (AT) btinternet (DOT) com> wrote:
Quote:
Ignoramus12411 wrote:
We have an app that, for certain legacy reasons, does not use
AUTO_INCREMENT feature.

We have a table with id column that is supposed to be auto
incremented, and need to insert data there via INSERT...SELECT
statement.

@max = SELECT MAX( id ) from mytable
INSERT INTO mytable SELECT (@max + current_row_in_resultset() + 1,
foo, bar)

what I would like is to find a function that I tentatively named
current_row_in_resultset, that returns number of current row in a
resultset. First row would have a number of (say) 0, the next row 1,
etc.

Am I clear? Is there such a function?

i

Wouldn't the first row in the results set be number 1, so the formula would
be (@max + current_row_in_resultset())


I have no idea what such a function would start with, 1 or 0, and I do
not care as long as it is incremented for every row.

i


Reply With Quote
  #5  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: Current row number whenm using INSERT...SELECT? - 01-05-2007 , 09:51 AM




Ignoramus30651 wrote:

Quote:
On Thu, 4 Jan 2007 20:58:07 -0000, Paul Lautman <paul.lautman (AT) btinternet (DOT) com> wrote:
Ignoramus12411 wrote:
We have an app that, for certain legacy reasons, does not use
AUTO_INCREMENT feature.

We have a table with id column that is supposed to be auto
incremented, and need to insert data there via INSERT...SELECT
statement.

@max = SELECT MAX( id ) from mytable
INSERT INTO mytable SELECT (@max + current_row_in_resultset() + 1,
foo, bar)

what I would like is to find a function that I tentatively named
current_row_in_resultset, that returns number of current row in a
resultset. First row would have a number of (say) 0, the next row 1,
etc.

Am I clear? Is there such a function?

i

Wouldn't the first row in the results set be number 1, so the formula would
be (@max + current_row_in_resultset())



I have no idea what such a function would start with, 1 or 0, and I do
not care as long as it is incremented for every row.

i
So what about the solution that I have also posted for you?



Reply With Quote
  #6  
Old   
Ignoramus30651
 
Posts: n/a

Default Re: Current row number whenm using INSERT...SELECT? - 01-05-2007 , 04:15 PM



On Thu, 4 Jan 2007 21:12:25 -0000, Paul Lautman <paul.lautman (AT) btinternet (DOT) com> wrote:
Quote:
Paul Lautman wrote:
Ignoramus12411 wrote:
We have an app that, for certain legacy reasons, does not use
AUTO_INCREMENT feature.

We have a table with id column that is supposed to be auto
incremented, and need to insert data there via INSERT...SELECT
statement.

@max = SELECT MAX( id ) from mytable
INSERT INTO mytable SELECT (@max + current_row_in_resultset() + 1,
foo, bar)

what I would like is to find a function that I tentatively named
current_row_in_resultset, that returns number of current row in a
resultset. First row would have a number of (say) 0, the next row 1,
etc.

Am I clear? Is there such a function?

i

Wouldn't the first row in the results set be number 1, so the formula
would be (@max + current_row_in_resultset())

Oh and I forgot to give you the answer!

SELECT @max:=MAX(id) from mytable;
INSERT INTO mytable SELECT @max:=@max+1, foo, bar FROM foobartable

Although the actual INSERT statement is untested.


Paul, your answer is FANTASTIC and works VERY WELL!!!

Thank you SO much!

You are da man!!!

i


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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.