August 15, 2011 at 6:12 am
i got this query
UPDATE tts
set tts.Company=t3.IndexId
from TargetTable_Staging tts
inner join (
select t1.id as PKid,t2.id as IndexId
FROM TargetTable t1 inner join Dyn_Company t2 (NOLOCK)
ON t1.Company=t2.Name COLLATE SQL_Latin1_General_CP1_CI_AS )t3 on tts.id=t3.PKid
after the query i try to get the row count :
Select @intErrorCode=@@ERROR,@intRowsCount=@@ROWCOUNT
i get wrong number on @intRowsCount
but when i query it i get the correct result (it takes few seconds between the query executed inside a Stored procedure, and when i manually query the table
any idea?
August 15, 2011 at 6:16 am
Is there a trigger on the table? Is that SELECT @@rowcount the VERY NEXT statement after the update?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 15, 2011 at 6:37 am
no trigger
and the query immediately after the update
August 15, 2011 at 7:11 am
-- run this
SELECT COUNT(*)
from TargetTable_Staging tts
inner join (
select t1.id as PKid,t2.id as IndexId
FROM TargetTable t1 inner join Dyn_Company t2 (NOLOCK)
ON t1.Company=t2.Name COLLATE SQL_Latin1_General_CP1_CI_AS )t3 on tts.id=t3.PKid
-- and this
select COUNT(*)
FROM TargetTable t1 inner join Dyn_Company t2 (NOLOCK)
ON t1.Company=t2.Name COLLATE SQL_Latin1_General_CP1_CI_AS
-- it's likely that the results are different.
-- which one do you want to record?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
August 15, 2011 at 8:04 am
first one
August 15, 2011 at 8:12 am
peleg (8/15/2011)
first one
Is your rowcount equal to the result of this query?
SELECT COUNT(*)
FROM TargetTable_Staging tts
WHERE EXISTS (SELECT 1
FROM TargetTable t1
INNER JOIN Dyn_Company t2
ON t1.Company=t2.Name COLLATE SQL_Latin1_General_CP1_CI_AS WHERE t1.id = tts.id)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
August 15, 2011 at 8:36 am
yes
August 15, 2011 at 8:56 am
@@ROWCOUNT will be the number of rows updated. Bear in mind that a row may be updated only once. Your findings indicate that for some rows of your target table, the results of the UPDATE ... FROM would touch the same target row more than once.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply