April 7, 2005 at 9:54 pm
Hi
I have a student table, with sno, sname, addr fields. For the sno i want to add the identity field thru the query. i tried by altering the table, but i'm not getting.
can u pls give the query to add the identity to the sno field.
Thanking you,
Thiru.
April 8, 2005 at 2:18 am
You can add new column to the existing table by this
alter table a add b int identity(1,1)
but if u want to do a change to an existing column it is not simple queary.
U have to create a temp table which will have the identity column and and copy the existing data to the temp table
then drop the irginal table and rename it
Bit of work, but no other way I guess
My Blog:
April 8, 2005 at 5:13 am
As Dinesh already mentioned, there is no easy way for this. See if this helps:
set nocount on
create table showme
(
sno int not null primary key
, c1 char default 'a'
)
insert into showme(sno,c1) values(1, 'a')
insert into showme(sno,c1) values(2, 'b')
go
create table showme_temp(
sno int identity not null primary key
, c1 char default 'a'
)
set identity_insert showme_temp on
insert into showme_temp (sno,c1) select sno, c1 from showme
set identity_insert showme_temp off
drop table showme
go
exec sp_rename 'showme_temp', 'showme'
go
insert into showme default values
select * from showme
drop table showme
set nocount off
Caution: Changing any part of an object name could break scripts and stored procedures.
The object was renamed to 'showme'.
sno c1
----------- ----
1 a
2 b
3 a
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 22, 2005 at 1:10 pm
I have a similar problem. I am converting forums from the older one with different structure to the new one. I'm not the greatest SQL dude, but in my script I cannot have the PK set to using the identity because after I create the table, I insert values from my old database. For instance here's my query:
if not exists (select 1 from sysobjects where id = object_id(N'yaf_Forum') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
create table dbo.yaf_Forum(
ForumIDint IDENTITY (1, 1) NOT NULL ,
...
)
GO
INSERT INTO yaf_Forum(ForumID,...)
SELECT Forum_ID, FROM OLDForums...oldForum
GO
Produces the error:
Cannot insert explicit value for identity column in table 'yaf_Forum' when IDENTITY_INSERT is set to OFF.
Epiphany!
Is there a way to just turn INDENTITY_INSERT to on?
*goes to search for this*
April 22, 2005 at 1:14 pm
Yep, that seemed to be the solution to my problem. Just added:
SET IDENTITY_INSERT yaf_Forum ON
Before I started my insert statement.
April 22, 2005 at 1:14 pm
SELECT col2,col3 FROM OLDForums...oldForum
******************
Dinakar Nethi
Life is short. Enjoy it.
******************
April 22, 2005 at 1:18 pm
The problem was though, that my new forum has to have the correct ForumID's in every related table ie: My first forum "Super Forum" has an ID value of 23 not 1. Admittedly I am not an expert in SQL so I'm sure that there might be a way to do this...but this is working so far for me. If I do have problems, I'll be back
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply