2016-08-18 102 views
1

插入多行我使用SQL Server 2008和與其他線程的幫助下,我已經能夠寫:基於SELECT語句

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不再爲真。

+3

這個問題不清楚,請看看[這裏](https://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-公共論壇/)如何改善問題.. – TheGameiswar

回答

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 
+1

作品一個款待,謝謝NEER – FredTheDog