【问题标题】:INSERT multiple rows based on SELECT statement根据 SELECT 语句插入多行
【发布时间】:2016-08-18 19:00:09
【问题描述】:

我正在使用 SQL Server 2008,在其他线程的帮助下,我已经能够编写以下内容:

insert into fml0grant (auto_key, roleid)
    select fml0.auto_key, 20 
    from fml0
    left join fml0grant on fml0.auto_key = fml0grant.auto_key
    where fml0.dwgname <> ''
      and fml0grant.roleid is null

但是,我需要为 where 子句中找到的每条记录插入多行。所以当 where 子句得到结果时,我需要插入:

  1. fml0.auto_key, 20
  2. fml0.auto_key, 508
  3. fml0.auto_key, 10

有没有办法将所有三个插入组合到一个语句中,因为在我的查询中的第一个语句之后,WHERE 子句中的 NULL 不再为真。

【问题讨论】:

  • 这个问题我不清楚,请看here如何改进问题..

标签: sql sql-server-2008


【解决方案1】:

您可以使用CROSS JOIN,如下所示。

insert into fml0grant (auto_key, roleid)
    select fml0.auto_key, V.Id 
    from fml0
    left join fml0grant on fml0.auto_key = fml0grant.auto_key
    CROSS JOIN (VALUES (20),(508),(10)) V (Id)
    where fml0.dwgname <> ''
      and fml0grant.roleid is null

【讨论】:

  • 工作愉快,谢谢NEER
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-11
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
相关资源
最近更新 更多