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 |
|
ann
Posting Yak Master
220 Posts |
Posted - 2008-09-18 : 15:09:16
|
| I have been banging my head against the wall to figure this out - hopefully someone here can help me:Requirement: Get branchManager UserName based on @UserName @UserName = 'John Doe'tableUsersUserName BranchNumber EmpNoJohn Doe 85 11Jane Smith 85 12tableBranchesbranchNumber BranchManagerID 85 Jane SmithResults Wanted: Jane SmithThis is what I came up, but it always returns nothing (no errors, just no records)Select Manager = C.userNameFrom users AInner Join Branches BOn A.BranchNumber = B.BranchNumberInner Join users COn B.BranchManagerID = C.userNumberWhere a.userName = @AssignedToIf anyone can help, I'd really appreciate it.Thanks - hope I explained the situation clearly |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-09-18 : 15:44:44
|
| the variable at the top is @userName but in your WHERE clause it is @AssignedTo Your tableUsers has a column called EmpNo butin your JOIN criteria you join to [userNumber]Post the actual, scripted out table structuresand post your actual, complete code.Be One with the OptimizerTG |
 |
|
|
ann
Posting Yak Master
220 Posts |
Posted - 2008-09-18 : 15:56:52
|
| Sorry, that was messy of me ....I was just trying to make my columns easier to read for this purpose, the sp is:Select Manager = C.userNameFrom users AInner Join Branches BOn A.BranchNumber = B.BranchNumberInner Join users COn B.BranchManagerID = C.userNumberWhere a.userName = 'John Doe'The tables should be:UsersUserName BranchNumber UserNoJohn Doe 85 11Jane Smith 85 12BranchesbranchNumber BranchManagerID 85 Jane SmithSo here's the odd part, this works fine in sql2000, but it returns nothing in 2005? And yes, I've verified the records exist in both db's |
 |
|
|
ann
Posting Yak Master
220 Posts |
Posted - 2008-09-18 : 16:04:58
|
| Here is the script for the tables:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[branches]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[branches]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[users]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[users]GOCREATE TABLE [dbo].[branches] ( [branchNumber] [int] NOT NULL ,[branchManagerID] [int] NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[users] ( [userNumber] [int] NOT NULL , [userName] [char] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [branchNumber] [int] NULL ) ON [PRIMARY]GOSelect Manager = C.userNameFrom users AInner Join Branches BOn A.BranchNumber = B.BranchNumberInner Join users COn B.BranchManagerID = C.userNumberWhere a.userName = 'John Doe'Again - this works in 2000 but not 2005? |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-09-18 : 16:25:11
|
This ran ok for me on a 2005 box:although I had to change what you had typed here:BranchesbranchNumber BranchManagerID 85 Jane Smith 12insert Users(UserName, BranchNumber, UserNumber)select 'John Doe', 85, 11 union allselect 'Jane Smith', 85, 12insert Branches (branchNumber, BranchManagerID )select 85, 12Select Manager = C.userNameFrom users AInner Join Branches BOn A.BranchNumber = B.BranchNumberInner Join users COn B.BranchManagerID = C.userNumberWhere a.userName = 'John Doe'results:Manager----------------------------------------Jane Smith Be One with the OptimizerTG |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-09-18 : 16:32:18
|
Take your time.... Your query information is still all over the place..quote: The tables should be:UsersUserName BranchNumber UserNoJohn Doe 85 11Jane Smith 85 12BranchesbranchNumber BranchManagerID 85 Jane Smith
Are you sure the "BranchMangerID" is supposed to be Jane Smith and not 12?drop table #Branchesdrop table #UsersCREATE TABLE #branches ([branchNumber] [int] NOT NULL ,[branchManagerID] [int] NULL ) CREATE TABLE #users ([userNumber] [int] NOT NULL ,[userName] [char] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[branchNumber] [int] NULL )Insert Into #Branches(BranchNumber,BranchManagerID)select 85,12 Union allselect 86,14 Insert into #Users(UserNumber,UserName,BranchNumber)Select 11,'Jane Doe',85 Union AllSelect 12,'Jane Smith',85 Union AllSelect 13,'Jane test',86 Union AllSelect 14,'Jane Another',86Select a.UserName,a.UserNumber,a.BranchNumber,c.UserName as BranchManagerfrom #users ainner Join #Branches bon a.BranchNumber = b.BranchNumberInner Join #Users con b.BranchManagerID = c.UserNumberwhere a.UserName = 'Jane Doe' Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-09-18 : 16:33:35
|
Didn't have my screen refreshed, TG already posted the solution.. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
|
|
|
|
|