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 2012 Forums
 Transact-SQL (2012)
 Create a table with identity selecting from view

Author  Topic 

Loneliness
Starting Member

10 Posts

Posted - 2013-09-27 : 06:16:18
Hello, i need to create a table selecting from a view:

select *
into Mytable
from Myview

but i want Mytable to have an IDENTITY column that is not present in Myview...can i achieve this directly in the select statement without altering the table afterwards?

Tank you.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-27 : 08:08:03
You can. Example:
SELECT *,
IDENTITY(INT,1,1) As NewColumn
INTO MyTable
FROM MyView


See documentation here: http://technet.microsoft.com/en-us/library/ms189838.aspx
Go to Top of Page

Loneliness
Starting Member

10 Posts

Posted - 2013-09-27 : 08:25:23
Thank you very much James.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-28 : 04:36:14
if you want identity values to be generated based on order of particular column make sure you use something like



SELECT *,
ROW_NUMBER() OVER (ORDER BY YourColumn ) As NewColumn
INTO MyTable
FROM MyView



SELECT INTO with IDENTITY will not guarantee the order in which identity values are generated

http://support.microsoft.com/kb/273586

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Loneliness
Starting Member

10 Posts

Posted - 2013-10-02 : 05:25:53
Thanx visakh16.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-04 : 01:29:40
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-10-08 : 08:52:13
Why do you want to create a new table? Can't you do it when selecting data from view?


SELECT *,
ROW_NUMBER() OVER (ORDER BY YourColumn ) As NewColumn
FROM MyView


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -