identity-column

Unit testing with identity columns

左心房为你撑大大i 提交于 2019-12-13 18:18:29
问题 I am using IDENTITY columns in my SQL Server tables. I have a unit test where I delete a data record with a certain id. When I insert the testdata I get this exception: Cannot insert explicit value for identity column in table 'MyTablename' when IDENTITY_INSERT is set to OFF Then I do a quick test in my management studio: SET IDENTITY_INSERT MyTablename ON It says Command(s) completed successfully. I rerun the unit test but still get the same error. How do I go about that in general? I want

Is using a (sequential) GUID the only viable alternative to a database generated ID?

与世无争的帅哥 提交于 2019-12-13 16:09:41
问题 We are migrating our MS-Access database to SQL Server Compact 4.0 using the Entity Framework 5 Code First approach. We found that using Database generated Integer ID's is very slow and to make it worse, the delay increases exponentially with the size of the database. This makes using an Identity column impossible and it seems there is a bad implementation of this feature in SQL Server Compact 4.0 paired with the Entity Framework. So, we ran some tests and found that using a client side

How to prevent interim identity holes in SQL Server

ⅰ亾dé卋堺 提交于 2019-12-13 04:28:48
问题 Is there a way (using config + transaction isolation levels) to ensure that there are no interim holes in a SQL Server IDENTITY column? Persistent holes are OK. The situation I am trying to avoid is when one query returns a hole but a subsequent similar query returns a row that was not yet committed when the query had been run the first time. 回答1: Your question is one of isolation levels and has nothing to do with IDENTITY. The same problem applies to any update/insert visibility. The first

SQL Identity column if no input value in insert query in ADO.NET throws exception that it can't be null

落爺英雄遲暮 提交于 2019-12-12 05:49:22
问题 I have an identity column in the DB which is primary key and increments its value by itself. But I have a insert query in asp.net ado.net class like below: cmd = new SqlCommand("insert into Images(Name,[Image]) VALUES ('Nature',@img)", conn); cmd.CommandType = CommandType.Text; cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = TextBox1.Text; cmd.Parameters.Add("@img", SqlDbType.Image).Value = img; cmd.ExecuteNonQuery(); However, it throws me exception that it cannot be null. My idea/goal

How does SQL Server generate values in an identity column?

自作多情 提交于 2019-12-12 05:38:07
问题 My question is if I have two batch inserts into one table in parallel, how does SQL Server create identity values? I mean, if in one session I insert multiple rows (Row1-Row2-Row3) and simultaneously another session inserts multiple rows (Row4-Row5-Row6) at the same time, the result would be like this? Row1 Row2 Row3 Row4 Row5 Row6 or maybe something like this? Row1 Row6 Row3 Row5 Row4 Row2 回答1: You are making the common fallacy of assuming an order in the table. Tables have no order. Only

Why does Teradata Identity column have negative values?

∥☆過路亽.° 提交于 2019-12-11 11:40:30
问题 I am very new to Teradata (1 week) and my primary skillset is SQL Server. I am pulling data from one of the TeraData tables to SQL Server table and the TD table has an Identity column with negative values. I looked at the DDL for that table and it does not mention a start with and end with values. So, how does TeraData have the negative values in there? Also, if I pull this over to SQL Server, should I create a new Identity Column or should I just maintain the Identity values I get from

Uniqueidentifier as Identity in SQL Server CE?

半城伤御伤魂 提交于 2019-12-11 07:54:43
问题 In SQL Server CE can I create an identity column with data type uniqueidentifier ? If so, how? If not, why not? I have tried creating it with "New Table"from the Server Explorer in VS2010, and the Identity option only gets enabled when the type is int. Edit: According to EricEJ's suggestion I did make the column PK. I also set it to be a "RowGuid" CREATE TABLE [MyTable] ( [Id] uniqueidentifier ROWGUIDCOL ... I don't know exactly the intended purpose of the RowGuid property, but it seem to

Teradata: How to back up a table that uses an identity column?

China☆狼群 提交于 2019-12-11 03:42:47
问题 In Teradata, the way I've been doing backups for tables is like this: create table xxx_bak as xxx with data Works great, but I have just discovered that this doesn't work for tables with identity columns. I need a backup method that can duplicate a table with its data intact so that I can roll it back in case I mess up some data. 回答1: If you just want a copy of the table, you can create one with the same structure but without making the key column an identity column. You can then insert into

How to write an INSTEAD OF INSERT trigger on a multi table view that works with identities?

99封情书 提交于 2019-12-11 03:41:52
问题 I have two tables that have a 1:1 relationship and use IDENTITY columns for their primary keys. A view is joining both tables together to give the impression that all columns are really stored in one table. For viewing purposes this is fine, but the view needs an INSTEAD OF INSERT trigger as well. How do you write such a trigger that splits the column values across both tables? Note : There is no candidate key other than the identity column in the parent table. Otherwise this question may

Does SQL Server guarantee sequential inserting of an identity column?

半腔热情 提交于 2019-12-09 08:38:28
问题 In other words, is the following "cursoring" approach guaranteed to work: retrieve rows from DB save the largest ID from the returned records for later, e.g. in LastMax later, "SELECT * FROM MyTable WHERE Id > {0}", LastMax In order for that to work, I have to be sure that every row I didn't get in step 1 has an Id greater than LastMax . Is this guaranteed, or can I run into weird race conditions? 回答1: Guaranteed as in absolutely under no circumstances whatsoever could you possibly get a