English 中文(简体)
Using uniqueidentifier as Primary Key and inserting it from the client application using parameterized queries
原标题:

I have created a database where I use uniqueidentifier as the primary key. I m trying to run a parametrized query that in the end looks like this. (insert into someTable (uniqueID) Values( 76c14693-5475-4224-ba94-7d30c919ac59 ) (uniqueID is my PK). This insert statement executes without issues when I run it in SQL Server Management Studio, however, it fails when I try to run it from my code.

Error: Cannot insert explicit value for identity column in table someTable when IDENTITY_INSERT is set to OFF.

I have looked this up and there are a lot of suggestions on turning ON the IDENTITY_INSERT but they all use stored procedures. I need to be able to do this using a parametrized query. Or I need a way to get the newly created guid in the database. This seems like something everyone would need at some point. Are there any simple solutions?

PS. I need the ID because I need to add records to other tables using that ID.

EDIT:

Here is the Structure of the table in question

CREATE TABLE [dbo].[someTable]
( 
        [NewsID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_someTable_NewsID] DEFAULT (newid()), 
        [ShortTitle] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, 
        [DateCreated] [datetime] NULL CONSTRAINT [DF_someTable_DateCreated] DEFAULT (getdate()), 
        CONSTRAINT [PK_cmc_News_1] PRIMARY KEY CLUSTERED ( 
            [NewsID] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF
        ) ON [PRIMARY] 
) ON [PRIMARY]
最佳回答

Actually, you must have some other problem, or some other column in you table, because you only would get that error if you have an IDENTITY column in your table, and IDENTITY columns can t be of the datatype uniqueidentifier - are you sure you re inserting into the same table, or you aren t using an ORM tool of some kind that is trying to specify a specific value for an identity column? Perhaps you could script the table definition out and attach it?

EDIT: Adding additional info since the question now includes the schema.

Looks like there is no identity in this particular table, so I d first check to see if you have any triggers defined on the table in question and see if there is an attempted insert taking place in another table that uses an identity...

问题回答

IN the first place GUIDs area a horrible choice for primary keys.

If you are using them however, you should not also have the same filed defined as an identity field. Please post your table structure code and we can help you redefine it correctly.

The problem is that you are not allowed to insert an ID yourself (I assume this is because the engine wants to do it itself). To turn this feature on, you need to execute this query before your query:

SET IDENTITY_INSERT <tablename> ON 

You have set the Identity property to On for the field, which means that the database creates the id when you insert a record. If you set Identity to Off for the field, you can set the value in the insert.

Alternatively, let the database create the id, then get the id that was created:

insert into someTable ();select scope_identity()

Note: A stored procedure and a parameterised query are not exclusive. I always call the stored procedures using a parameterised query.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签