Discussion Locked

Conditional Update/SQLServer Stored Proc

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

The following SQL Server Stored Procedure code portion (in the attached Word doc written in T-SQL) is working correctly. It represents a pattern in the procedure which is taking the largest chunk of execution time. Since the condition shown in eachUPDATE block is exclusive of the others, I want to rewrite it so that each successive block only looks at records which have not already met the conditions of the previous ones, etc. The present code searches the whole table in each UPDATE block.My problem lies in rewriting it by using nested IF logic in T-SQL.

UPDATE TableA
SET FieldNew="OTHER LIAB"
WHERE CC_Code = "OL" AND Kol = "01"

UPDATE TableA
SET FieldNew="OTHER"
WHERE CC_Code = "AL" AND Kol = "60"

UPDATE TableA
SET FieldNew="OTHER"
WHERE CC_Code <> "CL" AND CC_Code <> "CP" AND CC_Code <> "03" AND Kol = "31"

UPDATE TableA
SET FieldNew="DISABILITY"WHERE PP_Code = "IA" AND (CC_Code = "WI" OR CC_Code = "DI")

UPDATE TableA
SET FieldNew="MEDICAL"
WHERE PP_Code = "IA" AND (CC_Code = "MD" OR CC_Code = "WR" OR CC_Code = "WM")

9 replies

by gbworld

I would assume that this type of logic would only be run once, and that you would use some other method for maintenance.

One way to solve this problem is to pull a recordset into VB and either append the new field and place in the database, or update each record as it is cursored through. This would take the huge hit off the database server. The decision of updating the database in one step with the recordset or updating one record at a time would depend on the amount of concurrency you need. If the data is being updated elsewhere, you would want to update one record at a time. Adjust the cursor in the recordset for greater concurrency.

Another way to speed up this process (although still time consuming) would be to make a lookup table for the fieldNew value:

tblFieldNew
-----------
FieldNewID
FieldNewName

This is faster due to the fact that it is working with numbers instead of characters. The workload, however, would still be large.

Another option is to move to a cursor or a temp table and do the work there and then migrate it back. This may even be more painful, in some respects.

If you are talking about adding this process to every record that is added, you can make a trigger for INSERT or UPDATE to add the new field. This does not solve the immediate problem, but does not force code changes in the INSERT process.

To speed up your current problem, if it is just a maintenance issue, you would probably be better served to add temporary indexes to speed up the work. This will solve adding the new field.

I hope these ideas are on target for what you are looking to do. I generally do not suggest putting huge amounts of work into things that, by design, are one time processes.

Greg

by glen_mcleod

Try this (SQL Server 7.0) (hopefully the formatting will survive the posting process):

Update TableA Set FieldNew =
Case
When CC_Code = 'OL' AND Kol = '01'
Then 'OTHER LIAB'
When CC_Code = 'AL' AND Kol = '60'
Then 'OTHER'
When CC_Code <>'CL' AND CC_Code <> 'CP' AND CC_Code <> '03' AND Kol = '31'
Then 'OTHER'
When PP_Code = 'IA' AND (CC_Code = 'WI' OR CC_Code = 'DI')
Then 'DISABILITY'
When PP_Code = 'IA' AND (CC_Code = 'MD' OR CC_Code = 'WR' OR CC_Code = 'WM')
Then 'MEDICAL'
End

by glen_mcleod

In response to your comment: In programming languages like C/C++, VB, java, ect., replacing multiple If...ElseIf... clauses with a Switch or Select Case equivalent is standard programming practice. SQL Server offers just that capability in T-SQL, which is what I posted. Take another look.

Glen

by frankus

The respondent's suggestions are beyond my control to implement. I do not maintain this particular database. The UPDATEs shown in the code do not update the database. They are part of a query by external clients. The query is not run once, but many times during each day without altering the database. It is a complex query done by different clients, not an occasional update based on added information or new records. We are talking about 2 to 5 million records that have to be queried. The UPDATE in the submitted code creates a temporary table which feeds a report. It is a way to catagorize the entries based on the given criteria. The queries are initiated by clients through the internet. I.O.W.: I need to do the catagorizing ("MEDICAL", "OTHER", etc) for an inputted set of values for fields other than the criteria in the attached code. It has to be done in the stored procedure in T-SQL language. This query does not alter any data in the database. Changes in the database are done by separate sources, which are not controlled here. Otherwise, as the first answer implies, the whole database could be restructured so that updates to it include the catagories needed for our query.
I haven't rated the answer for the above reasons. As I said, what I have now works, but it is significantly slower than a simple MSAccess query, where IIF clauses are allowed. I know it can be done with IF statements in T-SQL. However, they have not worked for me. And none of the examples for IF clauses I find in the documentation or on the web show how to use them to insert values into record fields. What I tried is similar to:

UPDATE TableA
IF PP_Code = "IV" AND CC_Code = "MX"
SET FieldNew="OTHER LIAB"
ELSE
IFPP_Code = "IX" AND CC_Code = "ME"
SET FieldNew="PAID"

etc..

This discussion is closed to new replies.