• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

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.
0
Mehram
Asked:
Mehram
  • 2
  • 2
1 Solution
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
Try this..

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

Replace ur_table with your table name and run the above query..
0
 
MehramAuthor Commented:
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.
0
 
MehramAuthor Commented:
Oh no,

it is working, thanks
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
welcome, Mehram..
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now