April 17, 2008 at 11:18 pm
Dear Gurus,
Following is my procedure
CREATE PROCEDURE prg_SHOW_VIEW_RUNNINGTIME ( @strViewName NVARCHAR(255) )
AS
BEGIN
DECLARE @strSql NVARCHAR(400)
,@dtStartTimeDATETIME
,@dtEndTimeDATETIME
,@rowcountINT
SET @dtStartTime = GETDATE()
SET @strSql = 'SELECT * FROM ' + @strViewName
EXEC @rowcount = sp_executesql @strSql
SET @dtEndTime = GETDATE()
SELECT @rowcount AS ROWS_AFFECTED
SELECT DATEDIFF(s,@dtStartTime,@dtEndTime) / 60 AS EXECUTION_TIME
END
from this @rowcount variable returning 0 value, but i want it should return the rows affected by the dynamic query
any help is highly appreciated
Thanks in Advance
April 17, 2008 at 11:51 pm
You need to pass @rowcount as na OUTPUT parameter to sp_executesql.
_____________
Code for TallyGenerator
April 18, 2008 at 5:26 am
Sergiy (4/17/2008)
You need to pass @rowcount as na OUTPUT parameter to sp_executesql.
i dont get can you alter my query and show me
thanks in advance
April 18, 2008 at 5:58 am
This might be simpler...
Replace this...
EXEC @rowcount = sp_executesql @strSql
with this...
EXEC sp_executesql @strSql
set @rowcount = @@RowCount
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
April 18, 2008 at 6:09 am
i already tried the result is 1 not rows affected
try yourside
April 18, 2008 at 6:15 am
I did.
DECLARE @sSQL NVARCHAR(100), @rowcount INT
SET @sSQL = 'select * from sysobjects'
EXEC sp_executesql @sSQL
SELECT @rowcount = @@ROWCOUNT
PRINT '@rowcount = ' + CAST(@rowcount AS VARCHAR(4))
Output in the messages tab -
(118 row(s) affected)
@rowcount = 118
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
April 18, 2008 at 6:18 am
GilaMonster (4/18/2008)
I did.
DECLARE @sSQL NVARCHAR(100), @rowcount INT
SET @sSQL = 'select * from sysobjects'
EXEC sp_executesql @sSQL
SELECT @rowcount = @@ROWCOUNT
PRINT '@rowcount = ' + CAST(@rowcount AS VARCHAR(4))
Output in the messages tab -
(118 row(s) affected)
@rowcount = 118
So did I! Thanks Gail.
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
April 21, 2008 at 12:31 am
shamshudheen (4/18/2008)
i already tried the result is 1 not rows affectedtry yourside
Hi guys,
I am sorry, i made a mistake after execute sql i called getdate() function and after i called @@rowcount, so that i got wrong result
but your methods are fine
thank you all
December 20, 2011 at 3:52 pm
Hello Friends,
I know this is an old topic but related to my issue.
I have a similiar requirement. How do I suppress the output of this sp_executesql.?
if we run the below sql statments, we get all the data relating to sysobjects.
DECLARE @sSQL NVARCHAR(100), @rowcount INT
SET @sSQL = 'select * from sysobjects'
EXEC sp_executesql @sSQL
SELECT @rowcount = @@ROWCOUNT
PRINT '@rowcount = ' + CAST(@rowcount AS VARCHAR(4))
but I am just interested in knowing only whether the select query is valid or not. I tried parseonly but the sql statments which I am running is inside a stored procedure (a sqlserver stored procedure cannot use parseonly command).
Help
Abhi
January 19, 2015 at 1:37 pm
Use
[Code]
Set NoCount ON
[/code]
at the beginning of the script. for good measure, turn it back off when done...
January 19, 2015 at 4:24 pm
Output parameter gives more flexibility, as you do not need to have the counted SELECT as the last statement.
It even allows you to aggregate 2 or more counts:
DECLARE @sSQL NVARCHAR(500), @rowcount INT
SET @sSQL = 'select * from sysobjects;
SET @Count = @@ROWCOUNT;
select * from syscolumns;
SELECT @Count = @Count + @@ROWCOUNT'
EXEC sp_executesql @sSQL, N'@Count int OUTPUT', @Count = @rowcount OUTPUT
PRINT '@rowcount = ' + CAST(@rowcount AS VARCHAR(10))
_____________
Code for TallyGenerator
January 19, 2015 at 5:48 pm
danurbin (1/19/2015)
Use[Code]
Set NoCount ON
[/code]
at the beginning of the script. for good measure, turn it back off when done...
Shouldn't need to turn it off. When the session closes, so does the setting.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 25, 2018 at 2:09 pm
DECLARE @sqlBody VARCHAR(500),@TableCount INT, @SQL NVARCHAR(1000)
SELECT @sqlBody = 'from sysobjects'SELECT @SQL = N'SELECT @TableCount = COUNT(*) ' + @sqlBody
EXEC sp_executesql @SQL, N'@TableCount INT OUTPUT', @TableCount OUTPUTSELECT @TableCountGO
January 25, 2018 at 2:15 pm
Please note: 3 year old thread
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
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply