【问题标题】:How to insert an uniqueindentifier into a table using sql server?如何使用 sql server 将唯一标识符插入表中?
【发布时间】:2016-07-19 09:14:12
【问题描述】:

我已经有一个名为 a 的表,其中有一个名为 b 的列。 b 的类型是唯一标识符。我想在此列中插入一些内容。 但是,sql语句如下:

insert into a (b) 
values ('73C9B60E-27E4-4523-82A3-6992312348AE') 

失败。 我该如何解决这个问题?谢谢。

【问题讨论】:

  • 这对我来说似乎没问题,添加错误消息
  • “失败” - 失败如何?你得到0 row(s) inserted 吗?还是错误信息?如果是错误,它说什么

标签: sql uniqueidentifier


【解决方案1】:

uniqueidentifier 本质上是一个 GUID,因此您应该像这样格式化输入。 GUID 的格式为{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}。在 SQL 中,您可以省略括号。有关 GUID 的更多信息,请参阅 this wiki page

【讨论】:

  • 我已经有一个类似 '73C9B60E-27E4-4523-82A3-699239A478AE' 的 guid,如何插入它?
  • 好的,你的问题不是很清楚。您遇到了什么错误?
【解决方案2】:

您可以在 SQL Server 中创建GUID

select newid();

我已经有一个类似 '73C9B60E-27E4-4523-82A3-699239A478AE' 的 guid,如何插入它?

你的问题根本不清楚......但你可以这样做:

create table #test (myGuid uniqueidentifier)

insert into #test (myGuid)
select convert(uniqueidentifier, '73C9B60E-27E4-4523-82A3-699239A478AE')

drop table #test

【讨论】:

  • aaaaaaa 不是有效的uniqueidentifierUniqueidentifiersuniqueidentifiers。您必须在b 列中插入适当的类型。 newid() 是一种获取合适类型的方法
【解决方案3】:

您需要声明唯一标识符,它不会自动为您完成。

DECLARE @GUID uniqueidentifier

SET @GUID = NEWID()

INSERT INTO a VALUES (@GUID)

http://www.sqlteam.com/article/uniqueidentifier-vs-identity

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多