Home All Groups Group Topic Archive Search About
Author
11 Aug 2006 3:47 PM
robken
Hi all,

Does anyone know if theres a way to add an identity column into a
SELECT...INTO Query?  Either that or can you add one into an existing
table.

Thanks Alot!

Rob.

Author
11 Aug 2006 3:53 PM
Mike C#
How about the IDENTITY() function?

Show quote
"robken" <robin.kenn***@hotmail.co.uk> wrote in message
news:1155311239.277052.173590@i3g2000cwc.googlegroups.com...
> Hi all,
>
> Does anyone know if theres a way to add an identity column into a
> SELECT...INTO Query?  Either that or can you add one into an existing
> table.
>
> Thanks Alot!
>
> Rob.
>
Author
11 Aug 2006 4:13 PM
Baj-SGC818
HI Robken

Try this :

Select lastname, firstname  into #Foo
from Employees

Alter table #foo add counter int Identity(1,1)
Select * from #foo
drop table #foo

/*
>> which returns
lastname             firstname  counter
-------------------- ---------- -----------
Davolio              Nancy      1
Fuller               Andrew     2
Leverling            Janet      3
Peacock              Margaret   4
Buchanan             Steven     5
Suyama               Michael    6
King                 Robert     7
Callahan             Laura      8
Dodsworth            Anne       9

(9 row(s) affected)
*/

All the best

Baj-SGC818

robken wrote:
Show quote
> Hi all,
>
> Does anyone know if theres a way to add an identity column into a
> SELECT...INTO Query?  Either that or can you add one into an existing
> table.
>
> Thanks Alot!
>
> Rob.
Author
11 Aug 2006 6:18 PM
Dean
Use identity() function:

select identity(int,1,1) as id, name
into #tmp
from sysdatabases

HTH,

Dean

Show quote
"robken" <robin.kenn***@hotmail.co.uk> wrote in message
news:1155311239.277052.173590@i3g2000cwc.googlegroups.com...
> Hi all,
>
> Does anyone know if theres a way to add an identity column into a
> SELECT...INTO Query?  Either that or can you add one into an existing
> table.
>
> Thanks Alot!
>
> Rob.
>
Author
11 Aug 2006 7:08 PM
glen
Just a picky note; you might want to use a @table variable instead of a temp
#table, less overhead.
--
glen


Show quote
"Dean" wrote:

> Use identity() function:
>
> select identity(int,1,1) as id, name
> into #tmp
> from sysdatabases
>
> HTH,
>
> Dean
>
> "robken" <robin.kenn***@hotmail.co.uk> wrote in message
> news:1155311239.277052.173590@i3g2000cwc.googlegroups.com...
> > Hi all,
> >
> > Does anyone know if theres a way to add an identity column into a
> > SELECT...INTO Query?  Either that or can you add one into an existing
> > table.
> >
> > Thanks Alot!
> >
> > Rob.
> >
>
>
>
Author
11 Aug 2006 8:58 PM
Dean
"glen" <g***@discussions.microsoft.com> wrote in message
news:3B5EA01F-7FBF-43EF-9717-71837D5BC9AE@microsoft.com...
> Just a picky note; you might want to use a @table variable instead of a
> temp
> #table, less overhead.
> --
> glen

Too bad you can't SELECT ...  INTO a table variable..

Dean

AddThis Social Bookmark Button