Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

assigning the value of a one fieldf query to a variable/parameter, one record at a time. Expand / Collapse
Author
Message
Posted Friday, August 16, 2013 9:50 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 9:26 AM
Points: 72, Visits: 286
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.
Post #1485275
Posted Friday, August 16, 2013 10:15 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,302, Visits: 12,168
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 Moden's 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)
Post #1485292
Posted Friday, August 16, 2013 10:28 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 9:26 AM
Points: 72, Visits: 286
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.
Post #1485302
Posted Friday, August 16, 2013 10:31 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 9:26 AM
Points: 72, Visits: 286
Mind you, this script will be used in a ETL process that will be run weekly
Post #1485306
Posted Friday, August 16, 2013 10:41 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,302, Visits: 12,168
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 Moden's 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)
Post #1485309
Posted Friday, August 16, 2013 10:50 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 9:26 AM
Points: 72, Visits: 286
OK, Thank you
Post #1485312
Posted Monday, March 3, 2014 1:54 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, March 3, 2014 1:44 AM
Points: 1, Visits: 1
Good article.Thanks for sharing.

tory burch
Post #1546817
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse