Solved

sql server 2008 query

Posted on 2014-12-03
1
245 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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