SQL Server Developer Center > SQL Server Forums > Transact-SQL > possible in tsql? return one row then access individual fields from it?
Ask a questionAsk a question
 

Answerpossible in tsql? return one row then access individual fields from it?

  • Wednesday, November 04, 2009 2:23 PMc0pe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,
    Let say within a stored procedure you need to get several values that you need to be able to reference individually, but all of these values live within one record of a table. Instead of writting several individual statements like this:

    declare @sampleVar1 nvarchar(50)
    set @sampleVar1 = (select sampleField1 from sampleTable1 where PKID = 999)

    declare @sampleVar2 nvarchar(50)
    set @sampleVar2 = (select sampleField2 from sampleTable1 where PKID = 999)

    declare @sampleVar3 nvarchar(50)
    set @sampleVar3 = (select sampleField3 from sampleTable1 where PKID = 999)

    etc...

    is it possible to just use one select statement and store the whole row in a variable that you could subsequently access individual fields from? something like:

    declare @theRow (not sure on datatype if this is possible)
    set @theRow = (select * from sampleTable1 where PKID = 999)

    declare @sampleOutput nvarchar(max) = 'some labels ' + @theRow.sampleField1 + ' some more text ' + @theRow.sampleField2

    etc..

    ?

Answers

  • Wednesday, November 04, 2009 2:36 PMRadu Negru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    One way to obtain your values:

    DECLARE @v1 nvarchar(50), @v2 nvarchar(50);
    SELECT
        @v1 = sampleField1,
        @v2 = sampleField2
    FROM
        sampleTable1
    WHERE
        (PKID = 999)
    ;
    

    • Proposed As Answer bySean Gallardy Wednesday, November 04, 2009 2:39 PM
    • Marked As Answer byc0pe Wednesday, November 04, 2009 3:21 PM
    •  

All Replies

  • Wednesday, November 04, 2009 2:30 PMAbdshall Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You can't access the row this way. If you need to get part of the @TheRow you have to use something like substring.
    Also, I see that you are retrieving three values from three different columns in the following statement and not just one or part of a row. What are you trying to do may be we can help out.


    declare @sampleVar1 nvarchar(50)
    set @sampleVar1 = (select sampleField1 from sampleTable1 where PKID = 999)

    declare @sampleVar2 nvarchar(50)
    set @sampleVar2 = (select sampleField2 from sampleTable1 where PKID = 999)

    declare @sampleVar3 nvarchar(50)
    set @sampleVar3 = (select sampleField3 from sampleTable1 where PKID = 999)

    Abdallah, PMP, MCTS
  • Wednesday, November 04, 2009 2:36 PMRadu Negru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    One way to obtain your values:

    DECLARE @v1 nvarchar(50), @v2 nvarchar(50);
    SELECT
        @v1 = sampleField1,
        @v2 = sampleField2
    FROM
        sampleTable1
    WHERE
        (PKID = 999)
    ;
    

    • Proposed As Answer bySean Gallardy Wednesday, November 04, 2009 2:39 PM
    • Marked As Answer byc0pe Wednesday, November 04, 2009 3:21 PM
    •  
  • Wednesday, November 04, 2009 2:44 PMAbdshall Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    One way to obtain your values:

    DECLARE @v1 nvarchar(50), @v2 nvarchar(50);
    
    SELECT
    
        @v1 = sampleField1,
    
        @v2 = sampleField2
    
    FROM
    
        sampleTable1
    
    WHERE
    
        (PKID = 999)
    
    ;
    
    


    Radu, he is already doing that. His question is to see if he can create some kind of a variable and hold the enitre dataset that comes back from the table in that one variable. More like a datareader in .Net.
    Abdallah, PMP, MCTS
  • Wednesday, November 04, 2009 2:45 PMMike_MISQL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You could simply use @theRow as a table variable, perhaps?

    Whether it's simply one row or a table with one row, there doesn't seem to be a whole lot of difference in execution.

    declare @theRow table (sample1 int, sample2 bit, sample3 varchar(30))

    insert into @theRow
    select sample1, sample2, sample3 from sampleTable where ID = 5

    declare @sampleOutput varchar(max)

    select @sampleOutput = 'stuff' + r.sample1 + 'more stuff' + r.sample2 from @theRow as r


    Although, you can skip the variable part altogether and just select @sampleOutput = 'stuff' + r.sample1 from sampleTable where ID = 5

    Unless I'm missing what you're trying to do (which is certainly possible).
  • Wednesday, November 04, 2009 2:49 PMAbdshall Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Have you tried the table data type? Like


    DECLARE @theRow TABLE


    Abdallah, PMP, MCTS
  • Wednesday, November 04, 2009 3:05 PMc0pe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    thank you both for the input. To clarify, I was really looking to see if there was a way to get at all the data I needed with one call to the database rather than the multiple individual calls I was making... I only had the 3 individual calls there as an example but it is more like a dozen or so I will be making. It occurred to me that this may be ineffecient, as I typically only to very basic tsql queries and don't have much experience with the language beyond that. So I think Radu Negru's first response is right on the money... having all the data accessible from one variable is not required, its just the first thing I thought of coming from a .net background...
  • Wednesday, November 04, 2009 3:06 PMD.Padmanabhan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi,

    I would use XML for a situation like this. Other solutions are appreciated.

    Declare @xmlData XML
    
    Set @xmlData = 
    (
    select
    	[col1] "@Col1"
      , [col2] "@Col2"
      , [col3] "@Col3"
    from 
    	#test
    Where
    	[id] = 1
    For XML Path
    )
    
    declare @sampleOutput nvarchar(max)
    
    set @sampleOutput = 
    (
    SELECT
    	'some labels ' + @xmlData.query('data(row/@Col1)').value('.','varchar(100)')
    	+ ' more text ' + @xmlData.query('data(row/@Col2)').value('.','varchar(100)')
    	+ ' some more text ' + @xmlData.query('data(row/@Col3)').value('.','varchar(100)')
    	AS Val
    )
    print @sampleOutput

    Just a rough code, can definitely be made better. There are good resources in http://blog.beyondrelational.com/ for you to get started with XML.

    Cheers
    Padmanabhan
    • Edited byD.Padmanabhan Wednesday, November 04, 2009 3:07 PMcode error, changed it
    •  
  • Wednesday, November 04, 2009 3:18 PMRadu Negru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm glad you find your answer c0pe! Now, help us help you. What is that you are trying to do? I have a feeling that it's not really intented to T-SQL.
  • Wednesday, November 04, 2009 4:08 PMtechnocrat_aspire Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    you should be able to declare a variable of type table and then store the values in that table.