0

我想知道我是否可以获取一个已经创建的临时表(出于本练习的目的,我们假设我不能简单地在代码中添加任何内容)并使用 ALTER 添加一列,然后更新该新创建的列?像这样的东西

 create table #test (first int, second int)

 insert into #test (first, second) values (24,8439)
 GO

 alter table #test ADD SymCount [uniqueidentifier] NULL
 GO

 update #test
 Set SymCount = (Select distinct count(first) from #test)

SELECT * From #test

drop table #test

当我尝试更新时,它显然没有SymCount作为一列重新识别......这是不支持还是我做错了什么?关于为什么以及如何创建解决方法的任何见解都会非常有帮助。

提前致谢

4

3 回答 3

1

该错误是因为您将 int 设置为 uniqueidentifier。检查以下代码:

drop table #test
go

 create table #test (first int, second int)

 insert into #test (first, second) values (24,8439)

 GO

 alter table #test ADD SymCount [uniqueidentifier] NULL

 GO

 insert into #test select 1,1,newid(); -- works

 update #test
 Set SymCount = (Select distinct count(first) from #test) -- you can't do that - syntax error : Operand type clash: int is incompatible with uniqueidentifier. Int can't be set to uniqueidentifier

SELECT * From #test

drop table #test
于 2013-11-01T14:22:51.483 回答
1

SQL2008 - 操作数类型冲突:int 与 uniqueidentifier 不兼容。

'1' 不能用作唯一标识符值。尝试将 [uniqueidentifier] 更改为 [int]

来自http://technet.microsoft.com/en-us/library/ms187942.aspx

“唯一标识符是一个 16 字节的 GUID,可以通过以下方式初始化为一个值:

  1. 通过使用 NEWID 函数。

  2. 通过从 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 形式的字符串常量转换,其中每个 x 是 0-9 或 af 范围内的十六进制数字。例如,6F9619FF-8B86-D011-B42D-00C04FC964FF 是一个有效的唯一标识符值。”

于 2013-11-01T14:34:51.617 回答
0

您有混合数据类型。您正在尝试将整数分配给 uniqueidentifier 字段。

于 2013-11-01T14:21:16.767 回答