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.
| Author |
Topic |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2010-01-05 : 05:18:53
|
| /*In the last part of the below sql, you see that I am trying to add the new data but I am not sure how to do this.Note that the schema of the two tables are identical.Can you let me know how to do the insert of the new data in i.e. @tblNewData into @tblFinancials in the last part please.Thanks*/declare @tblFinancial table (ID int, Code varchar(20), Amount decimal(15, 2), Setting tinyint)insert into @tblFinancialselect 1,'xyz', 23.95, 3unionselect 1,'abc', 99.45, 2unionselect 1,'abc', 87.33, 2select 'Financials'select * from @tblFinancialdeclare @tblNewData table (ID int, Code varchar(20), Amount decimal(15, 2), Setting tinyint)insert into @tblNewDataselect 1,'abc', 99.45, 3unionselect 1,'abc', 87.33, 3unionselect 2,'TRE', 100.34, 3unionselect 1,'KWE', 54.22, 3select 'NewData'select * from @tblNewdata--Add new trades...--insert into @tblFinancial--select-- nd.ID, nd.Code, nd.Amount, nd.Setting--from -- @tblNewData as nd-- right join @tblFinancial as f-- on -- f.ID = nd.ID-- and f.Code = nd.Code-- and f.Amount = nd.Amount-- and f.Setting = nd.Setting--whereselect 'inserted Data'select * from @tblFinancial |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-05 : 05:24:39
|
isnt it just matter ofinsert into @tblFinancialselect n.ID, n.Code , n.Amount , n.Setting from @tblNewData nleft join @tblFinancial fon f.ID=n.IDand f.Code=n.Code and f.Amount=n.Amount and f.Setting = n.Settingwhere f.ID is null |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-01-05 : 05:27:15
|
left join @tblFinancial as f on f.ID = nd.ID and f.Code = nd.Code and f.Amount = nd.Amount and f.Setting = nd.Settingwhere f.ID is null No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-01-05 : 05:28:25
|
too late No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2010-01-05 : 05:30:07
|
| Thank you.I forgot about the where f.ID is null |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-01-05 : 05:31:45
|
And have a look about left join - not right join! No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|