【发布时间】:2016-02-12 18:39:59
【问题描述】:
使用 MS Sql Server 2008 我正在尝试使用花哨的插入/选择语法将多行插入到 users_roles 表中。执行此查询时,我收到错误
子查询返回超过 1 个值。这是不允许的,当 子查询遵循 =、!=、、>= 或当子查询用作 一个表达式。
两个子查询在单独执行时都会返回预期值。第一个子查询中有多个记录,第二个子查询中有一个记录。
insert into users_roles(userid, roleid)
select
(select distinct users.id as userID from users
inner join users_roles on users.id = users_roles.userid
inner join roles on users_roles.roleid = roles.id
where roles.projectid = 1)
,
(select id as roleID from roles where projectid = 1 and name = 'ALL')
我在这里错过了什么?
【问题讨论】:
-
所以你想要第 1 行(多个值,1 个值)之类的东西是不可能的。您需要重新考虑您的查询并避免使用
TOP 1进行修复
标签: sql-server tsql select sql-insert