1

I need to execute the query according to the Businessunit values which is to be passed in runtime. If the businessunit is null the AND condition should not get executed and if it is not null i have to take the businessunit which is passed.

// SQL Query

SELECT nvrEmpCode, nvrEmpName 
FROM tblHRIMS_EmployeeDetail
WHERE (intCategoryID NOT IN (0, 1)) AND (nvrResigned = 'No') 
AND nvrBusinessunit=

All these must be done in a SQLDatasource and not as a StoredProcedure. How to achieve this?

1
  • Do you know you can also cast vote? (Not saying to cast for me. I am saying because you have 0 vote cast in your profile) Commented Sep 18, 2012 at 7:40

3 Answers 3

1

Your WHERE condition should be like this:

WHERE (intCategoryID NOT IN (0, 1)) AND (nvrResigned = 'No') 
AND (@businessunit IS NULL OR nvrBusinessunit=@businessunit)
Sign up to request clarification or add additional context in comments.

3 Comments

i thin your and 'Y. Ecarri' answers should be merge to get the complete answer
@Talha Not required as OP should know how to add parameters (As he/she has not asked it!)
ok, thanks both of you. but as approch wise, hims answer should be accepted.
1

Assuming that you are using SQL Client:

SELECT nvrEmpCode, nvrEmpName 
FROM tblHRIMS_EmployeeDetail
WHERE (intCategoryID NOT IN (0, 1)) AND (nvrResigned = 'No') 
AND (nvrBusinessunit=@BU OR @BU is null)

Then you have to pass a value for the parameter:

command.Parameters.AddWithValue(@BU, yourValue)

Comments

0
SELECT        nvrEmpCode, nvrEmpName, @Businessunit AS varchar

FROM            tblHRIMS_EmployeeDetail
WHERE        (intCategoryID NOT IN (0, 1)) AND (nvrResigned = 'No') 
AND (nvrBusinessUnit = (CASE WHEN @Businessunit = '' THEN nvrBusinessunit ELSE @Businessunit END))

1 Comment

But this query helps in DataSet while passing NULL. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.