2

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

2 Answers 2

1

I want to use the IDs I get from this query to insert in another table Member which use ContaId as a foreign key.

You can use INSERT INTO .. SELECT instead of cursors and while loops like so:

INSERT INTO Member(ContaId)
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%'

Update: Try this:

INSERT INTO Member(ContaId, PromoId)
SELECT TOP 1000 c.ContaId, 91 AS PromoId
FROM FastGroupe fg
...

This will insert the same value 91 for the PromoId for all the 1000 records. And since the MemberId is set to be automatic, just ignore it in the columns' list and it will get an automatic value.

Sign up to request clarification or add additional context in comments.

2 Comments

wow so simple, but in the table Member, I have other columns which don't come from the SELECT query. Will that be a problem ?
@the_ruby_racer, It will be a problem, because the select statement will give you 1000 ids, for the other columns which don't come from the select query. How did you will get them? You have to put them by somehow within this select statment. By JOIN or UNION. Can you please explain more?
0

You need only to make sure that number of columns match the selected data in the subquery

the Syntax is like this:

Insert into YourTable(calumn1, column2, ..., columnN) 
values (Select (calumn1, column2, ..., columnN) from ... ) // The Subquery

2 Comments

in the table Member, I have other columns which don't come from the SELECT query. Will that be a problem ?
IT depends if these are Not Nullabel or Constraint Columns you will have to decide what values get there. Otherwise this will be no problem you will have only to write as much columns after Insert as much as you select in the Subquery

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.