Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 INSER INTO A TABLE with variable number to insert

Author  Topic 

sebastian11c
Posting Yak Master

129 Posts

Posted - 2012-02-17 : 10:26:55
hi , i need to insert several records from one table to another one


but the poblem is,

i need to insert almost 1000 record in the table "makes"

from the table "2012"
lets create the tables
CREATE TABLE [dbo].[makes](
[idmake] [int] IDENTITY(1,1) NOT NULL,
[make] [varchar](50) NOT NULL,
[index] [int] NOT NULL
) ON [PRIMARY]


CREATE TABLE [dbo].[2012](
[idmake] [int] IDENTITY(1,1) NOT NULL,
[make] [varchar](50) NOT NULL
) ON [PRIMARY]

lets put some record on table "2012" to simulate my real table
INSERT INTO [vehicle].[dbo].[2012]
([make])
VALUES
('AUDI'),
('BMW'),
('PORSCHE'),
('MASERATI')
GO



NOW i need to insert all the records from table "2012" to table "makes"

something like this

INSERT INTO [vehicle].[dbo].[makes]
([make]
,[index])
select make, X--(variable need to be increased one for each row inserted beginning in 500)
from 2012
GO


but i dont know how to do for inccrease the x variable


the result that i need is
on the table "makes"

idmake make index
1 audi 500 ( starts in 500)
2 bmw 501 (add 1)
3 porsche 502 (add 1)

etc


any help, any example


many thanks in advanced
GO

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-02-17 : 10:43:42
One option would be to use the ROW_NUMBER() function.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-17 : 11:37:40
I would question as to what sense this even makes BEFORE I tried to implement this "solution"

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -