Question

I have a piece of cursor code which I am trying to rewrite to a regular SQL code but I am getting an error. Here's the Cursor code:

FROM            sr_cursor
INTO            @partyID, @serviceRequestID WHILE @@FETCH_STATUS = 0
BEGIN
            SELECT        @customerName = c.TradeName, @legalName = c.LegalName
            FROM            v_RptCompany_v001 c 
            INNER JOIN
                          v_RptAppCompany_v001 ac ON ac.PartyID = c.PartyID AND ac.BeginDate = c.BeginDate
            WHERE        ac.AppID = @gappID AND ac.PartyID = @partyID 
            IF          (@customerName IS NULL OR DATALENGTH(@customerName) = 0) 
BEGIN
SET             @customerName = @legalName 
END

My Code:

SELECT          C.TradeName as CustomerName, 
            C.LegalName as LegalName, (CASE WHEN CustomerName IS NULL OR DATALENGTH(CustomerName) = 0 THEN CustomerName = LegalName ELSE CustomerName END) AS CustName 
FROM            v_RptCompany_v001 c 
INNER JOIN      v_RptAppCompany_v001 ac 
ON              ac.PartyID = c.PartyID AND ac.BeginDate = c.BeginDate

I suspect its the case statement because of which I am getting an error. Please suggest the possible answer. Appreciate your help. Thank you in advance.

Pls note: The AppID is the parameter but I don't want to have any parameter

.

Was it helpful?

Solution

Try this:

SELECT          
   C.TradeName as CustomerName, 
   C.LegalName as LegalName, 
   (CASE WHEN CustomerName IS NULL OR DATALENGTH(CustomerName) = 0 
           THEN LegalName 
           ELSE CustomerName END) AS CustName 
FROM            
   v_RptCompany_v001 c 
INNER JOIN      
   v_RptAppCompany_v001 ac ON ac.PartyID = c.PartyID AND ac.BeginDate = c.BeginDate
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top