SQL子查询不正确的语法将列转换为NewID()值

时间:2013-08-05 18:18:38

标签: sql-server-2008 select

CREATE TABLE dbo.Ids
( 
   DlId nvarchar(32) NOT NULL PRIMARY KEY,
   DlIdGUID uniqueidentifier NOT NULL
)

INSERT INTO dbo.Ids
(DlId, REPLACE(DlId, DlId, newid()))
(SELECT DISTINCT TOP 5 DriversLic FROM Student.dbo.StudDL
WHERE DlId LIKE '%345%')

错误说

  

'('

附近的语法不正确

1 个答案:

答案 0 :(得分:0)

插入匹配的DriversLic和新GUID;

INSERT INTO dbo.Ids (DlId, DlIdGUID)
    (SELECT DISTINCT TOP 5 DriversLic, NEWID() 
     FROM Student.dbo.StudDL 
     WHERE DlId LIKE '%345%')

使用DISTINCT

;with T(DriversLic) as (
    select distinct top 5 DriversLic from StudDL 
)
insert Ids  
    select DriversLic, NEWID() from T