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
 General SQL Server Forums
 New to SQL Server Programming
 select into

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-08-08 : 22:34:03
So i have the following query which i'm using in various ways to find records in one table that are not in another:


SELECT offenseno FROM offense.dbo.off_per t1
WHERE OFFENSENO not IN (SELECT OFFENSENO FROM offense.dbo.offense t2 WHERE t2.OFFENSENO=t1.OFFENSENO)


i'm wanting the resultset for the times i run this against different tables to be dropped into a new table. I've tried tacking a "Select * into " portion at the beginning and turning the above into a subselect, but I'm in syntax hell. So, how do i use the above to make a select into for a new table?

thanks

chadmat
The Chadinator

1974 Posts

Posted - 2012-08-08 : 22:52:52
SELECT offenseno INTO #Table FROM offense.dbo.off_per
EXCEPT
SELECT OFFENSENO FROM offense.dbo.offense

-Chad
Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-08-08 : 23:03:55
Sorry, my original query was inaccurate. i'm actually selecting ALL columns from off_per into the new table where the offenseno value is not in offense..offense

So, based on that, i've tried to tweak the solution you provided, but to no avail.
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2012-08-08 : 23:09:30
SELECT <ColumnList> INTO #Table FROM offense.dbo.off_per
WHERE offenseno NOT IN (SELECT OFFENSENO FROM offense.dbo.offense)


-Chad
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-08 : 23:21:37
i'm wanting the resultset for the times i run this against different tables to be dropped into a new table

do you mean repeating logic for several tables to populate a common destination table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-08-09 : 09:54:14
no, basically i'm just modifying the script to run it against different tables, but it should provide the same thing. The last solution provided by Chad seems to work. Thanks all
Go to Top of Page
   

- Advertisement -