April 9, 2007 at 11:31 pm
SET IDENTITY INSERT [dbo].[Table1] ON
INSERT [dbo].[Table1](Col1, Col2, Col3)
SELECT Col1, Col2, Col3
FROM [RemoteSvr].[DBName].[dbo].[Table1]
WHERE Col1 = '12345'
SELECT @@ROWCOUNT
SET IDENTITY INSERT [dbo].[Table1] OFF
SELECT Col1, Col2, Col3
FROM [RemoteSvr].[DBName].[dbo].[Table1]
WHERE Col1 = '12345'
SELECT @@ROWCOUNT
Then I actually get returned back '9'.
Now I am doing this sort of query for several different tables, and those tables it will return the correct @@ROWCOUNT for the number of rows inserted.
Any ideas why @@ROWCOUNT would not work for this table?!?
April 9, 2007 at 11:50 pm
Scope of system functions is limited to the local server.
Since no rows have been inserted or selected locally @@ROWCOUNT returns 0.
_____________
Code for TallyGenerator
April 11, 2007 at 12:12 am
I dont think the issue with selection of Remoteserver data.
Since selection is from remote server. And that too is giving right @@rowcount value when only select was used.
What I am suspecting is the Set IDENTITY Insert on statement, which stops @@rowcount variable to have valid values.
April 11, 2007 at 8:46 am
I ran the following test, and @@ROWCOUNT return 3, as expected. The difference here is that I don't have access to a remote server. However, since you are inserting data FROM a remote server INTO a local server, that shouldn't matter (but it might!). The SET IDENTITY_INSERT Table1 ON statement didn't adversely affect @@ROWCOUNT.
DROP TABLE dbo.Table1
GO
DROP TABLE dbo.Table2
GO
CREATE TABLE dbo.Table1
(
id int IDENTITY(1,1)
, col1 varchar(5)
, col2 varchar(5)
, col3 varchar(5)
)
GO
CREATE TABLE dbo.Table2
(
id int IDENTITY(1,1)
, col1 varchar(5)
, col2 varchar(5)
, col3 varchar(5)
)
GO
INSERT dbo.Table1 (col1) VALUES ('11111')
INSERT dbo.Table2 (col1)
SELECT '12345'
UNION ALL
SELECT '12345'
UNION ALL
SELECT '12345'
SET IDENTITY_INSERT Table1 ON
INSERT dbo.Table1 (id, Col1, Col2, Col3)
SELECT id, Col1, Col2, Col3
FROM Table2
WHERE Col1 = '12345'
SELECT @@ROWCOUNT
SET IDENTITY_INSERT [dbo].[Table1] OFF
April 11, 2007 at 8:52 am
I agree with Avinash. I just tried to select and insert into a table and @@rowcount function worked. Since it's a global function there maybe something else that setting the rowcount to 0. Why don't you declare a variable and set the rowcount to the variable and check the result.
Good day...
Bulent
April 11, 2007 at 9:15 am
I tried executing the below queries and found no problem from my side.
SET IDENTITY_iNSERT [dbo].[Table1] ON
INSERT [dbo].[Table1](Col1, Col2, Col3)
SELECT Col1, Col2, Col3 FROM [remotesrv].[remotedb].[dbo].[Table2]
SELECT @@ROWCOUNT as 'rowcount'
SET IDENTITY_INSERT [dbo].[Table1] OFF
And it gives pretty gud result too........
rowcount
-----------
3
Krissy, I was wondering if you can provide me of how Linked server was added onto your native Server..
April 11, 2007 at 11:39 am
Can the actions performed by a trigger affect the @@rowcount returned?
April 11, 2007 at 12:14 pm
Yes Donald, they do affect the @@rowcount variable. Say for example you have the following trigger on Table1 then you would only receive those Rowcount which may be result of the DMLs written in trigger itself.
Create trigger abcIns
on Table1
for insert
as
declare @col1var int
insert into #temptbl(timeofinsert,idoninsert) values(getdate(), @col1)
return.
Now whenever any insert is made on Table1 it will return only the rowcount of the insert statement(it may be any DML statement) inside the trigger. This carried to scope of the session in which trigger firing insert statement was issued.
April 11, 2007 at 1:35 pm
Thanks,
I thought that was the case.
So maybe there is an insert trigger on table1 that is changing the @@rowcount that is being returned. This would explain why the select showed the correct @@rowcount but the insert didn't. Just a thought.
June 25, 2012 at 2:35 pm
I know this topic is old, but I just tried to affect the value of @@rowcount using a trigger and I could not. I would insert 1 record into a table, then have a trigger select, insert or update many records (I tried all three) and the next @@rowcount that I ran after my initial insert would return a value of 1. An @@rowcount inside the trigger would return values greater than 1, but not the next @@rowcount in the scope of the initial insert statement. It looks to me like @@rowcount operates like scope_identity() rather than @@identity in the sense that it is scope-dependent. I got the same results in SQL 2000, 2005 and 2008r2.
Same result here:
http://stackoverflow.com/questions/7005225/sql-server-does-trigger-affects-rowcount
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply