1

I have to run multiple queries that are dependent on each other

select a from a ( result has multiple rows)
select b from b  ( result has multiple rows)

the for each row in that query i need to run this

select c from c where c.a=a amd c.b=b

 insert  c into d 

kinda like:

select a from a ( result has multiple rows)
select b from b  ( result has multiple rows 
  for a in a
      for b in b
          select c from c where c.a=a amd c.b=b
          insert  c,a,b into d 

is this possible to do as an stored procedure?

thank you

2
  • table a has multiple rows in the result and so as b Commented Nov 8, 2010 at 16:15
  • the final result should look like this table d: c,a,b Commented Nov 8, 2010 at 16:15

3 Answers 3

4

Stored procedure? Sure. Shoot, it's even possible in one statement:

INSERT INTO d (c)
  SELECT c FROM c WHERE c.a IN (SELECT a FROM a) AND c.b IN (SELECT b FROM b)

You can probably even do a little better with a join, but to write that I need to know more about how the tables relate.

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

Comments

3

I'm pretty sure you can write this query without using any loops in SQL.

Comments

1
INSERT
INTO    d
SELECT  c.*
FROM    a
CROSS JOIN
        b
JOIN    с
ON      c.a = a.id
        AND c.b = b.id

Comments

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.