Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to assign count(*) to a variable using  a dynamically generated statement excecuted by sp_executesql

Posted on 2004-09-22
8
Medium Priority
?
1,162 Views
Last Modified: 2008-01-09
I am trying to perform a select count(*) against a set of tables within a database.  The tablenames and database names are dynamic.  I am trying to assign the count to a variable for inserting into a second table that holds auditing information.

I have tried 2 different approaches thus far and receive different error messages syntax errors:

Approach 1:
declare @RowCount bigint
declare @SourceTable varchar(100)
declare @SourceDatabase varchar(100)
declare @SQLStatement varchar(4000)

set @SourceTable = 'DLctblSource'
set @SourceDatabase = 'discode_Db'
set @RowCount = 0

set @SQLStatement = 'select count(*) from '+ @SourceDatabase +'.dbo.'+ @SourceTable + ''
print @SQLStatement
 execute master.dbo.sp_executesql @SQLStatement
Error Message:  Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'

Approach 2:
declare @RowCount bigint
declare @SourceTable varchar(100)
declare @SourceDatabase varchar(100)
declare @SQLStatement varchar(4000)
set @SourceTable = 'DLctblSource'
set @SourceDatabase = 'discode_Db'
set @RowCount = 0
select @RowCount = count(*) from '+ @SourceDatabase +'.dbo.'+ @SourceTable + '
print @SQLStatement
Error Message: Line 11: Incorrect syntax near '+ @SourceDatabase +'.

I am then trying to insert either the @RowCount value into the audit table or am inserting the results of the select count(*) by

using set @SQLStatement = 'insert into Audit_tbl(tblname,rec_cnt) select  @tblname,count(*) from '+ @SourceDatabase +'.dbo.'+ @SourceTable +)

I am encountering syntax errors with both approaches- getting late in PM on East Coast
0
Comment
Question by:mikevs
8 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 12127495
Method 1 should be used, with some adjustments :-) :


declare @SQLStatement Nvarchar(4000)

set @SQLStatement = N'select @RowCount = count(*) from '+ @SourceDatabase +N'.dbo.'+ @SourceTable + N''
print @SQLStatement
execute master.dbo.sp_executesql @SQLStatement, '@RowCount BIGINT OUTPUT', @RowCount OUTPUT
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 12127514
CORRECTIONS:

execute sp_executesql @SQLStatement, N'@RowCount BIGINT OUTPUT', @RowCount OUTPUT
0
 

Author Comment

by:mikevs
ID: 12127562
declare @SQLStatement Nvarchar(4000)
declare @SourceTable varchar(30)
declare @SourceDatabase varchar(30)
declare @RowCount bigint
set @SourceTable = 'DLctblSource'
set @SourceDatabase = 'DiscoveryStage_Db'
set @RowCount = 0
set @SQLStatement = N'select @RowCount = count(*) from '+ @SourceDatabase +N'.dbo.'+ @SourceTable + N''
print @SQLStatement
execute master.dbo.sp_executesql @SQLStatement, '@RowCount BIGINT OUTPUT', @RowCount OUTPUT

Result:
/*-----------------------------
declare @SQLStatement Nvarchar(4000)
declare @SourceTable varchar(30)
declare @SourceDatabase varchar(30)
-----------------------------*/
select @RowCount = count(*) from DiscoveryStage_Db.dbo.DLctblSource
Server: Msg 214, Level 16, State 3, Procedure sp_executesql, Line 10
Procedure expects parameter '@parameters' of type 'ntext/nchar/nvarchar'.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 9

Expert Comment

by:paelo
ID: 12127598
Is DLctblSource a table or a procedure?

-Paul.
0
 
LVL 6

Expert Comment

by:jchopde
ID: 12127627
As ScottPletcher wrote, you need to change this line

execute master.dbo.sp_executesql @SQLStatement, '@RowCount BIGINT OUTPUT', @RowCount OUTPUT

to

execute master.dbo.sp_executesql @SQLStatement, N'@RowCount BIGINT OUTPUT', @RowCount OUTPUT
0
 
LVL 4

Expert Comment

by:davehilditch
ID: 12127666
Change this line:

set @SQLStatement = 'select count(*) from '+ @SourceDatabase +'.dbo.'+ @SourceTable + ''

to

set @SQLStatement = 'select count(*) from '+ @SourceDatabase + '.dbo.' + @SourceTable -- <-- why were you adding empty quotes here?

then instead of

execute master.dbo.sp_executesql @SQLStatement

just use:

exec(@SQLStatement)

Technically, exec not quite as fast as sp_executesql, but we are talking milliseconds, and it will work with code above.

Dave Hilditch.
0
 

Author Comment

by:mikevs
ID: 12127669
DLctblSource is a table name.  I have a variation of the procedure that goes against SYSINDEXES, but i am not guaranteed that the statistics are always up to date with the execution of this procedure.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 12127711
Unfortunately EXEC() will not directly return a query value to the calling code; you have to sp_executeSQL.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question