1

I have three tables, A B and C. For every entry in A x B (where x is a Cartesian product, or cross join) there is an entry in C.

In other words, the table for C might look like this, if there were 2 entries for A and 3 for B:

| A_ID | B_ID | C_Val |
----------------------|
|  1   |  1   |  100  |
|  1   |  2   |  56   |
|  1   |  3   |  19   |
|  2   |  1   |  67   |   
|  2   |  2   |  0    |
|  2   |  3   |  99   |

Thus, for any combination of A and B, there's a value to be looked up in C. I hope this all makes sense.

In practice, the size of A x B may be relatively small for a database, but far too large to populate by hand for testing data. Thus, I would like to randomlly populate C's table for whatever data may already be in A and B.

My knowledge of SQL is fairly basic. What I've determined I can do so far is get that cartesian product as an inner query, like so:

(SELECT B.B_ID, C.C_ID
 FROM B CROSS JOIN C)

Then I want to say something like follows:

INSERT INTO A(B_ID, C_ID, A_Val) VALUES
    (SELECT B.B_ID, C.C_ID, FLOOR(RAND() * 100)
     FROM B CROSS JOIN C)

Not surprisingly, this doesn't work. I don't think its valid syntax to genereate a column on the fly like that, nor to try to insert a whole table as values.

How can I basically convert this normal programming pseudocode to proper SQL?

foreach(A_ID in A){
    foreach(B_ID in B){
        C.insert(A_ID, B_ID, Rand(100));
    }
}
0

3 Answers 3

3

The syntax problem is because:

INSERT INTO A(B_ID, C_ID, A_Val) VALUES
  (SELECT B.B_ID, C.C_ID, FLOOR(RAND() * 100)
   FROM B CROSS JOIN C)

Should be:

INSERT INTO A(B_ID, C_ID, A_Val) 
  SELECT B.B_ID, C.C_ID, FLOOR(RAND() * 100)
   FROM B CROSS JOIN C;

(You don't use VALUES with INSERT/SELECT.)

However you will still have the problem that RAND() is not evaluated for every row; it will have the same value for every row. Assuming the combination of B_ID and C_ID is unique, you can use something like this:

INSERT INTO A(B_ID, C_ID, A_Val) 
  SELECT B.B_ID, C.C_ID, ABS(CHEKSUM(RAND(B.B_ID*C.C_ID))) % 100
   FROM B CROSS JOIN C;
2
  • It will take some time..I guess to get used maybe untill we get a score of 45.8 K like Aaron :).I am still not used to comments.I always forget and press enter and my comment get posted.With comment worst thing is that it can be edited just for five minutes...
    – Gulli Meel
    Jul 23, 2012 at 15:09
  • Working answer and great explanation too. Thanks!
    – Nick
    Jul 23, 2012 at 15:14
2
select A_id,B_Id, abs(checksum(newid()))%101 as C_val from A cross join B

This will give you different values in ranmge 0 to 100

0

Use CTE

  With cte as
  (SELECT B.B_ID, C.C_ID, ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) as A_Val
    FROM B CROSS JOIN C) 
  Insert into Table(B_ID, C_ID, A_Val)
  Select B_ID,C_ID,A_Val from cte

Since rand generates the same number you can use NEWID .Source

4
  • This is a whole lot of extra scaffolding for a simple INSERT/SELECT. And doesn't solve the RAND() issue. Jul 23, 2012 at 14:57
  • Then possibly he can use NEWID which will return different value for each row
    – praveen
    Jul 23, 2012 at 15:01
  • Maybe, but your solution doesn't really solve either problem IMHO. All you've done is make the syntax needlessly complex. Jul 23, 2012 at 15:08
  • CTE and apply are two features which are sometimes used unnecessarily..I guess it now becomes habit to use these..
    – Gulli Meel
    Jul 23, 2012 at 15:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.