Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Jagadish25
Starting Member
3 Posts |
Posted - 2005-10-03 : 15:03:26
|
| hi to the group,i am small problem,i am having two columns 1 is col1 which is a primary key and col2 any think .now i want to insert the data into second column at that time the first column must get the values in identity (like 1,2,3,4 etc)with out using identity(sql server)/generated always(db2) can any one knows please explain it bye |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-10-03 : 19:18:46
|
From Books Online:quote: IDENTITY (Property)Creates an identity column in a table. This property is used with the CREATE TABLE and ALTER TABLE Transact-SQL statements.Note The IDENTITY property is not the same as the SQL-DMO Identity property that exposes the row identity property of a column. SyntaxIDENTITY [ ( seed , increment ) ]ArgumentsseedIs the value that is used for the very first row loaded into the table.incrementIs the incremental value that is added to the identity value of the previous row that was loaded.You must specify both the seed and increment or neither. If neither is specified, the default is (1,1).RemarksIf an identity column exists for a table with frequent deletions, gaps can occur between identity values. If this is a concern, do not use the IDENTITY property. However, to ensure that no gaps have been created or to fill an existing gap, evaluate the existing identity values before explicitly entering one with SET IDENTITY_INSERT ON.If you are reusing a removed identity value, use the sample code in Example B to check for the next available identity value. Replace tablename, column_type, and max(column_type) - 1 with your table name, identity column data type, and numeric value of the maximum allowable value (for that data type) -1.Use DBCC CHECKIDENT to check the current identity value and compare it with the maximum value in the identity column. When the IDENTITY property is used with CREATE TABLE, Microsoft® SQL Server™ uses the NOT FOR REPLICATION option of CREATE TABLE to override the automatic incrementing of an identity column. Usually, SQL Server assigns each new row inserted in a table a value that is some increment greater than the previous highest value. However, if the new rows are replicated from another data source, the identity values must remain exactly as they were at the data source.ExamplesA. Use the IDENTITY property with CREATE TABLEThis example creates a new table using the IDENTITY property for an automatically incrementing identification number.USE pubsIF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'new_employees') DROP TABLE new_employeesGOCREATE TABLE new_employees( id_num int IDENTITY(1,1), fname varchar (20), minit char(1), lname varchar(30))INSERT new_employees (fname, minit, lname)VALUES ('Karin', 'F', 'Josephs')INSERT new_employees (fname, minit, lname)VALUES ('Pirkko', 'O', 'Koskitalo')B. Use generic syntax for finding gaps in identity valuesThis example shows generic syntax for finding gaps in identity values when data is removed.Note The first part of the following Transact-SQL script is designed for illustration purposes only. You can run the Transact-SQL script that starts with the comment: - - Create the img table.-- Here is the generic syntax for finding identity value gaps in data.-- This is the beginning of the illustrative example.SET IDENTITY_INSERT tablename ONDECLARE @minidentval column_typeDECLARE @nextidentval column_typeSELECT @minidentval = MIN(IDENTITYCOL) FROM tablenameIF @minidentval = IDENT_SEED('tablename') SELECT @nextidentval = MIN(IDENTITYCOL) + IDENT_INCR('tablename') FROM tablename t1 WHERE IDENTITYCOL BETWEEN IDENT_SEED('tablename') AND MAX(column_type) AND NOT EXISTS (SELECT * FROM tablename t2 WHERE t2.IDENTITYCOL = t1.IDENTITYCOL + IDENT_INCR('tablename'))ELSE SELECT @nextidentval = IDENT_SEED('tablename')SET IDENTITY_INSERT tablename OFF-- Here is an example to find gaps in the actual data.-- The table is called img and has two columns: the first column -- called id_num, which is an increasing identification number, and the -- second column called company_name.-- This is the end of the illustration example.-- Create the img table.-- If the img table already exists, drop it.-- Create the img table.IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'img') DROP TABLE imgGOCREATE TABLE img (id_num int IDENTITY(1,1), company_name sysname)INSERT img(company_name) VALUES ('New Moon Books')INSERT img(company_name) VALUES ('Lucerne Publishing')-- SET IDENTITY_INSERT ON and use in img table.SET IDENTITY_INSERT img ONDECLARE @minidentval smallintDECLARE @nextidentval smallintSELECT @minidentval = MIN(IDENTITYCOL) FROM img IF @minidentval = IDENT_SEED('img') SELECT @nextidentval = MIN(IDENTITYCOL) + IDENT_INCR('img') FROM img t1 WHERE IDENTITYCOL BETWEEN IDENT_SEED('img') AND 32766 AND NOT EXISTS (SELECT * FROM img t2 WHERE t2.IDENTITYCOL = t1.IDENTITYCOL + IDENT_INCR('img')) ELSE SELECT @nextidentval = IDENT_SEED('img')SET IDENTITY_INSERT img OFFSee AlsoALTER TABLECREATE TABLEDBCC CHECKIDENTIDENT_INCR@@IDENTITYIDENTITY (Function)IDENT_SEEDSELECTSET IDENTITY_INSERT©1988-2000 Microsoft Corporation. All Rights Reserved.
MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
|
|
|
|
|