Professional Web Applications Themes

Storing in a variable - Microsoft SQL / MS SQL Server

Hello All, Is it possible to store the value of all records for a particular column in a single variable. I know this can be done by looping through a cursor. But is there any other way round. e.g. tableA has following records id Name 1 abc 2 pqr 3 xyz 4 opq Can it happen to store the Name field values in a variable. The output should be: Nam =abc,pqr,xyz,opq Thanks in advance Anand...

  1. #1

    Default Storing in a variable

    Hello All,
    Is it possible to store the value of all records for a
    particular column in a single variable. I know this can
    be done by looping through a cursor. But is there any
    other way round.

    e.g.
    tableA has following records
    id Name
    1 abc
    2 pqr
    3 xyz
    4 opq


    Can it happen to store the Name field values in a variable.

    The output should be:
    Nam =abc,pqr,xyz,opq

    Thanks in advance
    Anand






    Anand Guest

  2. #2

    Default Re: Storing in a variable

    Pls try as shown

    Create table TableA (id int,Name varchar(5))

    INSERT TableA values(1,'abc')
    INSERT TableA values(2,'pqr')
    INSERT TableA values(3,'xyz')
    INSERT TableA values(4,'opq')

    DECLARE NameList varchar(150), delimiter char
    SET delimiter = ','

    SELECT NameList = COALESCE(NameList + delimiter, '')
    + Name
    from TableA

    select NameList = NameList

    HTH,
    Srinivas Sampangi



    "Anand" <org> wrote in message
    news:072701c3565a$bb907dc0$gbl... 


    sampangi Guest

  3. #3

    Default Re: Storing in a variable

    Hi Srinivas,
    Thanks for the input. I never new about this.
    Thanks once again
    Anand
     [/ref]
    for a [/ref]
    variable. 
    >
    >
    >.
    >[/ref]
    Anand Guest

  4. #4

    Default Re: Storing in a variable

    Don't forget this feature in every new SQL Server you'll upgrade to as it's
    undoented for use and does not present in SQL standards

    "Anand" <org> wrote in message
    news:06d901c35661$24068790$gbl... [/ref]
    > for a [/ref]
    > variable. 
    > >
    > >
    > >.
    > >[/ref][/ref]


    Alex Guest

  5. #5

    Default Re: Storing in a variable

    I meant don't forget to check this feature etc. :)

    "Alex Cieszinski" <com> wrote in message
    news:phx.gbl... 
    it's 


    Alex Guest

Similar Threads

  1. storing and changing an external variable
    By fourinchesofego in forum Macromedia Flash Data Integration
    Replies: 3
    Last Post: May 10th, 06:25 PM
  2. Storing value from XML into a variable
    By sun4none in forum Macromedia Flash Data Integration
    Replies: 1
    Last Post: May 22nd, 11:37 PM
  3. storing quotes into a variable
    By Rex Brooks in forum PHP Development
    Replies: 1
    Last Post: September 12th, 02:20 AM
  4. [PHP] storing quotes into a variable
    By Chris Neale in forum PHP Development
    Replies: 1
    Last Post: September 11th, 09:34 PM
  5. Variable & storing
    By yop in forum ASP.NET General
    Replies: 3
    Last Post: July 25th, 03:19 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139