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 6914341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:20:07+00:00 2026-05-27T09:20:07+00:00

I’m having a little problem getting SQL Server returning the same results as Oracle

  • 0

I’m having a little problem getting SQL Server returning the same results as Oracle for our SQL tree query.

We have a link table with attributes

CHILD_ID, LINK_ID, PARENT_ID

and a element table with ELEMENT_ID

In Oracle we use this

SELECT * FROM LINKS
JOIN ELEMENTS ON ELEMENT_ID = CHILD_ID
WHERE LINK_ID IN 
(SELECT LINK_ID FROM LINKS CONNECT BY PRIOR CHILD_ID = PARENT_ID START WITH PARENT_ID = 'startid')

In SQL Server we use this

WITH TREE_LINKS AS
(SELECT CHILD_ID, LINK_ID FROM LINKS WHERE PARENT_ID = 'startid'
UNION ALL
SELECT CURRENT_LINKS.CHILD_ID, CURRENT_LINKS.LINK_ID
FROM LINKS CURRENT_LINKS
INNER JOIN TREE_LINKS t1 ON CURRENT_LINKS.PARENT_ID = t1.CHILD_ID)

SELECT * FROM TREE_LINKS
INNER JOIN LINKS ON TREE_LINKS.LINK_ID = LINKS.LINK_ID 
INNER JOIN ELEMENTS ON ELEMENTS.ELEMENT_ID = TREE_LINKS.CHILD_ID

This works perfectly fine apart from 1 issue.

In Oracle we only get each unique link, based on LINK_ID. In SQL Server we get all the links describing the full tree which can include duplicates when 1 element exists below multiple other elements in different branches of the structure.

This means we get a large amounts of duplicate rows from SQL Server. I tested adding

SELECT DISTINCT TREE_LINKS.LINK_ID AS TREE_LINK_ID, * from TREE_LINKS 

in the last select statement but it just takes as long as the server has more work to remove the duplicates if found in different branches.

In 1 test case at the moment we have Oracle returning 20,000 rows and SQL Server returning 1.6 million rows. So far I have found no way to get SQL Server returning the same results as fast.

FYI : Adding DISTINCT in the recursion causes

DISTINCT operator is not allowed in the recursive part of a recursive
common table expression ‘TREE_LINKS’.

Edit: – An example

If we have links like this

PARENT_ID, LINK_ID, CHILD_ID
1          1        2
2          2        3 
3          3        4
1          4        3

There are 4 unique elements, but there are 6 elements in the complete tree. This is because there are 2 paths to element 3

  • 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-27T09:20:08+00:00Added an answer on May 27, 2026 at 9:20 am
    SELECT LINK_ID
      FROM LINKS
      START WITH PARENT_ID = 'startid'
      CONNECT BY PRIOR CHILD_ID = PARENT_ID
    

    should be equivalent to

    EDIT BELOW (AND in first edit): changed a PARENT_ID to LINK_ID in the second select

    WITH TREE_LINKS(CHILD_ID, LINK_ID) AS 
       (SELECT CHILD_ID, LINK_ID
        FROM LINKS
        WHERE PARENT_ID = 'startid'
            UNION ALL
        SELECT NLINKS.CHILD_ID, NLINKS.LINK_ID
        FROM LINKS as NLINKS, TREE_LINKS
        WHERE TREE_LINKS.CHILD_ID = NLINKS.PARENT_ID)
    
    SELECT LINK_ID FROM TREE_LINKS
    

    according to Simulation of CONNECT BY PRIOR of ORACLE in SQL SERVER

    The rest should be similar from what I can tell.

    EDIT: maybe something like:

    WITH TREE_LINKS(CHILD_ID, LINK_ID) AS 
       (SELECT CHILD_ID, LINK_ID
        FROM LINKS
        WHERE PARENT_ID = 'startid'
            UNION ALL
        SELECT NLINKS.CHILD_ID, NLINKS.LINK_ID
        FROM LINKS as NLINKS, TREE_LINKS
        WHERE TREE_LINKS.CHILD_ID = NLINKS.PARENT_ID)
    
    SELECT * FROM LINKS as TLINKS
    JOIN ELEMENTS ON ELEMENT_ID = TLINKS.CHILD_ID
    WHERE TLINKS.LINK_ID IN
    (SELECT TREE_LINKS.LINK_ID FROM TREE_LINKS)
    

    but I don’t have mssql or oracle to test it on currently

    EDIT: removed “AND TREE_LINKS.LINK_ID <> NLINKS.LINK_ID” from query as it was not needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.