Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 634361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:17:14+00:00 2026-05-13T20:17:14+00:00

I have written following stored procedure. If I don’t use IF ELSE block Order

  • 0

I have written following stored procedure. If I don’t use IF ELSE block Order by clause works fine but I need to use IF ELSE to build my select statement. Order by Is not working with IF else block. Can you please tell me what am I doing wrong?
Thanks in Advance!

ALTER PROCEDURE [dbo].[GetLookupItem] 

-- #description Gets LookupItem 
-- #tables   Subscriber.Reference.LookupItem,Subscriber.Reference.LookupText,Subscriber.Reference.LookupType
-- #parameter:   @LookupTypeId       This parameter defines a type of lookup
-- #parameter:   @LookupTextAbbreviationId            This Variable decides to tie Lookup Text to Abbreviation or Full Text. Most of the cases Full text will be used.
-- #parameter:   @LanguageCode     To show the Lookup Text in this Language

 @LookupTypeId int,
 @LookupTextAbbreviationId int ,
 @LanguageCode varchar(5)


AS

BEGIN

 SET NOCOUNT ON;
 DECLARE @MonthType int
 SET @MonthType = (SELECT Id FROM Subscriber.Reference.LookupType WHERE Name = 'Months')

 IF  @LookupTextAbbreviationId IS NULL
  BEGIN
   SELECT RLI.Id,RLT.Value
   FROM Subscriber.Reference.LookupItem RLI
   INNER JOIN Subscriber.Reference.LookupText RLT
   ON RLI.LookupTextId = RLT.Id AND RLT.LanguageCode = @LanguageCode AND RLI.LookupTextAbbreviationId IS NULL   -- LookupTextAbbreviationId will be null for full text records in Subscriber.Reference.LookupItem
   WHERE RLI.LookupTypeId = @LookupTypeId 
  END
 ELSE

  SELECT RLI.Id,RLT.Value
  FROM Subscriber.Reference.LookupItem RLI
  INNER JOIN Subscriber.Reference.LookupText RLT
  ON RLI.LookupTextAbbreviationId = RLT.Id AND RLT.LanguageCode = @LanguageCode
  WHERE RLI.LookupTypeId = @LookupTypeId


        ORDER BY                -- Order by will change for only Months by ID
        CASE  @LookupTypeId 
   WHEN @MonthType THEN RLT.Id 
   END,
  CASE 
   WHEN @LookupTypeId <> @MonthType THEN RLT.Value 
  END

 IF (@@ERROR <> 0)
  RETURN 1
 ELSE
  RETURN 0  

END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:17:15+00:00Added an answer on May 13, 2026 at 8:17 pm

    Do you want to order by RLT.Id when @LookupTypeId equals @MonthType and by RLT.Value otherwise? Then the case should be like this:

    ORDER BY
    CASE @LookupTypeId 
       WHEN @MonthType THEN RLT.Id
       ELSE RLT.Value
    END
    

    However, if RLT.Id and RLT.Value are not both the same type you will get a conversion error. If RLT.Id is int and RLT.Value is varchar then you could change the case to:

    ORDER BY
    CASE @LookupTypeId 
       WHEN @MonthType THEN STR(RLT.Id)
       ELSE RLT.Value
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following query, but in the WHERE clause I have used
I have written the following procedure to create a series of 20 databases. But
I have written the following stored procedure: ALTER PROCEDURE [dbo].[sp_Accounts_ValidateLogin] @EmailAddress varchar(255), @Password varchar(20)
I have written the following piece of code ( sql clr stored procedure )
Good Morning, I've written the following stored procedure that searches for records that have
I have the following c# code to call stored procedure testproc , but when
I have written the following algorithm in order to evaluate a function in MatLab
I have following Entity Framework Logic, and I want to translate to Stored Procedure,
I am using oracle 11g and have written a stored procedure which stores values
Someone has written the following stored proc query (I have removed all the irrelevant

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.