Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 add inner join of 2 tables into a new table

Author  Topic 

djamit
Starting Member

17 Posts

Posted - 2013-08-02 : 11:58:54
Hi,

I am using a query to join to tables, but i want to put the query results into a new table. How should I do this?

This is the query:
Select Query.PAT_NAME,Query.IDA,Query.MD_INITIALS,Query.LOCATION,Query.App_DtTM,Query.Sch_Id,vw_PatBro_Admissions.Birth_DtTm,vw_PatBro_Admissions.IDA,vw_PatBro_Admissions.Gender,vw_PatBro_Admissions.MDName
from Query
inner join vw_PatBro_Admissions
on Query.IDA=vw_PatBro_Admissions.IDA

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-08-02 : 12:22:00
you can do something like this:

[CODE]

-- CREATE A TABLE USING THE CORRECT DATA TYPES
CREATE TABLE dbo.Temp101 (
PAT_NAME VARCHAR(20),
IDA VARCHAR(20),
MD_INITIALS VARCHAR(20),
LOCATION VARCHAR(20),
App_DtTM VARCHAR(20),
Sch_Id VARCHAR(20),
Birth_DtTm DATE,
IDA2 VARCHAR(20),
Gender VARCHAR(2),
MDName VARCHAR(20)
);

INSERT INTO dbo.Temp101(
Select
Query.PAT_NAME,
Query.IDA,
Query.MD_INITIALS,
Query.LOCATION,
Query.App_DtTM,
Query.Sch_Id,
vw_PatBro_Admissions.Birth_DtTm,
vw_PatBro_Admissions.IDA,
vw_PatBro_Admissions.Gender,
vw_PatBro_Admissions.MDName
from
Query
inner join
vw_PatBro_Admissions ON Query.IDA = vw_PatBro_Admissions.IDA
)


-- DROP THE TABLE WHEN YOU ARE DONE
DROP TABLE dbo.Temp101
[/CODE]
Go to Top of Page

djamit
Starting Member

17 Posts

Posted - 2013-08-02 : 14:01:13
THANKS
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-08-02 : 23:46:52
the easiest way to create a table using query on the fly would be using SELECT... INTO syntax.


Query.PAT_NAME,Query.IDA,Query.MD_INITIALS,Query.LOCATION,Query.App_DtTM,Query.Sch_Id,vw_PatBro_Admissions.Birth_DtTm,vw_PatBro_Admissions.IDA,vw_PatBro_Admissions.Gender,vw_PatBro_Admissions.MDName
INTO NewTableName
from Query
inner join vw_PatBro_Admissions
on Query.IDA=vw_PatBro_Admissions.IDA


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -