views:

39

answers:

2

It seems like there should be a query for this, but I can't think of how to do it.

I've got a table with a composite primary key consisting of two fields I'd like to populate with data,

I can do an insert into from one table to fill up half the keys, but I want to fill up the other half with a set of constants (0, 3, 5, 6, 9) etc...

so the end result would look like this

+--------------+
|AwesomeTable  |
+--------------+
| Id1   | Id2  |
| 1     | 0    |
| 1     | 3    |
| 1     | 5    |
| 1     | 6    |
| 1     | 9    | 
| 2     | 0    |
| 2     | 3    |
| ...   | ...  |
+--------------+   

I've got as far as insert into awesometable (id1, id2) select id1, [need something here] from table1 [need something else here]

+1  A: 

I've got a table with 2 primary keys

No, you don't. A table can only have one primary key. You probably mean a composite primary key.

I believe you want this:

INSERT
INTO    awesometable (id1, id2)
SELECT  t1.id1, q.id2
FROM    table1 t1
CROSS JOIN
        (
        SELECT  0 AS id2
        UNION ALL
        SELECT  3
        UNION ALL
        SELECT  5
        UNION ALL
        SELECT  6
        UNION ALL
        SELECT  9
        ) q

, or in Oracle:

INSERT
INTO    awesometable (id1, id2)
SELECT  t1.id1, q.id2
FROM    table1 t1
CROSS JOIN
        (
        SELECT  0 AS id2
        FROM    dual
        UNION ALL
        SELECT  3
        FROM    dual
        UNION ALL
        SELECT  5
        FROM    dual
        UNION ALL
        SELECT  6
        FROM    dual
        UNION ALL
        SELECT  9
        FROM    dual
        ) q
Quassnoi
A: 

If I understand correctly, maybe you can use something like this:

insert into awesometable (id1, id2) 
select id1, (select top 1 id2 from table2 where /*a condition here to retreive only one result*/) 
from table1
Parkyprg
Sorry, I may not have been being clear enough, there is no table 2. It's just a set of constants, maybe you'd call it a temporary table.
Peter Turner