DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Alternative to recursive queries  (Read 7,011 times)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Alternative to recursive queries
« on: May 28, 2011, 08:32 PM »
I've been evaluating/using VistaDB on a project I'm writing, and ran into a problem, as I detailed in the ongoing review.

I wanted to use a function to return the result of a query on a pivot table as a comma delimited list.

Code: (sql)
DECLARE @r VARCHAR(200) 
SELECT @r = ISNULL(@r+',', '') 
        + gt.description
FROM GameTags gt JOIN Game2Tag g2g ON 
	gt.keygametag = g2g.keygametag   
WHERE g2g.keygame = 1 

SELECT @r

But it only returned the last row. 

The (relevant) structure of the tables is as follows:

Games
KeyGame int

GameTags
KeyGameTag int
Description varchar(50)

Game2Tags
KeyGame2Tag int
KeyGame int
KeyGameTag int

So, I wanted to return the a comma-delimited string of the Description fields for all of the matching tags, but it returned a string with only the description of the final record.  First, does anyone see anything wrong with the SQL?  I'm pretty sure it's right, but don't have any quick way to test it currently against any other dbs.  And second, does anyone know of another way to do the same thing without cursors?

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Alternative to recursive queries
« Reply #1 on: May 29, 2011, 09:26 AM »
I'm a bit worried about this part: @r = ISNULL(@r+',', '') + gt.description

Doing a lookup of the syntax leads me to this.
I'd be thinking more in the line of
Code: (sql)
    DECLARE @r VARCHAR(200)
    SELECT @r = CASE WHEN @r is NULL THEN ''
           ELSE  @r + ','
           END,
         @r = @r + gt.description
    FROM GameTags gt JOIN Game2Tag g2g ON
    gt.keygametag = g2g.keygametag  
    WHERE g2g.keygame = 1
     
    SELECT @r 
Tested with 3 records gave: Game 1,Game 2,Third game
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Alternative to recursive queries
« Reply #2 on: May 29, 2011, 09:48 AM »
Hm, did some more testing (using SQL 2008 :-[) and indeed your original query works just OK.
So it's probably an anomaly of VistaDB that it won't work there. Maybe my alternative syntax does?
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Alternative to recursive queries
« Reply #3 on: May 29, 2011, 03:59 PM »
No... VistaDB doesn't allow the use of recursive queries, so yours wouldn't work either.  The IsNull just packages up your case statement, but works fine.  The reason for that is that anything + null = null, so the first time through, it just adds an empty string to the query.

But thanks for verifying that for me :)

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Alternative to recursive queries
« Reply #4 on: May 29, 2011, 05:11 PM »
VistaDB doesn't allow the use of recursive queries
Oh, well, not so great Transact-SQL compatibility then :(
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
Re: Alternative to recursive queries
« Reply #5 on: May 29, 2011, 05:32 PM »
VistaDB doesn't allow the use of recursive queries
Oh, well, not so great Transact-SQL compatibility then :(

Oh, yes... It actually *does* have great T-SQL compatibility... read the review for more on that.  It just has a few incompatibilities, and they're straightforward* with what isn't compatible, and why.  Sometimes, things just haven't been tested, but so far, a quick search on their forums or a post, and they've responded.  

*Ref:
1. Unsupported SQL Server Features and Syntax
2. Differences from SQL Server
3. Unsupported syntax
4. SQL Server Foreign Key Differences

Original address: https://www.donationcoder.com/forum/index.php?topic=26905.0