ColdFusion: How to insert a list with a static foreign key in one insert?

前端 未结 4 1414
北海茫月
北海茫月 2021-01-21 10:45

I have a list like this thing1,thing2,thing3. And I want to insert them into a look-up table with the same foreign key. So ideally it would look like this:



        
4条回答
  •  清酒与你
    2021-01-21 11:02

    I was getting a database error with

    INSERT INTO lkp_things (foreign_key,thing) 
    VALUES (1,'thing1'),(1,'thing2')
    

    Leigh helped me realize that I was actually using SQL Server '00, and that version does not allow this method of insertion. So, I had to use this:

    INSERT INTO lkp_things (foreign_key,thing)
    SELECT 1,'thing1'
        UNION ALL
    SELECT 1,'thing2'
    

    In CF that looks like this:

    
    
        INSERT INTO lkp_Things (id,thing)
        
            SELECT ,
            UNION ALL
        
    
    

    I found this solution here: How do I insert multiple rows WITHOUT repeating the "INSERT INTO dbo.Blah" part of the statement?

提交回复
热议问题