|
|
|
You can use ALTER TABLE statement to add/remove column and to change some attributes of a column but there are some limitations with ALTER TABLE statement. You can not use ALTER TABLE to set a column as an identity column (Auto Increment field). You can ADD a new column with IDENTITY property but can not modify existing column's IDENTITY property using ALTER TABLE statement. Here is the stored procedure which is the best way to set and reset identity property of a field. |
Click here to copy the following block |
create proc sp_SetIdentity @tblname varchar(128) ='mytable' ,@set bit=0 ,@colname varchar(128)=NULL as
SET NOCOUNT ON SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED SET ANSI_WARNINGS OFF
declare @tmpcolname varchar(128) declare @datatype varchar(128) declare @tblname_bak varchar(128) declare @CrLf char(2)
set @tblname_bak = @tblname + replace(cast(newid() as varchar(36)),'-','') set @CrLf = char(13) + char(10)
select @tmpcolname = COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tblname and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
if (@colname is null) set @colname=@tmpcolname select @datatype=DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tblname AND COLUMN_NAME=@colname
DECLARE @cols varchar(8000), @delimiter char,@cmdSql varchar(8000) SET @delimiter = ',' SELECT @cols = COALESCE(@cols + @delimiter,'') + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tblname AND COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 0 AND COLUMN_NAME<>@colname
if @set=1 begin if (@tmpcolname is not null) begin if @tmpcolname = @colname print 'Info : ' + @colname + ' is already identity column' else print 'Error : Table can have only one identity column' end else begin set @cmdsql='select top 0 ' + @cols + ' into ' + @tblname_bak + ' from ' + @tblname execute(@cmdsql)
set @cmdsql= 'SET NOCOUNT ON' + @CrLf set @cmdsql= @cmdsql + 'Alter table ' + @tblname_bak + @CrLf + ' Add ' + @colname + ' ' + @datatype + ' identity(1,1) not null' execute(@cmdsql)
set @cmdsql= 'SET NOCOUNT ON' + @CrLf set @cmdsql= @cmdsql + 'SET IDENTITY_INSERT ' + @tblname_bak + ' ON' + @CrLf set @cmdsql= @cmdsql + 'INSERT INTO ' + @tblname_bak + '('+ @cols + ',' + @colname +')' + @CrLf + 'SELECT ' + @cols + ',' + @colname + ' FROM ' + @tblname + @CrLf set @cmdsql= @cmdsql + 'SET IDENTITY_INSERT ' + @tblname_bak + ' OFF' execute(@cmdsql)
set @cmdsql= 'drop table ' + @tblname + @CrLf set @cmdsql=@cmdsql + 'exec sp_rename ' + @tblname_bak + ',' + @tblname execute(@cmdsql) print @colname + ' is set to identity' end end else begin if (@tmpcolname <> @colname) begin print 'Error : ' + @colname + ' is not an identity column' end else begin set @cmdsql= 'SET NOCOUNT ON' + @CrLf set @cmdsql= @cmdsql + 'select top 0 ' + @cols + ' into ' + @tblname_bak + ' from ' + @tblname execute(@cmdsql)
set @cmdsql= 'SET NOCOUNT ON' + @CrLf set @cmdsql= @cmdsql + 'Alter table ' + @tblname_bak + @CrLf + ' Add ' + @colname + ' ' + @datatype execute(@cmdsql) set @cmdsql= 'SET NOCOUNT ON' + @CrLf set @cmdsql= @cmdsql + 'INSERT INTO ' + @tblname_bak + ' ('+ @cols + ',' + @colname +')' + @CrLf + 'SELECT ' + @cols + ',' + @colname + ' FROM ' + @tblname execute(@cmdsql) set @cmdsql= 'drop table ' + @tblname + @CrLf set @cmdsql=@cmdsql + 'exec sp_rename ' + @tblname_bak + ',' + @tblname execute(@cmdsql) print @colname + ' is reset' end end go |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|