city(id,c_name,c_fatherID)是一般的城市列表,现在想通过表量@name得到表中某父子相加的值,如广东广州,语句如下:declare @fatherID int
declare @name varchar(100)
set @fatherID=2
while @fatherID>0
begin
select @fatherID=c_fatherID,@name=@name+c_name from City where id=@fatherID
print 1 ---这里显示循环了两次
end
print @name --------print出来的是空请各位看看是怎么回事?

解决方案 »

  1.   

    declare @fatherID int
    declare @name varchar(100)
    set @name='' --赋初值,不然就是NULL
    set @fatherID=2
    while @fatherID>0
    begin
    select @fatherID=c_fatherID,@name=@name+c_name from City where id=@fatherID
    print 1 ---这里显示循环了两次
    end
    print @name --------print出来的是空
      

  2.   

    同意ls,不然的话null+后边的得出来结果还是null
      

  3.   

    高,但为什么null加字符串依然是null?
      

  4.   

    查了相关联机说明都没有说明,写程序的时候一般都null可以用来直接加的
      

  5.   

    而且为什么如:
    update table set col=col+'测试字符串'这种情况下即使列col是null也可以加入呢?
      

  6.   

    declare @i int,@j int
    set @i=1
    --set @j=2 --这句去了你试试
    print @i+@j
      

  7.   

    初始化@name,否则默认是null
    set @name=''
    避免null出现
    select @fatherID=c_fatherID,@name=@name+isnull(c_name,'') from City where id=@fatherID
      

  8.   

    gahade(与君共勉)是对的,操作2个字符串时,其中一个null,结果肯定是null;
      

  9.   

    oldmoon(电子商务人,电子商务路) ( ) 信誉:100    Blog   加为好友  2007-04-26 09:15:33  得分: 0  
     
     
       而且为什么如:
    update table set col=col+'测试字符串'这种情况下即使列col是null也可以加入呢?
      
    ---------------------------------------------
    楼主是怎么测试的啊???不可能吧!drop table [table]
    go
    create table [table](col varchar(100))
    insert into [table]
    select '111'
    union select NULL
    union select '222'update [table] set col=col+'测试字符串'select * from [table]
    /*
    col                    
    -----------------------
    NULL
    111测试字符串
    222测试字符串(所影响的行数为 3 行)
    */
      

  10.   

    gahade(与君共勉) 
    谢谢,是我测试错了,我想问你,null与任何数据进行逻辑运算结果都是null?
      

  11.   

    null与任何数据进行逻辑运算结果都是null
      

  12.   

    谢谢各位!告别鸣谢gahade(与君共勉) !!!结账!