Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 16th, 2006, 02:48 PM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default stored procedure

Im trying to create a stored procedure that joind 2 tables; Organizations and Users. Firstly it needs to return a resultset that list all matching Organizations and the Users that belong to each Organization. (see below)

SELECT
    O.HiddenOrgID, O.OrganizationName, O.OrgAddressNumber, O.OrgAddressDirection, O.OrgAddressStreet, O.OrgAddress2,
    O.OrgCity, O.OrgState, O.OrgZip, O.OrgZip6, O.BrokerID,
    U.HiddenUsrID, U.UserLastName, U.UserFirstName, U.UserMI
FROM dbo.Organizations O
    INNER JOIN dbo.Users U ON
        O.HiddenOrgID = U.HiddenOrgID


WHERE U.user_is_active = 1 AND (O.OrganizationName IS NOT NULL) AND (U.UserLastName IS NOT NULL)

Problem is, I also need to return with the same resultset the Broker Information which is also a join between the same 2 tables. Only not all Organization records will have a BrokerID populated; some will be nulls.

In other words I need to return the following;

1. All users attched to the Org.
2. Broker Info attached to Org if exist.

Any suggestions will be appreciated.



Reply With Quote
  #2 (permalink)  
Old June 19th, 2006, 03:37 PM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT O.HiddenOrgID,
         O.OrganizationName,
         O.OrgAddressNumber,
         O.OrgAddressDirection,
         O.OrgAddressStreet,
         O.OrgAddress2,
         O.OrgCity,
         O.OrgState,
         O.OrgZip,
         O.OrgZip6,
         O.BrokerID,
         U.HiddenUsrID,
         U.UserLastName,
         U.UserFirstName,
         U.UserMI,
           B.BrokerName,
           B.BrokerWhatEver
FROM dbo.Organizations O
INNER JOIN dbo.Users U ON O.HiddenOrgID = U.HiddenOrgID
INNER JOIN dbo.Brokers B ON B.HiddenBrokerID = O.HiddenBrokerID
WHERE U.user_is_active = 1
         AND O.OrganizationName IS NOT NULL
         AND U.UserLastName IS NOT NULL
Reply With Quote
  #3 (permalink)  
Old June 19th, 2006, 04:45 PM
Friend of Wrox
Points: 2,101, Level: 18
Points: 2,101, Level: 18 Points: 2,101, Level: 18 Points: 2,101, Level: 18
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2003
Location: , , .
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

I don't understand how you could have a HiddenOrgID in the Users table for an organization that doesn't have a name. I could see how you could have a User who was not yet assigned to an organization and there would be a NULL value in the Users table for that User.

If you had a NULL value in the Users table for HiddenOrgID you don't want to pull that record anyway. That would simplify things. Then you could do an OUTER JOIN when joining the Brokers table to the Org table. That way you'd get all of the records in the Users table that have been assigned to an Org with a Name even if they had not been assigned to a broker.

Something like:

SELECT *
FROM Users INNER JOIN
                      Orgs ON Users.OrgID = Orgs.OrgID LEFT OUTER JOIN
                      Brokers ON Orgs.BrokerID = Brokers.BrokerID

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stored procedure prashant_telkar SQL Server 2000 1 July 9th, 2007 07:57 AM
Stored Procedure jezywrap SQL Server ASP 1 January 3rd, 2007 12:29 AM
Stored Procedure rajanikrishna SQL Server 2000 0 July 18th, 2005 05:01 AM
Help About Stored Procedure zhuge6 BOOK: ASP.NET Website Programming Problem-Design-Solution 3 May 20th, 2005 09:27 AM
Stored Procedure help flyin SQL Server 2000 4 August 3rd, 2004 07:37 AM



All times are GMT -4. The time now is 08:18 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.