DECLARE m_info CURSOR FOR
SELECT @Colname FROM database_table WHERE id=@id
OPEN m_info
fetch next FROM m_info into @Value
CLOSE m_info
DEALLOCATE m_info还有在游标中也是一样

解决方案 »

  1.   

    use database
    declare @col nchar(255)
    set @col='户名'
    exec('select '+@col+' from database_table')
      

  2.   

    declare @s  varchar(8000)
    set @s='DECLARE m_info CURSOR FOR
    SELECT '+@Colname+' FROM database_table WHERE id='''+cast(@id as varchar)+''''
    exec(@s)
    OPEN m_info
    fetch next FROM m_info into @Value
    CLOSE m_info
    DEALLOCATE m_info
      

  3.   

    use database
    declare @col nchar(255)
    set @col='户名'
    exec('select '+@col+' from database_table')
      

  4.   


    select @col from database_table
    其实就相当于select '户名' from database_table
    查询后的结果当然全是户名其实你要的是:select 户名 from database_tableset @str='select' +@col +' from database_table'
    print @str  
    exec(@str)
      

  5.   

    use database
    declare @col nchar(255)
    set @col='户名'
    select @col=戶名字段 from database_table