简体   繁体   English

如果为null,如何在sql中生成uniqueidentifier

[英]How to generate a uniqueidentifier in sql if null

I am trying to add a column to an existing SQL table of uniqueidentifier type. 我试图将一列添加到现有SQL表的uniqueidentifier类型。 That column must not be null and of course unique. 该列不能为空,当然也不能唯一。 I have attempted this code: 我尝试了这段代码:

ALTER TABLE ScheduleJobs ADD CurrentInstanceID uniqueidentifier not null

followed by: 其次是:

ALTER TABLE ScheduleJobs ADD CONSTRAINT DF_CurrentInstanceID DEFAULT newsequentialid() FOR CurrentInstanceID

However, when I create a new record (from C#), the uniqueidentifier is always all zeros (presumably null.) I can create the GUID in C# and pass it to sql upon creating a new record which works fine, but I am concerned that a duplicate GUID could be created. 但是,当我从C#创建新记录时,uniqueidentifier总是全零(大概为null。)我可以在C#中创建GUID,并在创建新记录时将其传递给sql,但效果很好,但我担心可以创建一个重复的GUID。 Based on my readings, it appears that would be an extremely rare case, but it always seems bad practice to have any sort of potential error floating around. 根据我的阅读,这似乎是一种极为罕见的情况,但总是存在着任何潜在的错误浮出水面的坏习惯。 Note that the field will not be a PK for the table. 请注意,该字段将不是表的PK。 Suggestions and opinions welcome for the sake of education. 为了教育起见,欢迎提出建议和意见。

I am using C# 4.0 framework with MS SQL SERVER 2008 我在MS SQL SERVER 2008中使用C#4.0框架

Sorry for the delay, but I am glad to say that I have this issue resolved. 很抱歉造成您的延迟,但是我很高兴地说我已经解决了这个问题。 Thanks everyone for your overwhelming support. 感谢大家的大力支持。 While no one quite hit the nail on the head (and there were some really good suggestions btw), Eldog brought up Entity Framework not playing nice in his comment. 虽然没有人能完全打定主意(并且有一些非常好的建议),但埃尔多格提出了Entity Framework的意见不佳。 Thanks to him, I simply Googled Entity Framework + GUID and found the solution. 多亏了他,我才用Googled Entity Framework + GUID找到了解决方案。

This article steps through the issue and gives a great explanation on the problem, solution, and steps to resolve it. 本文逐步解决了该问题,并对问题,解决方案以及解决此问题的步骤进行了很好的解释。 I will note that I decided to step through and test one step at a time and that I didn't have to do the last step. 我将注意到,我决定逐步进行并测试一个步骤,而不必执行最后一步。 That leads me to believe that part of the issue may have been resolved in later versions of the Entity Framework. 这使我相信,部分问题可能已在更高版本的实体框架中得到解决。

I simply pulled up the edmx file in design view (not xml) and set the StoreGeneratedPattern property to "Identity." 我只是在设计视图(不是xml)中拉出edmx文件,并将StoreGeneratedPattern属性设置为“ Identity”。

Thanks again for the help and suggestions. 再次感谢您的帮助和建议。 You're an awesome bunch. 你真是棒极了。

Does your C# code attempt to pass in a CurrentInstanceID when creating the record? 创建记录时,您的C#代码是否尝试传递CurrentInstanceID? If so, can you drop that column from the INSERT statement? 如果是这样,您可以从INSERT语句中删除该列吗?

We do this with numeric primary keys. 我们使用数字主键来实现。 Our C# code calls a stored procedure for CRUD operations on our records. 我们的C#代码为记录上的CRUD操作调用存储过程。 The C# code generates a negative key on the client side for its own use. C#代码在客户端生成一个否定密钥供其自己使用。 When it is ready to create the record, it passes this key to the stored procedure. 准备好创建记录时,它将把此密钥传递给存储过程。

The proc ignores this key and inserts the rest of the data. proc会忽略此键并插入其余数据。 The output of the proc is the actual key that SQL assigned to the record. proc的输出是SQL分配给记录的实际键。 Finally, the C# code merges the new key into the existing data. 最后,C#代码将新密钥合并到现有数据中。

I wouldn't use a GUID for this. 我不会为此使用GUID。 GUIDs are used in quite a lot of operations in windows, so this won't be an identifier that will be only unique in your application , it will be a unique identifier in your operating system . GUID在Windows的许多操作中都使用过,因此,它不是一个仅在您的应用程序中 唯一的标识符 ,而是一个在操作系统中唯一标识符 Unless this makes sens in your case, I wouldn't use it. 除非这对您有帮助,否则我不会使用。

You could use an incremental value, like a simple uint . 您可以使用增量值,例如简单的uint If your table already has some data, you could write a script that fills existing rows with incremental values for your new column, and add the unique contraint to your column after executing that script. 如果表中已有数据,则可以编写一个脚本,用新列的增量值填充现有行,并在执行该脚本后将唯一约束添加到列中。

in your original Create table or alter table use something like the following 在原始的“创建”表或“更改”表中,使用如下所示的内容

create table ScheduleJobs (keyval int, id2 uniqueidentifier not null default newsequentialid())

then don't reference the column in your insert and a new GUID value will be added 然后不要引用插入内容中的列,而是将添加一个新的GUID值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM