user492998
user492998

Reputation: 29

SQL create table and query from it

Morning all, I'm running SQL Server and there are a whole lot of tables in there. I have taken one column from one of these tables using SELECT, and it gives me the list of IDs. I need to use these IDs as the lookup point to get the data for that ID from another table. Is it necessary that I do a CREATE TABE manouvre?

I was hoping I could just use the data returned from the original SELECT statement without having to set up a new table....

Cheers.

Upvotes: 0

Views: 130

Answers (3)

Doggett
Doggett

Reputation: 3464

You can also use a inner join if you need additional values from the first table

select OtherTable.*, FirstTable.extrafield1, FirstTable.extrafield2
from OtherTable
inner join FirstTable on FirstTable.Id = OtherTable.FirstTableId 

Upvotes: 0

Dustin Laine
Dustin Laine

Reputation: 38503

You can use a few options, but most typical would be to create a temp table.

SELECT ID 
INTO #TempTable 
FROM table

Upvotes: 0

Andomar
Andomar

Reputation: 238086

You can use a where ... in construct to retrieve matching rows from the other table:

select  *
from    OtherTable
where   id in
        (
        select  id
        from    FirstTable
        )

Upvotes: 1

Related Questions