1

I am using SQL Server 2008 and with the help of other threads I have been able to write the following:

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

However what I need to do is insert multiple rows for each record found in the where clause. So when the where clause gets a result I need to insert:

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

Is there any way to combine all three inserts into one statement as after the first in my query the NULL in the WHERE clause is no longer true.

1
  • 3
    This question is not clear to me,please look here on how to improve question.. Commented Aug 18, 2016 at 10:51

1 Answer 1

1

You can use CROSS JOIN as the below.

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
Sign up to request clarification or add additional context in comments.

1 Comment

Works a treat, Thanks NEER

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.