0

im trying to insert values into a table in sql in one run.

INSERT INTO sampleTable
       (  
       ,ID
       ,aa
       ,bb
       ,cc
       ,dd
       ,ee
       )  
      SELECT     
       ,(select id from otherTable where value="something")
       ,aa  
       ,bb
       ,cc  
       ,dd 
       ,ee 

how do i loop it in sql that it inserts values for each id on the otherTable?

4

1 回答 1

0
INSERT INTO sampleTable
       (  
       ,ID
       ,aa
       ,bb
       ,cc
       ,dd
       ,ee
       )  
      SELECT     
       ,id 
       ,aa  
       ,bb
       ,cc  
       ,dd 
       ,ee 
from otherTable where value="something"

Explanation: If you want to SELECT..INSERT multiple lines (a set) you need to have multiple lines in your SELECT statement. This will only work with a FROM part of the query.

The best way to test INSERT..SELECT is to remove the insert part and see if it works by itself. Once you are happy with the result you can add the INSERT part in front of it.

于 2013-10-23T03:24:30.763 回答