I want to insert records in a table with certain values which I got from a SELECT query on other other tables.
SELECT TOP 1000 c.ContaId
FROM FastGroupe fg
INNER JOIN FastParticipant fp
ON fg.FastGroupeId = fp.FastGroupeId
INNER JOIN Participant p
ON fp.ParticipantId = p.ParticipantId
INNER JOIN Contact c
ON p.ContaId = c.ContaId
WHERE FastGroupeName like '%Group%'
I want to use the IDs I get from this query to insert in another table Member which use ContaId as a foreign key.
I want to use a WHILE loop, but I don’t know the number of records I will get from the SELECT query.
So, is there a workaround for this problem.
EDIT
This is the table Member
CREATE TABLE [dbo].[Request](
[MemberId] int IDENTITY(1,1) NOT NULL,
[ContaId] int NOT NULL,
[PromoId] int NOT NULL
);
The PromoId is a non null column, but I want to put the same value for all the records I am about to create.
The Member records have to be like this
MemberId = Automatic,
ContaId = // one of the query results,
PromoId = 91
You can use
INSERT INTO .. SELECTinstead of cursors and while loops like so:Update: Try this:
This will insert the same value
91for thePromoIdfor all the 1000 records. And since theMemberIdis set to be automatic, just ignore it in the columns’ list and it will get an automatic value.