Query problem merge 2 query into one.

Posted by Rajesh_Kumar under Sql Server on 7/1/2015 | Points: 10 | Views : 1300 | Status : [Member] [MVP] | Replies : 1
Hi All,
I have below queries like:-

select * from notification_email where is_send_email_flag = 'N';
select * from notification_email;


first query returns data which have is_send_email_flag = 'N'
and second query returns all records irrespective of is_send_email_flag in('N','Y')
So,for that i have created two stored procedures.
But i want to merge these queries into one and pass @is_send_email_flag variable to get all records or only is_send_email_flag = 'N'.
So how do i achieve that one with one query.I have written somewhere with Coalesce function which returns all records if no value is passed in a query.
But i forgot. Can anyone help me to solve my problem.Because unnecessary i am writing 2 queries.

Rejesh Kumar


Responses

Posted by: Bandi on: 7/1/2015 [Member] [MVP] Platinum | Points: 25

Up
0
Down
DECLARE  @is_send_email_flag CHAR(1) = 'N' -- Pass N or NULL

select * from notification_email
where (is_send_email_flag = @is_send_email_flag or
@is_send_email_flag IS NULL
)



It returns all records if you pass NULL for @is_send_email_flag variable; if you pass 'N' for @is_send_email_flag variable, it returns the records which have is_send_email_flag = 'N' in the table

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Rajesh_Kumar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response