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 |
|
yolip
Starting Member
4 Posts |
Posted - 2006-05-03 : 21:38:49
|
| Can anyone tell me how to make this work?SELECT MAX(AssetID) + 1 AS newkeyFROM tbAssetWHERE (newkey NOT IN(SELECT KeyInUseFROM tbKeyInUse))thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-03 : 21:44:53
|
[code]SELECT newkeyFROM( SELECT MAX(AssetID) + 1 AS newkey FROM tbAsset) nWHERE NOT EXISTS (SELECT KeyInUse FROM tbKeyInUse x WHERE x.KeyInUse = n.newkey)[/code] KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-04 : 01:48:52
|
quote: Originally posted by yolip Can anyone tell me how to make this work?SELECT MAX(AssetID) + 1 AS newkeyFROM tbAssetWHERE (newkey NOT IN(SELECT KeyInUseFROM tbKeyInUse))thanks
Did you get error or wrong result?MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-04 : 01:51:38
|
quote: WHERE (newkey NOT IN(SELECT KeyInUseFROM tbKeyInUse))
It would gives error as newKey is a column alias KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-04 : 02:05:49
|
Thanks. Yes it is. Other way would be SELECT MAX(AssetID) + 1 AS newkeyFROM tbAssethaving (MAX(AssetID) + 1 NOT IN(SELECT KeyInUseFROM tbKeyInUse))MadhivananFailing to plan is Planning to fail |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-04 : 05:50:50
|
Hi all,Yolip - This seems like an odd request to me (since no rows will sometimes be returned), so I wonder if you meant to ask a different question. It's just a guess, but maybe you want get the 'next' key (for tbAsset) which isn't already in use (i.e. in tbKeyInUse).If so, then this might be of help...  --datadeclare @tbKeyInUse table (KeyInUse int)insert @tbKeyInUse select 1union all select 4union all select 5union all select 7declare @tbAsset table (AssetID int)insert @tbAsset select 1union all select 2union all select 3--calculationdeclare @numbers table (i int identity(1, 1), j bit)insert @numbers select top 1000 null from master.dbo.syscolumns a, master.dbo.syscolumns bselect min(i) from @numberswhere i > (select max(AssetID) from @tbAsset) and i not in (select KeyInUse from @tbKeyInUse) Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|
|
|