SQL Select Into Field

somejkuser picture somejkuser · Dec 22, 2009 · Viewed 17k times · Source

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.

Answer

Gabriel McAdams picture Gabriel McAdams · Dec 22, 2009
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).