Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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.



 
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
 
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





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





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