Hi folks, I have a question re: query loops, variables and table updates. I'm currently performing a large table update (1.2M records) via a ColdFusion template. I would like to change this update so I can perform it using a stored procedure and/or DTS, leaving CF out of the picture. Here's how it works:
The purpose of the update is to update a table with expenses which has expense data by manager. This is tracked by manager by month, with the expense tracking being handled by a hierarchical nine character code called a responsibility code (RC). These RCs change frequently, so I have users update a mapping table that contains the manager ID numbers and any old RCs that belong to them.
The three tables involved:
Managers - contains the current manager records only
RCMap - contains the current manager IDs with the associated old RCs
Expenses - contains monthly expenses with current RCs only.
Here's what I'm doing in ColdFusion:
1. Query the Managers table Inner joined with the RCMap table, getting only managers who have mapped cost centers.
SELECT DISTINCT Managers.UID, Managers.RC
FROM Managers INNER JOIN RCMap
ON Managers.UID = RCMap.UID
2. Loop through the first query and obtain a list of all the old RCs I need to update in the Expenses table
SELECT OldRC FROM RCMap WHERE UID = var(Query1.UID)
3. Still inside the initial query loop, run an update query on the Expenses table to update all the old RCs with the current RC. I'm using a CF function, QuotedValueList, which turns the results of the second query to into a list WITHOUT having to loop through that second query.
UPDATE Expenses SET NewRC = var(Query1.RC)
WHERE RC IN var(QuotedValueList(Query2.OldRC))
This third step is the crux of the whole deal because I don't have to loop through Query 2 to do the update, thereby saving me quite a bit of processing time.
So, can this type of script be run inside a stored procedure? I don't even need to pass any variables, the variables are all query result set generated.
Thanks in advance!
Kerr
(For you CF'ers I know this is not syntactically correct in CF but I wanted to be clear on where I'm using query driven variables to see how this can be done using SQL Server only.)
The purpose of the update is to update a table with expenses which has expense data by manager. This is tracked by manager by month, with the expense tracking being handled by a hierarchical nine character code called a responsibility code (RC). These RCs change frequently, so I have users update a mapping table that contains the manager ID numbers and any old RCs that belong to them.
The three tables involved:
Managers - contains the current manager records only
RCMap - contains the current manager IDs with the associated old RCs
Expenses - contains monthly expenses with current RCs only.
Here's what I'm doing in ColdFusion:
1. Query the Managers table Inner joined with the RCMap table, getting only managers who have mapped cost centers.
SELECT DISTINCT Managers.UID, Managers.RC
FROM Managers INNER JOIN RCMap
ON Managers.UID = RCMap.UID
2. Loop through the first query and obtain a list of all the old RCs I need to update in the Expenses table
SELECT OldRC FROM RCMap WHERE UID = var(Query1.UID)
3. Still inside the initial query loop, run an update query on the Expenses table to update all the old RCs with the current RC. I'm using a CF function, QuotedValueList, which turns the results of the second query to into a list WITHOUT having to loop through that second query.
UPDATE Expenses SET NewRC = var(Query1.RC)
WHERE RC IN var(QuotedValueList(Query2.OldRC))
This third step is the crux of the whole deal because I don't have to loop through Query 2 to do the update, thereby saving me quite a bit of processing time.
So, can this type of script be run inside a stored procedure? I don't even need to pass any variables, the variables are all query result set generated.
Thanks in advance!
Kerr
(For you CF'ers I know this is not syntactically correct in CF but I wanted to be clear on where I'm using query driven variables to see how this can be done using SQL Server only.)