Link to home
Start Free TrialLog in
Avatar of Mehram
MehramFlag for Pakistan

asked on

Conditionala where clause in sql query

SQL Server 2005

in sql query to construct a where clause for
if host_name() = 'MATEEN' then
    unit_code = '21'
else
   ignore unit code matching and show all records
end if


I tried
where  (  unit_code =  '21' and host_name() =  'MATEEN') or 1=1 )
but it is showing all records in all machines including MATEEN'S P.C.
ASKER CERTIFIED SOLUTION
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mehram

ASKER

SELECT * FROM ur_table
WHERE unit_code = (CASE WHEN host_NAME() = 'MATEEN' then '21' ELSE unit_code end)

cannot be else unit_code end , I want to show records of all units if user is not mateen.
Avatar of Mehram

ASKER

Oh no,

it is working, thanks
welcome, Mehram..