SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Copy all info if not
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jfm
Posting Yak Master

134 Posts

Posted - 07/16/2012 :  06:38:53  Show Profile  Reply with Quote
Hi there,

I need to copy into a new_table all the data is not in Table_feb from Table_jan.

We use column id, which is in both tables, to check if the id's in Table_Feb are
in Table_Jan.

any idea?

Thanks

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/16/2012 :  06:41:43  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
select a.*
into newtable
from Table_Jan a
left join Table_Feb b
on a.id = b.id
where b.id is null

Do you see a problem with your design here?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 07/16/2012 :  06:47:42  Show Profile  Reply with Quote
Sorry for asking,

I might make a mistake with a.*

Which is a.* Value for you?


Many thanks
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/16/2012 :  06:50:38  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
a is an alias for the table Table_Jan, a.* is all columns from that table.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 07/16/2012 :  07:27:40  Show Profile  Reply with Quote
Perfect.

Using that query I will have all the ID's in Jan that they are not in FEb?

select a.*
from Table_Jan a
left join Table_Feb b
on a.id = b.id
where b.id is null

Thanks a lot nigelrivett
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/16/2012 :  07:42:11  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
That's right.
Another way of doing it is

select *
from Table_Jan
where ID not in (select ID from Table_Feb)

That only works if you have a single join column though.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 07/16/2012 :  07:54:57  Show Profile  Reply with Quote
Perfect,

What happens in my case using

select a.*
into newtable
from Table_Jan a
left join Table_Feb b
on a.id = b.id
where b.id is null

is that i cant create the new table:

msg 2705, level 16, state 3, line 1
column name must be unique

I tried in different ways, but still the same issue.

Sorry for asking too much..

Many thanks
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/16/2012 :  09:17:00  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Is Table_Jan a table?
Are you sure that is what you are running?
Do you have select * rather than select a.*?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 07/16/2012 :  09:34:10  Show Profile  Reply with Quote
Yes,

The query is as follows:

SELECT * INTO new_table
FROM Table_Jan
INNER JOIN Table_Feb
ON table_jan.ID= table_feb.id
WHERE table_feb.id IS NULL

Im sure Im missing something, but i dont know what.. THanks a lot
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/16/2012 :  09:37:46  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
>> Do you have select * rather than select a.*?

Yes you do and that is the problem.
You've also changed the left join to an inner join so you won't get any rows returned.

Change it to
SELECT a.*
INTO new_table
FROM Table_Jan a
Left JOIN Table_Feb b
ON a.ID= b.id
WHERE b.id IS NULL


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 07/16/2012 :  09:54:42  Show Profile  Reply with Quote
Sorry for the INNER JOIN, I'm running another query too.

I don't understand the first part:

SELECT a.* = SELECT * FROM Table_Jan , It's correct?

>> a is an alias for the table Table_Jan, a.* is all columns from that table

But Im asking twice to the SQL :

SELECT * FROM Table_Jan
INTO new_table
FROM Table_Jan
LEFT JOIN Table_Feb
ON table_jan.ID= table_feb.id
WHERE table_feb.id IS NULL

Sorry again nigelrivett
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/16/2012 :  09:58:10  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
SELECT a.* = SELECT * FROM Table_Jan , It's correct?
No.
select a.* returns the columns from a
select * returns the columns from all tables involved. In this case you have two tables with the same column names hence the error.
The values from b will be null anyway so no point in including them.

If you really don't want to alias then

SELECT Table_Jan.*
FROM Table_Jan
INTO new_table
FROM Table_Jan
LEFT JOIN Table_Feb
ON table_jan.ID= table_feb.id
WHERE table_feb.id IS NULL


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 07/16/2012 :  10:17:49  Show Profile  Reply with Quote
Thanks for your replies and your time nigelrivett

After SELECT I have removed FROM Table_Jan because if not I have "msg 156, level 15, state 1, line 2" Removing the first FROM Table_Jan, the values that I have in my new_table, are the same as the ones from Table_jan. It seems the query is not crossing the info.

SELECT Table_Jan.*
INTO new_table
FROM Table_Jan
LEFT JOIN Table_Feb
ON table_jan.ID= table_feb.id

Sorry again and thanks nigelrivett
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/16/2012 :  10:21:26  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
You need the where clause.

SELECT Table_Jan.*
INTO new_table
FROM Table_Jan
LEFT JOIN Table_Feb
ON table_jan.ID= table_feb.id
where table_feb.id is null

Why don't yoou try running what I first posted - it shoould work.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.

Edited by - nigelrivett on 07/16/2012 10:22:04
Go to Top of Page

jfm
Posting Yak Master

134 Posts

Posted - 07/16/2012 :  10:35:33  Show Profile  Reply with Quote
Perfect.

It's the same as the first one that you posted.

The value that I have is too high. That's why I was trying to create another query.

Thanks for your time.

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000