table:employee
EID ENAME MID(manager ids)
101 rama null
102 sita 101
103 siva 101
104 ganesh 103
. . .
. . .
for 103 ID the manager ID is 101(RAMA) and for 104 manager
is SIVA
if i give employee id (EID) you have to tell the manager for
that EID write query?
eample:if i give 102 .The query output should be manager for
102 ID that it should print RAMA as output
Answer Posted / mohana krishna
create table #emp (eid int, ename varchar(50),mid int)
insert into #emp values(1,'Ganesh',null)
insert into #emp values(2,'Ramesh',1)
insert into #emp values(3,'Suresh',1)
insert into #emp values(4,'Selvam',2)
insert into #emp values(5,'Vignesh',3)
declare @aid int
set @aid =1
select ename from #emp
where eid = (select case isnull(mid,0) when 0 then eid
else mid end mid
from #emp
where eid=@aid
)
select m.ename from #emp e
join #emp m on (m.eid=isnull(e.mid,e.eid))
where e.eid=1
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
Explain “row_number()” in sql server with an example?
Explain about service Broker functions?
How to define the name and server for a new dsn?
What are the dis_advantages of stored procedures, triggers, indexes?
How to check status of stored procedure in sql server?
What is difference between materialized view and view?
what is database replicaion? What are the different types of replication you can set up in sql server? : Sql server database administration
What is user-defined functions? What are the types of user-defined functions that can be created?
What is subreport?
What is a bit datatype?
How to test subquery results with the exists operator?
what are the Prerequisites for Replication?
What are the differences between DDL, DML and DCL in SQL?
Explain what is it unwise to create wide clustered index keys?
What is repeatable read?