August 16, 2013 at 9:50 am
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.
August 16, 2013 at 10:15 am
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/
August 16, 2013 at 10:28 am
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.
August 16, 2013 at 10:31 am
Mind you, this script will be used in a ETL process that will be run weekly
August 16, 2013 at 10:41 am
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 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/
August 16, 2013 at 10:50 am
OK, Thank you
March 3, 2014 at 1:54 am
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply