Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


can we give the identity for existing column of the table?
(already table contains 10(1-10) unique randam records)

Answers were Sorted based on User's Feedback



can we give the identity for existing column of the table? (already table contains 10(1-10) unique ..

Answer / pravati

ALTER TABLE Tablename
ADD oldcolumnname newcolumnname

Is This Answer Correct ?    0 Yes 0 No

can we give the identity for existing column of the table? (already table contains 10(1-10) unique ..

Answer / sambashiva

Yes we can
1. Adding Identity Property to an existing column in a table.

How difficult is it to add an Identity property to an existing column in a table? Is there any T-SQL that can perform this action?

For most, the answer to the above two questions is an absolute NO! There is no straightforward T-SQL like ALTER TABLE… or MODIFY COLUMN to add an Identity Property to an existing column in a table.

However, there is an easy way to accomplish this action. It can be done through SSMS.

Are you finding my answer difficult to believe? Let me explain.

Let’s first see what SSMS does in backend when you add Identity property on an existing column in any table.

Now, let’s create an example table for better understanding.



This table is vacant, with no records, as you can see in the following screenshot.



Take a look at the design of this table in SSMS.



Now let us make eid, an Identity column.

This is very easy. All you have to do is just select Yes from the drop down list and you are done!



But before moving further let’s see what T-SQL SQL Server is using to make this change.

You will notice that T-SQL is used by SQL Server to make this change.

After you make the change for Identity property from No to Yes, on top in tools box, you will see Generate Change Script. This is the T-SQL Script that SQL Server will use to make this change.





Unfortunately, I cannot expand this dialogue box further to show you the complete script, so I have copied this script below.

/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_example1
(
eid INT NOT NULL IDENTITY (1, 1)
) ON [PRIMARY]
GO
SET IDENTITY_INSERT dbo.Tmp_example1 ON
GO
IF EXISTS(SELECT * FROM dbo.example1)
EXEC('INSERT INTO dbo.Tmp_example1 (eid)
SELECT eid FROM dbo.example1 WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_example1 OFF
GO
DROP TABLE dbo.example1
GO
EXECUTE sp_rename N'dbo.Tmp_example1', N'example1', 'OBJECT'
GO
COMMIT

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More SQL Server Interview Questions

Tell me can we use custom code in ssrs?

0 Answers  


What do you understand by mirroring and mention the advantages of the mirroring?

0 Answers  


what is a join? : Sql server database administration

0 Answers  


Please illustrate physical database architecture? : SQL Server Architecture

0 Answers  


What are the types of normalization?

0 Answers  


Can you explain what are commit and rollback in sql?

0 Answers  


you have a table with close to 100 million records recently, a huge amount of this data was updated now, various queries against this table have slowed down considerably what is the quickest option to remedy the situation? : Sql server administration

0 Answers  


How do you trace the traffic hitting a sql server?

0 Answers  


how to get rank of diffrent student in same table based on newly inserted row in sql server2008

4 Answers   ABC,


Can you explain the disadvantages/limitation of the cursor?

0 Answers  


What is the difference between a function and a trigger?

0 Answers  


What are the rules to use the rowguidcol property to define a globally unique identifier column?

0 Answers  


Categories