Link to home
Create AccountLog in
Avatar of nigelr99
nigelr99Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Query design causing locked table / odbc errors

I have a view called view_quotation_list which is pretty much as it says, a detailed list of quotation information. Now these 'quote' records can have a non-unique quote_number and I created a second view called view_quote_totals which totals the values of records with the same quote_number as shown below:

SELECT quote_number, SUM(CAST((ISNULL(door_qty, 1) * quotation_total) * (1 - CAST(ISNULL(discount, 0) AS DECIMAL) / 100) AS DECIMAL(9 , 0))) AS quote_value
FROM dbo.quotations
GROUP BY quote_number

Open in new window



This should give me output such as that below:
Id / Customer / Quote Number / Quote Value
100 / Customer A / 123 / £100
101 / Customer B / 124 / £500
102 / Customer B / 124 / £500
103 / Customer B / 124 / £500
104 / Customer C / 125 / £50 etc.

It seems that by simply linking the totals view to the list view (by the quote number field) causes some sort of locking with major problems for all database users. My Access front-end is unable to save changes to the quotations table and ends up giving a query time-out ODBC error or communication link failure error. I should also say that I am using a pass through query in Access to return x records from view_quotation_list as follows:

SET DATEFORMAT dmy;SELECT TOP 200 * FROM dbo.view_quotation_list WHERE latest_revision=1 ORDER BY quote_number DESC, item_number ASC, revision_number DESC;

Open in new window


If I remove the totals view, everything works fine again so I tried putting the select statement shown below (which calculates the totals) into the original list view but the same 'locking' happens again.

(SELECT SUM(CAST((ISNULL(door_qty, 1) * quotation_total) * (1 - CAST(ISNULL(discount, 0) AS DECIMAL) / 100) AS DECIMAL(9, 0))) AS Expr1
FROM          dbo.quotations AS q
WHERE      (quote_number = dbo.quotations.quote_number) AND (latest_revision = 1)) AS quote_value

Open in new window



It's as though there's a recursive process going on maybe due to my query design? Any thoughts on alternative solutions or insights into where I've gone wrong most welcome. Thanks
Avatar of nigelr99
nigelr99
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I've also included an image of view_quotation_list design, including view_quotation_totalsUser generated image
Avatar of Guy Hengel [angelIII / a3]
as this is in a SQL server db, could you run the relevant SQL in SQL server itself and get the explain plan for it?
by that we could see if there are full table scans resulting in this "bad" behavior.
the solution would be to add appropriate indexes.
I need to add some further detail for this problem as it seems everything works fine in SSMS, it's only when I add an additional WHERE clause specifically against the quote_total field to the pass-through query in my Access code that the problem occurs.

So, SELECT * FROM view_quotation_list is fine but
SELECT * FROM view_quotation_list WHERE quote_value>10000 causes the issue immediately.

I've checked I have an index on the quote_number field and even with this WHERE clause it works fine in SSMS.

angelIII - could you please specify what exactly I'm looking for in the execution plan as I've little experience of analysing these (I know I should ... don't tell me!)
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks for your help angelIII- after a lot of digging around / research / trials etc. etc. I still couldn't find a solution so ended up adding a seperate form in Access to avoid having to include totals as originally designed.
The problem only occurred when executing the query in Access so I guess it's some strange scenario which I'd like to think was merely a Microsoft 'feature'!
Although unable to solve the problem, I'm sure with enough time, AngelIII's references would have led me to the cause of the problem so the points are yours! Thanks