因为xx表是变化的
所以存储过程中的select 语句只能写成这样
declare @title nvarchar(100)
set @sql='select  @Title =  title from '+@xx表+' where id=1'
exec(@sql)@title 在此存储过程下面还会用到,所以就定义了下。
实操的时候发现这个语句有问题,请问怎么才能将查询出来的title赋值给@title呢?

解决方案 »

  1.   

    declare @title nvarchar(100);declare @cmd nvarchar(4000);
    set @cmd = 'select @title = title from [' + @xx表 + '] where id = 1';exec sp_executesql @cmd ,N'@title nvarchar(100) output',@title output;select @title;
      

  2.   

    看了半天没看出来...不会是长度越界了吧 nvarchar(100)?
      

  3.   

    declare @title nvarchar(100) 
    set @sql='select '+ @title +'=title from '+ @xx表 +' where id=1'
    exec(@sql) 
      

  4.   

    declare @title nvarchar(100) 
    select @title=title from 表名 where id=1
    set方法是要求直接给一个值,不能接查询语句,上边才是正确的查询赋值
      

  5.   

    declare @title nvarchar(100);declare @cmd nvarchar(4000);
    set @cmd = 'select @title = title from [' + @xx表 + '] where id = 1';exec sp_executesql @cmd ,N'@title nvarchar(100) output',@title output;select @title;