declare @id int,@title char(10),@str char(10),@lstr char(100)
declare my_cursor cursor for
  select employeeid,titleofcourtesy
  from employees
Open my_cursor
fetch my_cursor into @id,@title
while @@fetch_status=0
begin
   set @lstr = @lstr+@title 
   print  @lstr
   fetch my_cursor into @id,@title
end
print @title
close my_cursor
deallocate my_cursor为什么打印出来是空白,好像不能循环连接?在线等...

解决方案 »

  1.   

    CHAR无法直接打出来。转成字符串吧。set @lstr = @lstr+@title 
      print  cAST(@lstr AS VARCHAR(10)) 
     
      

  2.   

    fetch my_cursor into @id,@title 
    改为:
    fetch next my_cursor into @id,@title 另外你检查一下数据源是不是空的?
      

  3.   

    declare @id int,@title varchar(10),@str varchar(10),@lstr varchar(100)
    declare my_cursor cursor for
      select employeeid,titleofcourtesy
      from employees
    Open my_cursor
    fetch my_cursor into @id,@title
    while @@fetch_status=0
    begin
       set @lstr =  @title 
       print  @lstr
       fetch my_cursor into @id,@title
    end
    close my_cursor
    deallocate my_cursor这样就有数据
      

  4.   

    DECLARE @lstr VARchar(100) 
    SET @LSTR=''
    还得加上楼上的
      

  5.   


    declare @id int,@title char(10),@str char(10),@lstr char(100) 
    set @lstr=''--要初值
    set @title='';
    set @id=''
    set @str='';
    declare my_cursor cursor for 
      select employeeid,titleofcourtesy 
      from employees 
    Open my_cursor 
    fetch my_cursor into @id,@title 
    while @@fetch_status=0 
    begin 
      set @lstr = @lstr+@title 
      print  @lstr 
      fetch my_cursor into @id,@title 
    end 
    print @title 
    close my_cursor 
    deallocate my_cursor 
      

  6.   

    数据库用的northwind 里面 employees表
      

  7.   


    declare @id int,@title char(10),@str char(10)
    declare @lstr varchar(max) -- 变换类型
    declare my_cursor cursor for 
      select employeeid,titleofcourtesy 
      from employees 
    Open my_cursor 
    fetch my_cursor into @id,@title 
    while @@fetch_status=0 
    begin 
      set @lstr = isnull(@lstr, '') + @title --转化NULL值
      print @lstr
      fetch next from my_cursor into @id,@title 
    end 
    print @title 
    close my_cursor 
    deallocate my_cursor 
      

  8.   

    isnull(@lstr,'')因为null值与任何字符串连接,都是空呀。
      

  9.   

    declare @id int,@title char(10),@str varchar(100),@lstr nvarchar(100) 
    declare my_cursor cursor for 
      select employeeid,titleofcourtesy 
      from employees 
    Open my_cursor 
    fetch my_cursor into @id,@title 
    set @lstr=''
    while @@fetch_status=0 
    begin 
      set @lstr = @lstr+@title 
      print  @lstr 
      fetch my_cursor into @id,@title 
    end 
    print @lstr
    close my_cursor 
    deallocate my_cursor 
    /*
    Ms.       
    Ms.       Dr.       
    Ms.       Dr.       Ms.       
    Ms.       Dr.       Ms.       Mrs.      
    Ms.       Dr.       Ms.       Mrs.      Mr.       
    Ms.       Dr.       Ms.       Mrs.      Mr.       Mr.       
    Ms.       Dr.       Ms.       Mrs.      Mr.       Mr.       Mr.       
    Ms.       Dr.       Ms.       Mrs.      Mr.       Mr.       Mr.       Ms.       
    Ms.       Dr.       Ms.       Mrs.      Mr.       Mr.       Mr.       Ms.       Ms.       
    Ms.       Dr.       Ms.       Mrs.      Mr.       Mr.       Mr.       Ms.       Ms
    */
      

  10.   

    或者可以这样declare @id int,@title char(10),@str varchar(100),@lstr nvarchar(100) 
    declare my_cursor cursor for 
      select employeeid,titleofcourtesy 
      from employees 
    Open my_cursor 
    fetch my_cursor into @id,@title 
    set @lstr=''
    while @@fetch_status=0 
    begin 
      set @lstr = @lstr+@title 
      print  @lstr 
      fetch my_cursor into @id,@title 
    end 
    print @title 
    close my_cursor 
    deallocate my_cursor 
    /*
    Ms.       
    Ms.       Dr.       
    Ms.       Dr.       Ms.       
    Ms.       Dr.       Ms.       Mrs.      
    Ms.       Dr.       Ms.       Mrs.      Mr.       
    Ms.       Dr.       Ms.       Mrs.      Mr.       Mr.       
    Ms.       Dr.       Ms.       Mrs.      Mr.       Mr.       Mr.       
    Ms.       Dr.       Ms.       Mrs.      Mr.       Mr.       Mr.       Ms.       
    Ms.       Dr.       Ms.       Mrs.      Mr.       Mr.       Mr.       Ms.       Ms.       
    Ms.       */