解决了它我就觉得你是高手

Eric1006 2003-08-25 10:43:56
declare @firstfield varchar(20)
declare @first_pkid numeric(2)
declare @secondfield varchar(20)
declare @second_pkid numeric(2)
declare @totalcount int
/*以下几个变量可以在程序中定义,这里是暂时为它定义量值*/
select @firstfield='zn'
select @first_pkid='3'
select @secondfield='indeg'
select @second_pkid='21'
/*目的是满足@firstfield=convert(nvarchar,@first_pkid)的记录数目@totalcount,
不用exec sp_executesql @str的方法因为还要对记录数@totalcount进行操作*/
select @totalcount=count(distinct pkid) from table_yeikue where @firstfield=convert(nvarchar,@first_pkid) --这里不能通过
select @totalcount

各位,我该怎么改,或者有什么变通的方法?
...全文
60 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
CrazyFor 2003-08-25
  • 打赏
  • 举报
回复
参考:

declare @tcnt int
declare @paras varchar(100)
DECLARE @SQLString NVARCHAR(500)
set @paras ='1,3,5'
set @sqlstring=N'select @cnt=count(*) from sysobjects where id in ('+@paras+')'

set @tcnt=0
execute sp_executesql
@sqlstring,
N'@cnt int output',
@cnt=@tcnt output
select @tcnt


select * from sysobjects



Create Procedure GetMaxID
@TableName varchar(100), @ID int output
as
begin
declare @sql nvarchar(1000)
set @sql='select @ID = count(ID) from '+@TableName
exec sp_executesql @sql,N'@id int output',@id output
end
txlicenhe 2003-08-25
  • 打赏
  • 举报
回复
declare @firstfield varchar(20)
declare @first_pkid numeric(2)
declare @secondfield varchar(20)
declare @second_pkid numeric(2)

/*以下几个变量可以在程序中定义,这里是暂时为它定义量值*/
select @firstfield='zn'
select @first_pkid='3'
select @secondfield='indeg'
select @second_pkid='21'


declare @sql varchar(4000)
set @sql=' declare @totalcount int select @totalcount=count(distinct pkid) from table_yeikue where '+@firstfield+'='+cast(@first_pkid as nvarchar(10))
+ ' select @totalcount ' ---这里存在未定义变量问题
exec(@sql)
pengdali 2003-08-25
  • 打赏
  • 举报
回复
declare @firstfield varchar(20)
declare @first_pkid numeric(2)
declare @secondfield varchar(20)
declare @second_pkid numeric(2)
declare @totalcount int

select @firstfield='zn'
select @first_pkid='3'
select @secondfield='indeg'
select @second_pkid='21'


declare @sql nvarchar(4000)
set @sql=N'select @totalcount=count(distinct pkid) from table_yeikue where '+@firstfield+'='+cast(@first_pkid as nvarchar(10))
exec sp_executesql @sql,N'@totalcount int output',@totalcount output


select @totalcount
Eric1006 2003-08-25
  • 打赏
  • 举报
回复
txlicenhe(不做技术高手):
declare @sql varchar(4000)
set @sql='select @totalcount=count(distinct pkid) from table_yeikue where '+@firstfield+'='+cast(@first_pkid as nvarchar(10))
+ ' select @totalcount ' ---这里存在未定义变量问题
exec(@sql)
Eric1006 2003-08-25
  • 打赏
  • 举报
回复
pengdali(大力 V2.0)跟 nboys()的方法都存在:
过程需要参数 '@statement' 为 'ntext/nchar/nvarchar' 类型
nboys 2003-08-25
  • 打赏
  • 举报
回复
declare @firstfield varchar(20)
declare @sql varchar(8000)
declare @first_pkid numeric(2)
declare @secondfield varchar(20)
declare @second_pkid numeric(2)
declare @totalcount int
select @firstfield='zn'
select @first_pkid='3'
select @secondfield='indeg'
select @second_pkid='21'

set @sql=N'select @totalcount=count(distinct pkid) from table_yeikue where '+@firstfield+'='+cast(@first_pkid as nvarchar(10))+''
exec sp_executesql @sql,N'@totalcount int output',@totalcount output

print @totalcount
txlicenhe 2003-08-25
  • 打赏
  • 举报
回复
...
declare @sql varchar(4000)
set @sql='select @totalcount=count(distinct pkid) from table_yeikue where '+@firstfield+'='+cast(@first_pkid as nvarchar(10))
+ ' select @totalcount '
exec(@sql)

我不是高手,也不想成为高手,只想学习。
nboys 2003-08-25
  • 打赏
  • 举报
回复
不能用select直接返回值
select @totalcount=count(distinct pkid) from table_yeikue where @firstfield=convert(nvarchar,@first_pkid) --这里不能通过
select @totalcount

用sp_executesql

pengdali 2003-08-25
  • 打赏
  • 举报
回复
declare @firstfield varchar(20)
declare @first_pkid numeric(2)
declare @secondfield varchar(20)
declare @second_pkid numeric(2)
declare @totalcount int

select @firstfield='zn'
select @first_pkid='3'
select @secondfield='indeg'
select @second_pkid='21'


declare @sql varchar(4000)
set @sql=N'select @totalcount=count(distinct pkid) from table_yeikue where '+@firstfield+'='+cast(@first_pkid as nvarchar(10))
exec sp_executesql @sql,N'@totalcount int output',@totalcount output


select @totalcount
Eric1006 2003-08-25
  • 打赏
  • 举报
回复
多谢各位高手!!!

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