| Author |
Topic |
|
havey
Starting Member
16 Posts |
Posted - 2007-11-30 : 00:04:14
|
Hi i would like to query multiple tables for the top1 record, this is what i have created so far, but its not working, any suggestions:CREATE PROCEDURE dbo.sp_getMCpage1(@MemberID int) ASSET NOCOUNT ONSelect TOP 1 * from MCPage1, MCPage1a, MCPage1b, MCPage1c, MCPage1d, MCPage1e, MCPage1fwhere MCPage1.MemberID=@MemberIDRETURNGO Thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-11-30 : 00:07:59
|
quote: from MCPage1, MCPage1a, MCPage1b, MCPage1c, MCPage1d, MCPage1e, MCPage1f
where is the JOIN condition for these tables ?What do you want to do here ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2007-11-30 : 00:08:38
|
| select top 1 * from MCPage1union allselect top 1 * from MCPage1a...etcTop 1 does not mean anyting without an order by on something unique so you'd want something likeselect * from (select top 1 * from MCPage1 order by x)union allselect * from (select top 1 * from MCPage1a order by x)Sounds like a very dodgy db design to me though. |
 |
|
|
havey
Starting Member
16 Posts |
Posted - 2007-11-30 : 00:14:33
|
| ok i'm a little lost so excuse me, there are the aformentioned tables, they all have info for a particular MemberID, i would like sql to grab all the info in the tables in regards to that member id.I thought using Top 1 ment that when sql found the record that it would stop scanning the table looking for another record with the same id, am i wrong? just thought it was more effecient, and also thought that didnt need to order by with top 1, am i wrong again?Thanks |
 |
|
|
evilDBA
Posting Yak Master
155 Posts |
Posted - 2007-11-30 : 02:29:37
|
| Also, it is not recommended to call procedures sp_ . |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-30 : 03:29:41
|
quote: Originally posted by havey ok i'm a little lost so excuse me, there are the aformentioned tables, they all have info for a particular MemberID, i would like sql to grab all the info in the tables in regards to that member id.I thought using Top 1 ment that when sql found the record that it would stop scanning the table looking for another record with the same id, am i wrong? just thought it was more effecient, and also thought that didnt need to order by with top 1, am i wrong again?Thanks
Do you want one row for each id for each table?select t.columns from(Select columns,row_number() over(partition by id order by id) as rownum from table) as twhere rownum=1You can apply the same in all tablesMadhivananFailing to plan is Planning to fail |
 |
|
|
havey
Starting Member
16 Posts |
Posted - 2007-11-30 : 11:52:10
|
quote: Do you want one row for each id for each table?
madhivanan thanks, yes that correct, i would like 1 record from each of the tables where MemberID=89.There is a form on a webpage and the tables for the long form is seperated by required fields and nonRequired fields, questions used for 'heavy' statistical mining, and questions/answers used by another application, etc... So now i just need to bring ALL the data from the tables back into the form based on MemberID=XI'm a little lost by this method, can someone please break it bown for me so i can learn, thankserror: 'row_number' is not a recognized function name.Maybe this a sql2005 function, i'm on 2k, sry for not mentioning thatselect t.columns from(Select columns,row_number() over(partition by id order by id) as rownum from table) as twhere rownum=1 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|