| Author |
Topic |
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-07-16 : 06:38:53
|
| 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 arein Table_Jan. any idea? Thanks |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-16 : 06:41:43
|
| select a.*into newtablefrom Table_Jan aleft join Table_Feb bon a.id = b.idwhere b.id is nullDo 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. |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-07-16 : 06:47:42
|
| Sorry for asking, I might make a mistake with a.*Which is a.* Value for you? Many thanks |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-16 : 06:50:38
|
| 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. |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-07-16 : 07:27:40
|
| Perfect. Using that query I will have all the ID's in Jan that they are not in FEb? select a.*from Table_Jan aleft join Table_Feb bon a.id = b.idwhere b.id is nullThanks a lot nigelrivett |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-16 : 07:42:11
|
| That's right.Another way of doing it isselect *from Table_Janwhere 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. |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-07-16 : 07:54:57
|
| Perfect, What happens in my case using select a.*into newtablefrom Table_Jan aleft join Table_Feb bon a.id = b.idwhere b.id is nullis that i cant create the new table: msg 2705, level 16, state 3, line 1column name must be uniqueI tried in different ways, but still the same issue. Sorry for asking too much.. Many thanks |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-16 : 09:17:00
|
| 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. |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-07-16 : 09:34:10
|
| Yes,The query is as follows: SELECT * INTO new_tableFROM Table_JanINNER JOIN Table_FebON table_jan.ID= table_feb.idWHERE table_feb.id IS NULLIm sure Im missing something, but i dont know what.. THanks a lot |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-16 : 09:37:46
|
| >> 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 toSELECT a.*INTO new_tableFROM Table_Jan aLeft JOIN Table_Feb bON a.ID= b.idWHERE 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. |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-07-16 : 09:54:42
|
| 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 tableBut Im asking twice to the SQL :SELECT * FROM Table_JanINTO new_tableFROM Table_JanLEFT JOIN Table_FebON table_jan.ID= table_feb.idWHERE table_feb.id IS NULLSorry again nigelrivett |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-16 : 09:58:10
|
| SELECT a.* = SELECT * FROM Table_Jan , It's correct?No.select a.* returns the columns from aselect * 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 thenSELECT Table_Jan.*FROM Table_JanINTO new_tableFROM Table_JanLEFT JOIN Table_FebON table_jan.ID= table_feb.idWHERE 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. |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-07-16 : 10:17:49
|
| Thanks for your replies and your time nigelrivettAfter 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_tableFROM Table_JanLEFT JOIN Table_FebON table_jan.ID= table_feb.idSorry again and thanks nigelrivett |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-16 : 10:21:26
|
| You need the where clause.SELECT Table_Jan.*INTO new_tableFROM Table_JanLEFT JOIN Table_FebON table_jan.ID= table_feb.idwhere table_feb.id is nullWhy 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. |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-07-16 : 10:35:33
|
| 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. |
 |
|
|
|