Solved

Conditionala where clause in sql query

Posted on 2013-01-28
4
296 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
Comment Utility
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
Comment Utility
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
Comment Utility
Oh no,

it is working, thanks
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
welcome, Mehram..
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now