SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with complex Stored Procedure


Help with complex Stored Procedure

Author
Message
mhighfi
mhighfi
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 11
I am having trouble getting all the data, exactly as needed in 1 stored procedure. I'll try sum this up as best as I can.

I have 4 tables
tblRecords (has SiteID)
tblSites
tblRegions (has SiteID)
lkup_Record_Regions (RecordID, RegionID here) <- this table represents regions that the record could be located in
lkup_Record_Regions_Serv (RecordID, RegionID here) <- this table represents regions that the record can serve

What I need is this. Simple data from Record table, given RecordID as SP Parameter. However, here's the part I'm stuck on.

I need all regions for the site of a record, as well as the following. If tblRecords.field3 = 0, I need to know all the regions for the record's site that exist in either lkup table. If tblRecord.field3 = 1, same data but only from lkup_Record_Regions. I really hope this makes sense, I find it complicating to even explain, lol.

End result is this is what I need. http://tinypic.com/view.php?pic=faty5c&s=5

Where Field 6 is the list of regions, and a checkmark image displays under each region if the record is displayed in that region. (tblRecords.field3 value determines whether or not the lkup_Record_Regions_Serv table is factored into where the record is displayed).

If this doesn't make sense I apologize, please let me know and I will try clear up anything
LutzM
LutzM
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10687 Visits: 13559
I'm leaving the design discussion out for a moment...

You might want to use a LEFT OUTER JOIN together with a CASE statement. (column based result set, meaning results for lkup_Record_Regions_Serv in one column and lkup_Record_Regions in another one) or
use a UNION query (or a cte) to get the results "row based".

Please note that I didn't take the time to link to a picture of your expected result...
If you need a more detailed answer, please provide a moer detailed question.
Have a look at the first article referenced in my signature for a good example how to do it ;-)



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search