Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

Sql Server function with cursor

Hi ,
   Can you please provide me with the equivalent Sql Server function  for  the Oracle function given below  .
     (  Note : Thru the answer to another question  in the link , I have an idea of how to write function .   But All I am really looking for is how to use cursor
                   http://www.experts-exchange.com/Database/MS-SQL-Server/Q_28486708.html
    )

   Please see the oracle query below

create or replace 
Function GetMultiObjs
(
  pParentID       IN  VARCHAR2,
  pID          IN  VARCHAR2,
  pDir       IN  VARCHAR2 DEFAULT '12'
)
  RETURN VARCHAR2 
IS 
  mRetVal    VARCHAR2(2000);

  CURSOR c1 IS
    SELECT  obj2.ObjName
    FROM    datarel rel, dataobj obj2
    WHERE Rel.uid1 = pParentID
            AND rel.uid2 = obj2.objuid
            AND rel.defuid = pID
          
  CURSOR c2 IS
    SELECT  obj2.ObjName
    FROM    datarel rel, dataobj obj2
    WHERE Rel.uid2 = pParentID
            AND rel.uid1 = obj2.objuid
            AND rel.defuid = pID
          
BEGIN
  mRetVal := '';
  BEGIN
    IF pDir = '12' THEN
      FOR rec IN c1 LOOP
          If mRetVal IS NULL Then
            mRetVal := rec.ObjName;
          ELSE
            mRetVal := mRetVal || '|' || rec.ObjName;
          END IF;
      END LOOP;
    ELSE
      FOR rec IN c2 LOOP
          If mRetVal IS NULL Then
            mRetVal := rec.ObjName;
          ELSE
            mRetVal := mRetVal || '|' || rec.ObjName;
          END IF;
      END LOOP;
    END IF;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      mRetVal := '';
    WHEN OTHERS THEN
      mRetVal := '!!!ERR!!!';
  END;

  RETURN mRetVal;
END GetMultiObjs;

Open in new window

0
Sam OZ
Asked:
Sam OZ
  • 3
  • 3
  • 2
  • +2
4 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
> But All I am really looking for is how to use cursor
why would you use a cursor if that is the least efficient solution?
especially as here, it's to return a single value, which should be done with a simple select ...
0
 
