﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / T-SQL  / execute sp_executesql COUNT(*) Output / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 16:44:26 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: execute sp_executesql COUNT(*) Output</title><link>http://www.sqlservercentral.com/Forums/Topic817615-8-1.aspx</link><description>[quote][b]bjvaishnani (11/11/2009)[/b][hr]DECLARE @SelectQuery	NVARCHAR(MAX)DECLARE @WhereQuery		NVARCHAR(MAX)SET @WhereQuery = ' WHERE COND1 and cond2'SET @SelectQuery = ' SELECT Col1, col2,, From Table1 ' + @WhereQuerySET @CountSQLQuery =  N'SELECT COUNT(*) 						FROM							dbo.Table1 ' + @WhereQuerySET @TempPara = '@recordcount INT OUTPUT 'execute sp_executesql 				@SQLQuery,				@TempPara,				@recordcount = @CountSQLQuery----------------------------------------------------As i am geting the err.---&amp;gt; Error converting data type nvarchar(max) to int.When i try to run the sp...[/quote][code]SET @SQLQuery = ' SELECT Col1, col2,, From Table1 ' + @WhereQuery + ' SELECT @RCount = @@ROWCOUNT '.....execute sp_executesql 				@SQLQuery,				@TempPara,				@recordcount = @RCount OUTPUT[/code]</description><pubDate>Mon, 16 Nov 2009 16:25:11 GMT</pubDate><dc:creator>Sergiy</dc:creator></item><item><title>RE: execute sp_executesql COUNT(*) Output</title><link>http://www.sqlservercentral.com/Forums/Topic817615-8-1.aspx</link><description>[quote][b]DougGifford (11/13/2009)[/b][hr][quote][b]Markus Jägerskogh (11/12/2009)[/b][hr]If you need to do it by dynamic code you are boulding the parameters for [i]sp_executesql[/i] incorrectly.Short example:[code="sql"]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);[/code]Good luck!/Markus[/quote]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 ?[/quote]Sorry... found my answer at:http://databases.aspfaq.com/general/why-do-some-sql-strings-have-an-n-prefix.htmlHas to do with Unicode strings and some stored proceedures like sq_executeSQL.</description><pubDate>Fri, 13 Nov 2009 10:21:31 GMT</pubDate><dc:creator>DougGifford</dc:creator></item><item><title>RE: execute sp_executesql COUNT(*) Output</title><link>http://www.sqlservercentral.com/Forums/Topic817615-8-1.aspx</link><description>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'</description><pubDate>Fri, 13 Nov 2009 10:21:29 GMT</pubDate><dc:creator>  Jack Corbett</dc:creator></item><item><title>RE: execute sp_executesql COUNT(*) Output</title><link>http://www.sqlservercentral.com/Forums/Topic817615-8-1.aspx</link><description>[quote][b]Markus Jägerskogh (11/12/2009)[/b][hr]If you need to do it by dynamic code you are boulding the parameters for [i]sp_executesql[/i] incorrectly.Short example:[code="sql"]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);[/code]Good luck!/Markus[/quote]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 ?</description><pubDate>Fri, 13 Nov 2009 10:10:56 GMT</pubDate><dc:creator>DougGifford</dc:creator></item><item><title>RE: execute sp_executesql COUNT(*) Output</title><link>http://www.sqlservercentral.com/Forums/Topic817615-8-1.aspx</link><description>If you need to do it by dynamic code you are boulding the parameters for [i]sp_executesql[/i] incorrectly.Short example:[code="sql"]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);[/code]Good luck!/Markus</description><pubDate>Thu, 12 Nov 2009 07:17:07 GMT</pubDate><dc:creator>Hunterwood</dc:creator></item><item><title>RE: execute sp_executesql COUNT(*) Output</title><link>http://www.sqlservercentral.com/Forums/Topic817615-8-1.aspx</link><description>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?</description><pubDate>Thu, 12 Nov 2009 06:43:23 GMT</pubDate><dc:creator>  Jack Corbett</dc:creator></item><item><title>execute sp_executesql COUNT(*) Output</title><link>http://www.sqlservercentral.com/Forums/Topic817615-8-1.aspx</link><description>DECLARE @SelectQuery	NVARCHAR(MAX)DECLARE @WhereQuery		NVARCHAR(MAX)SET @WhereQuery = ' WHERE COND1 and cond2'SET @SelectQuery = ' SELECT Col1, col2,, From Table1 ' + @WhereQuerySET @CountSQLQuery =  N'SELECT COUNT(*) 						FROM							dbo.Table1 ' + @WhereQuerySET @TempPara = '@recordcount INT OUTPUT 'execute sp_executesql 				@SQLQuery,				@TempPara,				@recordcount = @CountSQLQuery----------------------------------------------------As i am geting the err.---&amp;gt; Error converting data type nvarchar(max) to int.When i try to run the sp...</description><pubDate>Wed, 11 Nov 2009 23:11:44 GMT</pubDate><dc:creator>bjvaishnani</dc:creator></item></channel></rss>