views:

25

answers:

1

I've just gone blank. I have a many to many relationship with three tables. Lets call them A, B and C. C stores the primary keys of the other two tables.

Now, I would like to insert all primary keys from table A, and lets say primary key 1 from B into table C. I just don't get it. All I came up with was some stored procedure that runs with a cursor through all primary keys of A and insert them with 1 into C. Guess, there is a much easier way to do this.

Any help appreciated!

+1  A: 

I would say:

INSERT INTO C
SELECT A_Id, 1
FROM A

where 1 is the single primary key from B.

Check here for more info on the INSERT statement.

Ronald Wildenberg
1 is a constant. Doesn't seem to work here...
Do you get a specific error message? Or should 1 not be a constant but the result of a query from B?
Ronald Wildenberg
it says syntax error near '1'.
That's strange. If I run the exact same statement on a test database, it works. We are talking about SQL Server here?
Ronald Wildenberg
my bad..thanks a lot I go to bed now.
Sleep well :) It's still morning here in Holland..
Ronald Wildenberg