views:

212

answers:

4

I want to accomplish something of the following:

Select DISTINCT(tableA.column) INTO tableB.column FROM tableA

The goal would be to select a distinct data set and then insert that data into a specific column of a new table.

+2  A: 

You're pretty much there.

SELECT DISTINCT column INTO tableB FROM tableA

It's going to insert into whatever column(s) are specified in the select list, so you would need to alias your select values if you need to insert into columns of tableB that aren't in tableA.

SELECT INTO

Justin Swartsel
+1  A: 

Try the following...

INSERT INTO tableB (column)
Select DISTINCT(tableA.column)
FROM tableA
pdavis
+1  A: 
SELECT column INTO tableB FROM tableA

SELECT INTO will create a table as it inserts new records into it. If that is not what you want (if tableB already exists), then you will need to do something like this:

INSERT INTO tableB (
column
)
SELECT DISTINCT
column
FROM tableA

Remember that if tableb has more columns that just the one, you will need to list the columns you will be inserted into (like I have done in my example).

Gabriel McAdams
A: 
Dave Quick