Sam OZAuthor Commented:
Hi Thanks for the reply . can you please guide me with how to  do it with cursor ? ( I don't need the whole Function - Just the  Declaration and looping thru cursor

 If you think , it can be done by a simple Query, Can you please  provide that query ?

   thanks

   Sam
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please see this article which shows the difference between cursor processing (and the general non-cursor replacement):
http://www.experts-exchange.com/Database/MS-SQL-Server/A_13640-processing-cursor-vs-temp-table-syntax.html
note: row by row processing is only needed for things where really, one row at a time needs to be "processed", and not the whole set

in your case, you don't have multiple rows to be processed, just 1 single values to be returned, hence cursor is not to be used. even in Oracle you don't need cursors for this kind of functions, just a big waste of resources.

in regards to the "simple query", I see that in the linked question you got already the answer there
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
ste5anSenior DeveloperCommented:
Afaik, even in PL/SQL you can write that in a single, recursive statement (CTE).
0
 
Sam OZAuthor Commented:
Hi ,
   Can you please provide me the query  ? It is different from  the previous one in just one aspect
     ( As I told before , I need only the cursor part of the query - If  simple PL/SQL  can do it , Please provide me that )

       The core logic in this is

              Loop All Rows
                   If mRetVal IS NULL Then                               --- First Row
                        mRetVal := rec.ObjName;    
                   ELSE    ---  Keep appending to RetVal with a Delim + ObjName
                       mRetVal := mRetVal || '|' || rec.ObjName;
                  END IF;
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
in oracle, you use listagg function:
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions089.htm

in sql server, you can do this using the FOR XML subquery, see here for example
http://www.experts-exchange.com/Database/MS-SQL-Server/SQL-Server-2005/Q_28272543.html
0
 
Sam OZAuthor Commented:
Hi Thanks for all valuable comments .  Can someone help me with the For XML query ( I am on a tight timeline and really don't have time for trial and error)  

 Can someone please give me  the For XML equivalent of just one loop

    FOR rec IN c1 LOOP
          If mRetVal IS NULL Then
            mRetVal := rec.ObjName;
          ELSE
            mRetVal := mRetVal || '|' || rec.ObjName;
          END IF;
      END LOOP;
0
 
ste5anSenior DeveloperCommented:
E.g.
DECLARE @ID INT;
DECLARE @ParentID INT;

SELECT  STUFF(( SELECT  '\' + obj2.ObjName
                FROM    datarel rel
                        INNER JOIN dataobj obj2 ON rel.uid1 = obj2.objuid
                WHERE   Rel.uid2 = @ParentID
                        AND rel.defuid = @ID
              FOR
                XML PATH('')
              ), 1, 1, '');

Open in new window


Caveat: Your Oracle solution and the above T-SQL solution guarentee no order!
0
 
PortletPaulCommented:
LISTAGG()

is the "equivalent"

of "FOR XML PATH" in sql server

Plus: you don't need it "for just one loop" because LISTAGG() will handle ALL the loops*

*actually it won't be loops that you use, just a simple query

    CREATE TABLE DATAOBJ
    	("OBJNAME" varchar2(8))
    ;
    
    INSERT ALL 
    	INTO DATAOBJ ("OBJNAME")
    		 VALUES ('askjakja')
    	INTO DATAOBJ ("OBJNAME")
    		 VALUES ('aasdasd')
    	INTO DATAOBJ ("OBJNAME")
    		 VALUES ('asdfsfds')
    	INTO DATAOBJ ("OBJNAME")
    		 VALUES ('dfdfgdf')
    	INTO DATAOBJ ("OBJNAME")
    		 VALUES ('dfgdfg')
    	INTO DATAOBJ ("OBJNAME")
    		 VALUES ('dfgdfg')
    	INTO DATAOBJ ("OBJNAME")
    		 VALUES ('dfgdfg')
    	INTO DATAOBJ ("OBJNAME")
    		 VALUES ('rehtrt')
    	INTO DATAOBJ ("OBJNAME")
    		 VALUES ('dfdthrr')
    SELECT * FROM dual
    ;

Open in new window

**Query 1**:

    SELECT
           LISTAGG(OBJNAME, ' | ')
             WITHIN GROUP (ORDER BY OBJNAME) AS "Objname_list"
    FROM DATAOBJ
    

**[Results][2]**:
    
    |                                                                          OBJNAME_LIST |
    |---------------------------------------------------------------------------------------|
    | aasdasd | asdfsfds | askjakja | dfdfgdf | dfdthrr | dfgdfg | dfgdfg | dfgdfg | rehtrt |



  [1]: http://sqlfiddle.com/#!4/dd1e9/1

Open in new window

0
 
PortletPaulCommented:
oops I got this wrong way around, oh well. Sorry.

But: you do not need a cursor in Oracle, and you don't need one in SQL Server either.

i.e. You can replace that Oracle cursor code with a query using listagg()
0
 
Scott PletcherSenior DBACommented:
CREATE FUNCTION GetMultiObjs
--ALTER FUNCTION GetMultiObjs
(
  @pParentID  nvarchar(50),
  @pID  nvarchar(50),
  @pDir  nvarchar(50) = '12'
)
RETURNS nvarchar(max)
AS
BEGIN
SET NOCOUNT ON
DECLARE @return_value nvarchar(max)
SET @return_value = ''

SELECT @return_value = STUFF((
    SELECT '|' + obj2.ObjName
    FROM datarel rel
    INNER JOIN dataobj obj2 ON
        Rel.uid1 = @pParentID AND
        rel.uid2 = obj2.objuid AND
        rel.defuid = @pID
    ORDER BY obj2.ObjName
    FOR XML PATH('')
    ), 1, 1, '')

SELECT @return_value = @return_value + STUFF((
    SELECT '|' + obj2.ObjName
    FROM datarel rel
    INNER JOIN dataobj obj2 ON
        Rel.uid2 = @pParentID AND
        rel.uid1 = obj2.objuid AND
        rel.defuid = @pID
    ORDER BY obj2.ObjName
    FOR XML PATH('')
    ), 1, 1, '')
   
RETURN @return_value
END --FUNCTION
GO
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now