Faking Multidimensional Arrays in T-SQL made easy!

  • stax68 (11/4/2008)


    Jeff M:

    What isn't true? I am talking about the limitations on passing table variables, not string representations of arrays, so the link provided seems beside the point.

    Agreed... you can't pass tables. Just strings that can quickly be shredded into arrays. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • why not just do the following?

    DECLARE @AR TABLE

    (

    ROW int NULL,

    COL int NULL,

    VALUE varchar(2000) NULL

    )

    -- add some dummy data

    INSERT @AR (ROW, COL, VALUE) SELECT 5,7,'DATA IN ELEMENT 5,7'

    -- retrieve it

    SELECT VALUE FROM @AR WHERE ROW = 5 AND COL = 7

    it's much easier...

    seng

  • Jeff Moden (11/4/2008)


    No, I don't think the article was about 3D arrays... go back and read what I posted in my first post. ;).

    I read it the first time. You seemed to say (and therefore like it or not did say) it was about 3D arrays, but the author didn't go far enough in his explanation.

    But, with a little imagination, you can create a "3d" array... or at least a 2 dimensional representation of one.

    NS, Sherlock? [wink]

    Tim Wilkinson

    "If it doesn't work in practice, you're using the wrong theory"
    - Immanuel Kant

  • Keep it clean, slick. 😉 And, don't blame me if you don't understand what I'm saying. :w00t:

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • WoW!

    This seems to have brought out a ton of thinkers!

    The article is to provide a look back into history as to how arrays were first created - be they 2d or 3d or more.

    There are going to be some follow-on articles about how to use this in practical, everyday (for some of us) applications, and possibly even to go on into rocket science.

    A couple of comments, here might be appropriate though -

    1) You do NOT have to pass tables to procedures - especially if the data is already stored on a table on disk. You don't even have to pass pointers. Everything is already there.

    2) Captain Kirk once said, in his final battle with Khan - "He is evidencing classic two dimensional thinking" - we all evidence that quite often.

    3) Just because it is new and improved does not mean it is necessarily better.

    4) Show me how passing the word 'YES' (3 bytes) can be dome more efficiently using XML? Take it even further, lets pass a single bit - 0 or 1 to another process using XML and have it take up as few bytes.

    This is the concept in the real engineering world - not the concepts that are taken in the lofty world of education.

    Randal Burke

    I wish there was a "head scratchin' " emoticon here...

  • Jeff Moden (11/4/2008)


    Keep it clean, slick. 😉

    Oh, I do - mens sana in corpore sano old chap.

    And, don't blame me if you don't understand what I'm saying. :w00t:

    in that case may I congratulate you on your magnificent imperial robe!

    Tim Wilkinson

    "If it doesn't work in practice, you're using the wrong theory"
    - Immanuel Kant

  • This is great. Going back to using the concept of a stack and heap. This is way faster than using a cursor to pivot data. Now if you could just find a way to run SQL Server on a MOS 6502...

  • Joshua Perry (11/4/2008)


    This is great. Going back to using the concept of a stack and heap. This is way faster than using a cursor to pivot data. Now if you could just find a way to run SQL Server on a MOS 6502...

    Well,

    Since you asked... the MOS6502 WAS able to run a cross-assembled version of CP/M, from which DOS was built.

    Then Windows 1 was first brought out on an Apple //e (without using the Z80 card) - then came the Microsoft Z-80 card with Windows 3.0... and now we have advanced all the way up to having Windows Compact which runs on... OMG!!! those pesky little microprocessors with only a few megs of program space and then those wonderful little plug-in memory sticks that we call PDA!

    What goes around definitely comes back. 🙂

    Everything old is new again!

  • mmcclure (11/4/2008)


    Okay, corrected the code for SQL 2008 (at least this is what worked for me).

    /* create the basic table structures needed */

    CREATE TABLE [X_DIMENSION]

    ( [LOC] [int] NULL , [X_ELEMENT] [varchar] (2000) NULL )

    CREATE TABLE [Y_DIMENSION]

    ( [LOC] [int] NULL, [Y_ELEMENT] [varchar] (2000) NULL )

    CREATE TABLE [X_Y_INDEX] ( X_LOC [int] NULL, Y_LOC [int] NULL )

    /* Now we create some data to place into the tables, indexed */

    INSERT X_Y_INDEX (X_LOC, Y_LOC)SELECT 5,7

    INSERT X_DIMENSION (LOC,X_ELEMENT)SELECT 5,'DATA IN ELEMENT 5 '

    INSERT Y_DIMENSION (LOC,Y_ELEMENT)SELECT 7,'REMAINING

    DATA FOR ELEMENT 5,7'

    DECLARE @X INT, @Y INT

    SET @X = 5 -- or whatever method of loading you wantSET @Y = 7

    SELECT A.X_ELEMENT + B.Y_ELEMENT FROM X_DIMENSION A, Y_DIMENSION B WHERE A.LOC = @X AND B.LOC = @Y

    /* The Query returns the concatenated value! */

    /* DATA IN ELEMENT 5, REMAINING DATA IN ELEMENT 7*/

    Still pointless. Why use the X_Y_INDEX table, your not using it as part of the retreive, so why have it.

    If you think this all works, show us how you would store a second independant, seperate item in element (5,8).

    I'm astounded this got past any approval/editing process.

  • Randal Burke (11/4/2008)


    2) Captain Kirk once said, in his final battle with Khan - "He is evidencing classic two dimensional thinking" - we all evidence that quite often.

    I would have sworn that it was Spock who made this comment...

  • stax68 (11/4/2008)


    Jeff Moden (11/4/2008)


    Keep it clean, slick. 😉

    Oh, I do - mens sana in corpore sano old chap.

    And, don't blame me if you don't understand what I'm saying. :w00t:

    in that case may I congratulate you on your magnificent imperial robe!

    Wow... tough crowd, Tim. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Here is the actual quote from movie:

    He is intelligent, but not experienced. His pattern indicates 2 dimensional thinking. -- Spock

  • I read it the first time. You seemed to say (and therefore like it or not did say) it was about 3D arrays, but the author didn't go far enough in his explanation.

    From what I read of Jeff's comment it was the other way around, that the article didnt go far enough and should have included 3D arrays to further explain the process.

    And is there a full moon about somewhere, there seems to be so many agressive comments about this post...

  • Spot-on, David.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • David B (11/4/2008)


    I read it the first time. You seemed to say (and therefore like it or not did say) it was about 3D arrays, but the author didn't go far enough in his explanation.

    From what I read of Jeff's comment it was the other way around, that the article didnt go far enough and should have included 3D arrays to further explain the process.

    And is there a full moon about somewhere, there seems to be so many agressive comments about this post...

    Well, OK but 'far enough in Xing' to me means Xing, but not far enough.

    Re aggression - lots of criticism, certainly, along with a bit of robust badinage between Mr Moden and myself. Perhaps more emoticons needed to add some passivity, eh? 😉

    Tim Wilkinson

    "If it doesn't work in practice, you're using the wrong theory"
    - Immanuel Kant

Viewing 15 posts - 16 through 30 (of 38 total)

You must be logged in to reply to this topic. Login to reply