declare  @firstfield  varchar(20)
declare  @first_pkid  numeric(2)
declare  @secondfield  varchar(20)
declare  @second_pkid  numeric(2)
declare  @totalcount  intselect  @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

解决方案 »

  1.   

    不能用select直接返回值
    select @totalcount=count(distinct pkid) from table_yeikue where @firstfield=convert(nvarchar,@first_pkid) --这里不能通过
    select @totalcount用sp_executesql
      

  2.   

    ...
    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)我不是高手,也不想成为高手,只想学习。
      

  3.   

    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 outputprint @totalcount
      

  4.   

    pengdali(大力 V2.0)跟 nboys()的方法都存在:
     过程需要参数 '@statement' 为 'ntext/nchar/nvarchar' 类型
      

  5.   

    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)
      

  6.   

    declare  @firstfield  varchar(20)
    declare  @first_pkid  numeric(2)
    declare  @secondfield  varchar(20)
    declare  @second_pkid  numeric(2)
    declare  @totalcount  intselect  @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
      

  7.   

    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)
      

  8.   

    参考: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 sysobjectsCreate 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