1) SQL Server allows only ONE identity column per table.
Ex:
Create Table #Identity_Post (RankNumber int identity(1,1),
First_Name varchar(40))
2) Identity(seed,increment)
Ex: identity(1,1) means starting number is 1 and it will add 1 to next number
identity(1000,5) means starting number is 1000 and it will add 5 to next number
3) SET IDENTITY_INSERT [Table_Name] [ON/OFF]
This property is one very usefull, When we want to insert a deleted rowid (identity number) again in the table we can use this property.
Ex:
CREATE TABLE #TempTable (Rowid int IDENTITY(1,1), Name nvarchar(20))
INSERT INTO #TempTable (Name) VALUES ('BiSpecialist')
INSERT INTO #TempTable (Name) VALUES ('BwSpecialist')
INSERT INTO #TempTable (Name) VALUES ('IsSpecialist')
INSERT INTO #TempTable (Name) VALUES ('RsSpecialist')
4) DBCC CHECKIDENT (Database Console Command)
http://msdn.microsoft.com/en-us/library/aa258817(v=sql.80).aspx
DBCC CHECKIDENT
( table_name
[, { NORESEED | { RESEED [, new_reseed_value ] } } ]
) [ WITH NO_INFOMSGS ]
Arguments:
| Argument | Description |
| table_name | Is the name of the table for which to check the current identity value. The table specified must contain an identity column. Table names must comply with the rules for identifiers. |
| NORESEED | Specifies that the current identity value should not be changed. |
| RESEED | Specifies that the current identity value should be changed. |
| new_reseed_value | Is the new value to use as the current value of the identity column. |
| WITH NO_INFOMSGS | Suppresses all informational messages. |
Parameter Specifications:
| DBCC CHECKIDENT statement | Identity correction(s) made |
| DBCC CHECKIDENT ('table_name', NORESEED) | The current identity value is not reset. DBCC CHECKIDENT returns a report indicating the current identity value and what it should be. |
| DBCC CHECKIDENT ('table_name') | If the current identity value for a table is lower than the maximum identity value stored in the column, it is reset using the maximum value in the identity column. |
| DBCC CHECKIDENT ('table_name', RESEED,new_reseed_value) | Current identity value is set to the new_reseed_value. If no rows have been inserted into the table since the table was created, or if all rows have been removed by using the TRUNCATE TABLE statement, the first row inserted after you run DBCC CHECKIDENT uses new_reseed_value as the identity. Otherwise, the next row inserted uses new_reseed_value + the current increment value. |
Exceptions: The following table lists conditions when DBCC CHECKIDENT does not automatically reset the current identity value and provides methods for resetting the value.
| Conditions | Reset Methods |
| The current identity value is larger than the maximum value in the table. | Execute DBCC CHECKIDENT (table_name, NORESEED) to determine the current maximum value in the column, and then specify that value as the new_reseed_value in a DBCC CHECKIDENT (table_name, RESEED, new_reseed_value) command. (or) Execute DBCC CHECKIDENT (table_name, RESEED, new_reseed_value) with new_reseed_value set to a very low value, and then run DBCC CHECKIDENT (table_name, RESEED) to correct the value. |
| All rows are deleted from the table. | Execute DBCC CHECKIDENT (table_name, RESEED, new_reseed_value) with new_reseed_value set to the desired starting value. |
No comments:
Post a Comment