Home Forums SQL Server 2008 Working with Oracle assigning the value of a one fieldf query to a variable/parameter, one record at a time. RE: assigning the value of a one fieldf query to a variable/parameter, one record at a time.

  • stevenplee (8/16/2013)


    I need to be able to use the results of a simple query, but one record at a time.

    I have a query that will return only one field, There are a total of 25 records as the result of the query. What I need to do is create a loop that will take the value of the first record returned by the simple query and assign the value to a variable.

    This variable is bound to several block that will run. After they have completed. I need to be able to get the next value from the simple query, pass it to the variable and it will then be used in the query blocks.

    The process will be repeated until all of the records returned by the simple query has been used in the variable.

    Any suggestions.

    Sounds like a cursor. If at all possible you should avoid cursors in sql server because they are horribly inefficient. If you can provide some details about what you are trying to do we can help you come up with a set based version that would be much faster.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/