Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQLServer
declare @vc_supplier_name varchar(200) =''
declare @in_supplier_type_id int =2
declare @bt_active bit =1
 
SELECT a.in_supplier_id, dbo.format_fnc_bit(1, a.bt_active) AS vc_active, b.vc_supplier_type_name, a.in_supplier_type_id, a.vc_supplier_name,
 
    dbo.admin_user_fnc_get_name(1, a.in_updatedby_user_id) AS vc_updated_by, a.dt_updated
 
    FROM dbo.supplier a
 
    INNER JOIN dbo.common_data_vw_supplier_type b ON a.in_supplier_type_id = b.in_supplier_type_id
 
    WHERE a.in_supplier_type_id = @in_supplier_type_id AND a.vc_supplier_name LIKE '%' + @vc_supplier_name + '%'
    AND  a.bt_active = case @in_supplier_type_id when 2 THEN @bt_active
 
    ORDER BY a.vc_supplier_name
 
Error comes :Incorrect syntax near the keyword 'ORDER'.
 
here i want to use bt_active only if @in_supplier_type_id=2
Posted 14-Feb-13 8:56am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Quote:
case @in_supplier_type_id when 2 THEN @bt_active

You missed the ending END ( Smile | :) ) here.
  Permalink  
v2
Comments
mrinal_kumar_jha at 14-Feb-13 15:13pm
   
Thanks CPallini It worked for @in_supplier_type_id =2 This query is not executing in case if @in_supplier_type_id is not 2.. What could be reason
Marcus Kramer at 14-Feb-13 15:47pm
   
+5.
Espen Harlinn at 15-Feb-13 9:04am
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You need to add an "End" to the end of your case statement.
 
This:
AND  a.bt_active = case @in_supplier_type_id when 2 THEN @bt_active
 
should be this:
 
AND  a.bt_active = case @in_supplier_type_id when 2 THEN @bt_active End
 
You can also use an Else if need be.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 472
1 sanket saxena 355
2 CPallini 205
3 thatraja 200
4 OriginalGriff 195
0 Sergey Alexandrovich Kryukov 8,784
1 OriginalGriff 5,025
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid