SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


execute sp_executesql COUNT(*) Output


execute sp_executesql COUNT(*) Output

Author
Message
bjvaishnani
bjvaishnani
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 2
DECLARE @SelectQuery NVARCHAR(MAX)
DECLARE @WhereQuery NVARCHAR(MAX)

SET @WhereQuery = ' WHERE COND1 and cond2'


SET @SelectQuery = ' SELECT Col1, col2,, From Table1 ' + @WhereQuery


SET @CountSQLQuery = N'SELECT COUNT(*)
FROM
dbo.Table1 ' + @WhereQuery

SET @TempPara = '@recordcount INT OUTPUT '

execute sp_executesql
@SQLQuery,
@TempPara,
@recordcount = @CountSQLQuery

----------------------------------------------------

As i am geting the err.---> Error converting data type nvarchar(max) to int.

When i try to run the sp...
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18302 Visits: 14889
I think you need your @COuntSQLQUery to be this: "Select @recordcount = count(*) from dbo.table1"

The question now becomes, why are you using dynamic sql for this?



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Hunterwood
Hunterwood
Right there with Babe
Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)

Group: General Forum Members
Points: 722 Visits: 297
If you need to do it by dynamic code you are boulding the parameters for sp_executesql incorrectly.

Short example:

DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
DECLARE @CountSQLQuery varchar(30);

SET @SQLString = N'SELECT @result = COUNT(*) FROM INFORMATION_SCHEMA.TABLES';
SET @ParmDefinition = N'@result varchar(30) OUTPUT';

EXECUTE sp_executesql @SQLString, @ParmDefinition, @result=@CountSQLQuery OUTPUT;

SELECT CAST(@CountSQLQuery as int);



Good luck!

/Markus
DougGifford
DougGifford
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 860
Markus Jägerskogh (11/12/2009)
If you need to do it by dynamic code you are boulding the parameters for sp_executesql incorrectly.

Short example:

DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
DECLARE @CountSQLQuery varchar(30);

SET @SQLString = N'SELECT @result = COUNT(*) FROM INFORMATION_SCHEMA.TABLES';
SET @ParmDefinition = N'@result varchar(30) OUTPUT';

EXECUTE sp_executesql @SQLString, @ParmDefinition, @result=@CountSQLQuery OUTPUT;

SELECT CAST(@CountSQLQuery as int);



Good luck!

/Markus


Question: Why
"N'SELECT @result = COUNT(*) FROM INFORMATION_SCHEMA.TABLES' "
and not
" SELECT @result = COUNT(*) FROM INFORMATION_SCHEMA.TABLES' "

What is the N' that preceeds the SELECT ?

"When in danger or in doubt. Run in circles, scream and shout!" TANSTAAFL
"Robert A. Heinlein"
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18302 Visits: 14889
The N signifies unicode and keeps a conversion from happening. sp_executesql expects nvarchar (Unicode) parameters so when assigning values to them you should use N'string'



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
DougGifford
DougGifford
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 860
DougGifford (11/13/2009)
Markus Jägerskogh (11/12/2009)
If you need to do it by dynamic code you are boulding the parameters for sp_executesql incorrectly.

Short example:

DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
DECLARE @CountSQLQuery varchar(30);

SET @SQLString = N'SELECT @result = COUNT(*) FROM INFORMATION_SCHEMA.TABLES';
SET @ParmDefinition = N'@result varchar(30) OUTPUT';

EXECUTE sp_executesql @SQLString, @ParmDefinition, @result=@CountSQLQuery OUTPUT;

SELECT CAST(@CountSQLQuery as int);



Good luck!

/Markus


Question: Why
"N'SELECT @result = COUNT(*) FROM INFORMATION_SCHEMA.TABLES' "
and not
" SELECT @result = COUNT(*) FROM INFORMATION_SCHEMA.TABLES' "

What is the N' that preceeds the SELECT ?


Sorry... found my answer at:
http://databases.aspfaq.com/general/why-do-some-sql-strings-have-an-n-prefix.html

Has to do with Unicode strings and some stored proceedures like sq_executeSQL.

"When in danger or in doubt. Run in circles, scream and shout!" TANSTAAFL
"Robert A. Heinlein"
Sergiy
Sergiy
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10224 Visits: 11958
bjvaishnani (11/11/2009)

DECLARE @SelectQuery NVARCHAR(MAX)
DECLARE @WhereQuery NVARCHAR(MAX)

SET @WhereQuery = ' WHERE COND1 and cond2'


SET @SelectQuery = ' SELECT Col1, col2,, From Table1 ' + @WhereQuery


SET @CountSQLQuery = N'SELECT COUNT(*)
FROM
dbo.Table1 ' + @WhereQuery

SET @TempPara = '@recordcount INT OUTPUT '

execute sp_executesql
@SQLQuery,
@TempPara,
@recordcount = @CountSQLQuery

----------------------------------------------------

As i am geting the err.---> Error converting data type nvarchar(max) to int.

When i try to run the sp...



SET @SQLQuery = ' SELECT Col1, col2,, From Table1 ' + @WhereQuery + '
SELECT @RCount = @@ROWCOUNT '
.....
execute sp_executesql
@SQLQuery,
@TempPara,
@recordcount = @RCount OUTPUT


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search