SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


assigning the value of a one fieldf query to a variable/parameter, one record at a time.


assigning the value of a one fieldf query to a variable/parameter, one record at a time.

Author
Message
stevenplee
stevenplee
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 307
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.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61529 Visits: 17954
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
stevenplee
stevenplee
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 307
OK, Thank you. Actually this is in PL/SQL but the process logic should basically be the same.

I have a query as SELECT DISTINCT Field FROM Table a

I would like to set the value of parameter as follows

Step A
set Param = (SELECT DISTINCT Field FROM table a) -- mind you I only need the first record from this query

Param is then used in a series of other queries below.


Step B
INSERT INTO Table b
SELECT FIELD, FIELD, ... FROM Table c where Field = Param

UPDATE Table b
SET FIELD_Z = (SELECT FIELD_1 FROM Table d WHERE field_3 = Param

.....

Once all of the query have been executed, I need to go back to STEP A and this time use the next record returned by the query in Step a.

The process is repeated until all the records in Step A query have been processed.
stevenplee
stevenplee
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 307
Mind you, this script will be used in a ETL process that will be run weekly
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61529 Visits: 17954
stevenplee (8/16/2013)
OK, Thank you. Actually this is in PL/SQL but the process logic should basically be the same.

I have a query as SELECT DISTINCT Field FROM Table a

I would like to set the value of parameter as follows

Step A
set Param = (SELECT DISTINCT Field FROM table a) -- mind you I only need the first record from this query

Param is then used in a series of other queries below.


Step B
INSERT INTO Table b
SELECT FIELD, FIELD, ... FROM Table c where Field = Param

UPDATE Table b
SET FIELD_Z = (SELECT FIELD_1 FROM Table d WHERE field_3 = Param

.....

Once all of the query have been executed, I need to go back to STEP A and this time use the next record returned by the query in Step a.

The process is repeated until all the records in Step A query have been processed.


Like I said before, this is a cursor. I am no Oracle expert so I can't help you at all with the syntax but this is probably a decent place to start.

http://www.oracle.com/technetwork/issue-archive/2013/13-mar/o23plsql-1906474.html

_______________________________________________________________

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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
stevenplee
stevenplee
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 307
OK, Thank you
freepeople8
freepeople8
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
Good article.Thanks for sharing.

tory burch
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search