Sql 是否可以在临时表中添加一列(使用alter),然后立即更新该列?

Sql 是否可以在临时表中添加一列(使用alter),然后立即更新该列?,sql,sql-server,Sql,Sql Server,我想知道我是否可以在本练习中使用一个已经创建的临时表让我们假设我不能简单地在代码中添加任何内容,并使用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 #te

我想知道我是否可以在本练习中使用一个已经创建的临时表让我们假设我不能简单地在代码中添加任何内容,并使用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重新识别为列。。。这是不受支持还是我做错了什么?任何关于为什么以及如何创建一个解决方案的见解都会非常有用


提前感谢

您有混合数据类型。您正试图为uniqueidentifier字段分配一个整数。

错误是因为您将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

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

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

发件人:

Uniqueidentifier是一个16字节的GUID,可以通过以下方式初始化为值:

通过使用NEWID函数

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


我猜你实际上想要先从测试中选择countdistinct…正如dan指出的,你需要将SymCount切换为int而不是uniqueidentifier,因为quid和int不能很好地相互作用。但我不确定这会有多大价值。这不只是创建一个新行吗?是否可以在新创建的“SymCount”上使用更新?是的,它将创建一个新行。是的,您也可以进行更新,但需要将uniqueidentifier传递给uniqueidentifier列。