Solved

sql server 2008 query

Posted on 2014-12-03
1
247 Views
Last Modified: 2014-12-03
I'm using SQL Server 2008 R2.

I have a table that looks like this:

table
Here is my fiddle for it: http://sqlfiddle.com/#!3/9ad11/2

If you look at the table. The value in the BossID column contains the EmployeeID of that employee's boss.
So if you look at my table Homer, Michael, Taylor and Erin's BossID is 2451 which pertains to the EmployeeID of value 2451 which is Montgomery Burns.  
So Montgomery Burns is Homer, Michael, Taylor and Erin's Boss.
Now if you look at the BossID value of 2356 for Montgomery Burns that pertains to EmployeeID of value 2356. So Donal Trump is Montgomery Burn's boss.

What I want is to write a query that takes in one parameter called @username.
So when I feed it the username of an employee the result set is all of the people that are his employees.
So for example if I set the parameter like this SET @UserName = 'mburns' then the result set should show all the employees which work for montgomery burns. Which in the case of my table would look like this:

result set when parameter is set to 'mburns'
if I set the parameter like this SET @UserName = 'dtrump' then the result set should show all the employees which work for donald trump. Which in the case of my table would look like this:

result set when parameter is set to dtrump

Right now I have the following query:

DECLARE @UserName varchar(50)
SET @UserName = 'mburns'

SELECT [Rid]
      ,[Lname]
      ,[Fname]
      ,[UserName]
      ,[Region]
      ,[OrderYear]
      ,[BossID]
      ,[EmployeeID]
FROM [TestR1]
WHERE [UserName] = @UserName

But that gives me this:

Here is the fiddle for my query:  http://sqlfiddle.com/#!3//3

result set of my query

Any idea of how I need to revise my query?
0
Comment
Question by:maqskywalker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
Richard Obenchain earned 500 total points
ID: 40478789
DECLARE @UserName varchar(50)
 SET @UserName = 'mburns'

SELECT t1.[Rid]
       ,t1.[Lname]
       ,t1.[Fname]
       ,t1.[UserName]
       ,t1.[Region]
       ,t1.[OrderYear]
       ,t1.[BossID]
       ,t1.[EmployeeID]
 FROM [TestR1] t1 join TestR1 t2 on t1.bossid = t2.employeeid
 WHERE t2.[UserName] = @UserName
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question