>>>>>>Hi,
>>>>>>
>>>>>>What would be the TSQL syntax to check if a column is set to be an Indenity column (Is Identity = Yes)? And if No, how to do set the column in Identity Yes in code?
>>>>>>
>>>>>>TIA
>>>>>
>>>>>Sys.columns has IsIdentity bit column, e.g.
>>>>>
>>>>>if exists (SELECT name, is_identity
>>>>>FROM sys.columns
>>>>>WHERE [object_id] = object_id('items')
>>>>>and is_identity = 1)
>>>>>
>>>>>print 'Table Items has identity column'
>>>>>
>>>>>(If you know the column name, just check it using a similar script).
>>>>>
>>>>>If you want to make the column to be identity, you need to re-create the table (e.g. if you don't want to preserve current values, you can do
>>>>>
>>>>>select identity (int,1,1) as NewIdentityColumn, col1, col2, ...
>>>>>into myNewTable
>>>>>from myTable
>>>>
>>>>The code (above that checks if a table has identity) works. How do I change it if I want to include the column name. That is, if I want to know if a certain column in table 'items' is identity?
>>>>
>>>>On the last point, I do want to preserve the current value in the column before making it an identity. From this point, the column value will be incremented then. So I do not want to recreate the table.
>>>>
>>>>Thank you for your help.
>>>>
>>>>UPDATE. I know that on my first question I just need to specify the name of the column in the WHERE. Got it.
>>>
>>>For the first question, it.is
>>>
>>>select * from sys.columns where name = 'myColumnName' and object_id = object_id('MyTable')
>>>
>>>You still need to re-create the table, so
>>>
>>>create myNewTable (.....)
>>>
>>>set identity_insert dbo.MyNewTable ON
>>>
>>>insert into myNewTable (Col1, Col2, Col3)
>>>select Col1, Col2, Col3 from myOriginalTable
>>>
>>>set identity_insert dbo.MyNewTable OFF
>>>
>>>drop table myOriginalTable
>>>execute sp_rename 'MyOriginalTable', 'MyNewTable'
>>>
>>
>>Thank you for the code. I will, however, change it in SSMS. The table could have like 500,000 records and many columns. So copying from the existing into a temp could be long or a problem. I suppose SSMS does it faster.
>
>If you read Pinal's article, you'll find out that SSMS uses exactly the same script behind the scenes. So, it's not faster.
I did see that. But I am just not trusting myself to execute this code (from VFP, no less). So I will use SSMS.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham