identity-column

Can a sql server table have two identity columns?

孤街醉人 提交于 2019-11-26 19:00:58
I need to have one column as the primary key and another to auto increment an order number field. Is this possible? EDIT: I think I'll just use a composite number as the order number. Thanks anyways. CREATE TABLE [dbo].[Foo]( [FooId] [int] IDENTITY(1,1) NOT NULL, [BarId] [int] IDENTITY(1,1) NOT NULL ) returns Msg 2744, Level 16, State 2, Line 1 Multiple identity columns specified for table 'Foo'. Only one identity column per table is allowed. So, no, you can't have two identity columns. You can of course make the primary key not auto increment (identity). Edit: msdn:CREATE TABLE (Transact-SQL)

BULK INSERT with identity (auto-increment) column

為{幸葍}努か 提交于 2019-11-26 12:25:10
问题 I am trying to add bulk data in database from CSV file. Employee table has a column ID (PK) auto-incremented. CREATE TABLE [dbo].[Employee]( [id] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NULL, [Address] [varchar](50) NULL ) ON [PRIMARY] I am using this query: BULK INSERT Employee FROM \'path\\tempFile.csv \' WITH (FIRSTROW = 2,KEEPIDENTITY,FIELDTERMINATOR = \',\' , ROWTERMINATOR = \'\\n\'); .CSV File - Name,Address name1,addr test 1 name2,addr test 2 but it results in this error

Can a sql server table have two identity columns?

随声附和 提交于 2019-11-26 12:16:35
问题 I need to have one column as the primary key and another to auto increment an order number field. Is this possible? EDIT: I think I\'ll just use a composite number as the order number. Thanks anyways. 回答1: CREATE TABLE [dbo].[Foo]( [FooId] [int] IDENTITY(1,1) NOT NULL, [BarId] [int] IDENTITY(1,1) NOT NULL ) returns Msg 2744, Level 16, State 2, Line 1 Multiple identity columns specified for table 'Foo'. Only one identity column per table is allowed. So, no, you can't have two identity columns.

How do you determine what SQL Tables have an identity column programmatically

旧街凉风 提交于 2019-11-26 12:07:50
问题 I want to create a list of columns in SQL Server 2005 that have identity columns and their corresponding table in T-SQL. Results would be something like: TableName, ColumnName 回答1: Another potential way to do this for SQL Server, which has less reliance on the system tables (which are subject to change, version to version) is to use the INFORMATION_SCHEMA views: select COLUMN_NAME, TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where COLUMNPROPERTY(object_id(TABLE_SCHEMA+'.'+TABLE_NAME), COLUMN

SQL Server - Auto-incrementation that allows UPDATE statements

混江龙づ霸主 提交于 2019-11-26 11:39:28
问题 When adding an item in my database, I need it to auto-determine the value for the field DisplayOrder. Identity (auto-increment) would be an ideal solution, but I need to be able to programmatically change (UPDATE) the values of the DisplayOrder column, and Identity doesn\'t seem to allow that. For the moment, I use this code: CREATE PROCEDURE [dbo].[AddItem] AS DECLARE @DisplayOrder INT SET @DisplayOrder = (SELECT MAX(DisplayOrder) FROM [dbo].[MyTable]) + 1 INSERT INTO [dbo].[MyTable] (

SQL Identity (autonumber) is Incremented Even with a Transaction Rollback

岁酱吖の 提交于 2019-11-26 00:54:59
问题 I have a .net transaction with a SQL insert to a SQL Server 2005 database. The table has an identity primary key. When an error occurs within the transaction, Rollback() is called. The row inserts are rolled back correctly, however the next time I insert data to the table, the identity is incremented as if the rollback never occurred. So essentially there are gaps in the identity sequence. Is there any way to have the Rollback() method reclaim the missing identity? Am I not approaching this

Adding an identity to an existing column

巧了我就是萌 提交于 2019-11-25 21:49:00
问题 I need to change the primary key of a table to an identity column, and there\'s already a number of rows in table. I\'ve got a script to clean up the IDs to ensure they\'re sequential starting at 1, works fine on my test database. What\'s the SQL command to alter the column to have an identity property? 回答1: You can't alter the existing columns for identity. You have 2 options, Create a new table with identity & drop the existing table Create a new column with identity & drop the existing

Identity increment is jumping in SQL Server database

冷暖自知 提交于 2019-11-25 21:44:01
问题 In one of my tables Fee in column \"ReceiptNo\" in SQL Server 2012 database identity increment suddenly started jumping to 100s instead of 1 depending on the following two things. if it is 1205446 it is jumps to 1206306, if it is 1206321, it jumps to 1207306 and if it is 1207314, it jumps to 1208306. What I want to make you note is that the last three digits remain constant i.e 306 whenever the jumping occurs as shown in the following picture. this problem occurs when I restart my computer