2

I have a 4 tables. One of the tables we are going to be inserting data into (Table A). Table A is going to receive misc data from Table B, C, D and also some unknown variable parameter data.

How do I set up the INSERT with a SELECT with also receiving parameters?

2 Answers 2

4

Something like this?

Insert INTO TableA (col1, col2,col3,col4)
  SELECT b.col1, c.col2, d.col3, @myparam
  FROM TableB as b
  INNER JOIN TableC as c
    ON b.id = c.id
  INNER JOIN TableD as d
    on c.id = d.id
Sign up to request clarification or add additional context in comments.

3 Comments

But there are going to be parameters to be inserted in to TableA as well, not just based on a where clause. TableA (col1 = value from parameter, col2 = value from tableb, col3 = value from table c)
Just put it in the select. See my updated. You can just throw a parameter into any select statement, it does not need to somehow be part of the table.
Thank you so much. I believe I see where your coming from now. I will try in the morning and give you kudos and checkmarks :D I cant thank you enough. This was my last task for the day and was not getting anywhere with it until you come along. A big thank you to you :)
0

Something like this:

DECLARE @a int, @b int
SET @a = 5
SET @b = 7

INSERT INTO TableA(Column1, Column2)
SELECT SomeOtherColumn, @a
FROM TableB
UNION
SELECT YetAnotherColumn, @b
FROM TableC

2 Comments

Yes but, @a is coming from a web service not from tableB. Maybe Im missing something. Ive been programming for over 12 hours straight today and sort of hallucinating right now, LOL
@ArgyleGhost No @a and @b aren't coming from the tables, they're the variables I declared above. I was just demonstrating how they could be used whilst mixing them with the columns selected from one of your other tables!

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.