Group By 子句无法与 sp_executesql 一起正常工作
Group By clause not working correctly with sp_executesql
sp_executesql
无法与 group by
子句一起正常工作,我正在调试它。
我从 sp_executesql
中删除了所有参数,得到了这个简单的批处理:
declare @SQLStatement nvarchar(1000);
set @SQLStatement='select '+cast(count('phonenumbertypeid') as nvarchar(100))+ ' from person.personphone
group by phonenumbertypeid'
exec sp_executesql @SQLStatement
现在,这给了我 1
的结果,这肯定是错误的。
我 运行 没有 sp_executesql
的相同语句,它给了我 736
的正确结果,这是正确的:
select cast(COUNT(PhoneNumberTypeID) as nvarchar(100)) from person.PersonPhone
group by PhoneNumberTypeID
我坚信问题是由我在 @SQLStatement
中执行的演员表引起的:
set @SQLStatement='select '+cast(count('phonenumbertypeid') as nvarchar(100))+ ' from person.personphone
如何解决 sp_executesql
语句中 group by
子句的问题?
如果你执行这个你会看到问题:
declare @SQLStatement nvarchar(1000);
set @SQLStatement='select '+cast(count('phonenumbertypeid') as nvarchar(100))+ ' from person.personphone
group by phonenumbertypeid'
select @SQLStatement
您正在执行此操作:
select 1 from person.personphone group by phonenumbertypeid
试试这个(未测试):
declare @SQLStatement nvarchar(1000);
set @SQLStatement='select cast(count(''phonenumbertypeid'') as nvarchar(100)) from person.personphone
group by phonenumbertypeid'
exec sp_executesql @SQLStatement
sp_executesql
无法与 group by
子句一起正常工作,我正在调试它。
我从 sp_executesql
中删除了所有参数,得到了这个简单的批处理:
declare @SQLStatement nvarchar(1000);
set @SQLStatement='select '+cast(count('phonenumbertypeid') as nvarchar(100))+ ' from person.personphone
group by phonenumbertypeid'
exec sp_executesql @SQLStatement
现在,这给了我 1
的结果,这肯定是错误的。
我 运行 没有 sp_executesql
的相同语句,它给了我 736
的正确结果,这是正确的:
select cast(COUNT(PhoneNumberTypeID) as nvarchar(100)) from person.PersonPhone
group by PhoneNumberTypeID
我坚信问题是由我在 @SQLStatement
中执行的演员表引起的:
set @SQLStatement='select '+cast(count('phonenumbertypeid') as nvarchar(100))+ ' from person.personphone
如何解决 sp_executesql
语句中 group by
子句的问题?
如果你执行这个你会看到问题:
declare @SQLStatement nvarchar(1000);
set @SQLStatement='select '+cast(count('phonenumbertypeid') as nvarchar(100))+ ' from person.personphone
group by phonenumbertypeid'
select @SQLStatement
您正在执行此操作:
select 1 from person.personphone group by phonenumbertypeid
试试这个(未测试):
declare @SQLStatement nvarchar(1000);
set @SQLStatement='select cast(count(''phonenumbertypeid'') as nvarchar(100)) from person.personphone
group by phonenumbertypeid'
exec sp_executesql @SQLStatement