Solved

Conditionala where clause in sql query

Posted on 2013-01-28
4
299 Views
Last Modified: 2013-01-28
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
Comment
Question by:Mehram
  • 2
  • 2
4 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 38826339
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
 

Author Comment

by:Mehram
ID: 38826344
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
 

Author Comment

by:Mehram
ID: 38826349
Oh no,

it is working, thanks
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 38826429
welcome, Mehram..
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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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