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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 if then within a view

Author  Topic 

homeguard
Starting Member

32 Posts

Posted - 2008-05-05 : 16:24:06
I want to create a view that has an if then statement within it, how is this done?

Van
Constraint Violating Yak Guru

462 Posts

Posted - 2008-05-05 : 16:35:19
Use CASE.
Go to Top of Page

homeguard
Starting Member

32 Posts

Posted - 2008-05-06 : 14:46:43
i think i need some help:
case

When (SELECT count(userid) FROM dbo.EmployeeInfo where supervisorname = REPLACE(SUSER_SNAME(), 'rxsol\', '')) > 0 then select * from dbo.employeeinfo where supervisorname = REPLACE(SUSER_SNAME(), 'rxsol\', '')



When (SELECT count(userid) FROM dbo.EmployeeInfo where managername = REPLACE(SUSER_SNAME(), 'rxsol\', '')) > 0 then select * from dbo.employeeinfo where managername = REPLACE(SUSER_SNAME(), 'rxsol\', '')



When (SELECT count(userid) FROM dbo.EmployeeInfo where directorname = REPLACE(SUSER_SNAME(), 'rxsol\', '')) > 0 then select * from dbo.employeeinfo where directorname = REPLACE(SUSER_SNAME(), 'rxsol\', '')

end


the when statement needs to change per case, does that make sense?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-06 : 15:22:04
[code]SELECT *
FROM dbo.EmployeeInfo
WHERE REPLACE(SUSER_SNAME(), 'rxsol\', '') IN (SupervisorName, ManagerName, DirectorName)[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

homeguard
Starting Member

32 Posts

Posted - 2008-05-06 : 16:08:48
that doesnt give the the different select statements i was looking for
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-07 : 02:38:57
[code]SELECT TOP 1 WITH TIES
*
FROM dbo.EmployeeInfo
WHERE REPLACE(SUSER_SNAME(), 'rxsol\', '') IN (SupervisorName, ManagerName, DirectorName)
ORDER BY CASE REPLACE(SUSER_SNAME(), 'rxsol\', '')
WHEN SupervisorName THEN 1
WHEN ManagerName THEN 2
WHEN DirectorName THEN 3
END[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -