views:

130

answers:

3

I tried to make the title as clear as possible... here is my scenario:

I have 2 tables (let's call them table A and table B) that have a similar schema. I would like write a stored procedure that would select specific columns of data out of table A, and insert this data as a new record in table B.

Can someone point me in the write direction to make such a query? I am unsure how to "Hold" the values from the first query, so that I may then perform the insert.

I am trying to avoid making a query, processing it with C# and then making another query...

Thanks.

+4  A: 

You can do this as a single query from C# like this:

Insert into tableB (col1, col2, col3) select col1, col2, col3 from tableA where ...

The trick is that column names need to be in the same order and compatible types.

John Stauffer
+5  A: 
INSERT INTO B (Col1, Col2) SELECT Col1, Col2 FROM A

Is this what you mean?

Keith Walton
A: 

use a SELECT INTO

SELECT
    [Col1],
    [COl2]
INTO TableA
FROM TableB
Jared
select into only works if the table does not already exist
HLGEM