August 9, 2010 at 4:44 am
I need to find the number of rows affected for each query, when I fire set of queries
For e.g.:
Select top 10 * from Employee
Insert into Employee1 Select * From Employee
Update Employee1 Set Name = 'Jon' Where name = 'mic'
RESULT:
Select top 10 * FRom Employee
10 rows affected
Insert into Employee1 Select * From Employees
50 rows affected
Update Employee1 Set Name = 'Jon' Where name = 'mic'
2 rows affected
Need: I need to audit user Queries and don't have control on the queries. So i need to write a backend scriot to check (check like query and rows affected) for the multiple queries fired by set of user.
August 9, 2010 at 4:53 am
SET NOCOUNT OFF .
When u normally execute a query, you sure will get the number of rows affected in the "Message" Pane of the Results Window, unless u specify SET NOCOUNT ON...
August 9, 2010 at 5:00 am
Thanks for reply. But how can aduit the qureies fired by end users against the rows affected?
August 9, 2010 at 5:52 am
You can use @@rowcount function to capture the no. of rows affected for the query executed.
Thanks,
Amit Kulkarni
August 9, 2010 at 5:55 am
If i use @@rowcount, then i will get for last query but not set of qureies fired 🙁
August 9, 2010 at 6:03 am
if you need the total, you;ll need to declare a variable, and add them together:
declare @TotalRows int
SET @TotalRows = 0
Select top 10 * from Employee
SET @TotalRows = @TotalRows + @@rowcount
Insert into Employee1 Select * From Employee
SET @TotalRows = @TotalRows + @@rowcount
Update Employee1 Set Name = 'Jon' Where name = 'mic'
SET @TotalRows = @TotalRows + @@rowcount
Lowell
August 9, 2010 at 6:42 am
Hi OP,
It sounds to me like you want to capture the rows affected by queries run by users of the database. For instance if the application (could be a custom app written in c++ for instance) has a query window that lets you run an ad-hoc query on the database, you want to capture the rows affected when Bob from accounting selects the number of outstanding payments this month, is that correct?
As opposed to you creating multiple statements in SSMS and wanting to know the row counts of your own queries..
Jim.
August 9, 2010 at 6:47 am
You are correct Jim. I need to capture the rows affected for each query fired along with it's counter part. At a time multiple queries can be fired together.
August 9, 2010 at 7:06 am
Would I also be correct to assume that you want this to run in the background to create a file that shows the queries with the user's name, time and rows affected?
Meaning that the profiler won't be a solution?
Jim.
EDIT:
I've found some info that you might find useful. You could create a trace that will create a file for you.
How to create a trace file:
http://support.microsoft.com/kb/283790/
This gives some info on what can be traced:
http://technet.microsoft.com/en-us/library/ms186265.aspx
Obviously you should expect the relevant decreases in performance.
Jim
August 9, 2010 at 7:09 am
Let me try using profiler. Do we have any other solution than profiler?
August 10, 2010 at 10:38 pm
Thanks to all. Since we have trace enabled for our databased. I used them to extract required info.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply