newid()和别名TSQL

时间:2018-07-06 10:13:38

标签: sql-server tsql

我有这个问题

select newId() as code1,
       newId() as code2,
       parentId as code1

但是很明显,这件事行不通,如何在SELECT中两次使用相同的随机ID?

1 个答案:

答案 0 :(得分:1)

不确定我是否理解这个问题,但这两次选择了“相同的”随机代码。

;with cte as
(
    select 
        newId() as code1,
        C.*
    from
        contacts AS C
)
select 
    a.*,
    a.code1 AS code2 -- repeated same newId()
from 
    cte a