关于sql中字符串的疑惑
今天早上写了一个存储过程,如下
DECLARE packway_cursor CURSOR FOR
select Distinct by00902 from sdby009 where by00901 in(
select by02108 from sdby021 where by02118=@by02118)
OPEN packway_cursor
FETCH NEXT FROM packway_cursor
INTO @PackWay
while(@@fetch_status=0)
begin
set @PackMeanId =@PackMeanId + @PackWay
fetch next from packway_cursor into @PackWay
if(@@fetch_status=0)
begin
set @PackMeanId = @PackMeanId + '/'
end
end
close packway_cursor
deallocate packway_cursor
select @PackMeanId
问题是无论有没有记录存在@PackMeanId 都为null,不知道原因是什么,而我将他改为如下
DECLARE packway_cursor CURSOR FOR
select Distinct by00902 from sdby009 where by00901 in(
select by02108 from sdby021 where by02118=@by02118)
OPEN packway_cursor
FETCH NEXT FROM packway_cursor
INTO @PackWay
set @PackMeanId = ''
while(@@fetch_status=0)
begin
set @PackMeanId =@PackMeanId + @PackWay
fetch next from packway_cursor into @PackWay
if(@@fetch_status=0)
begin
set @PackMeanId = @PackMeanId + '/'
end
end
close packway_cursor
deallocate packway_cursor
select @PackMeanId
红色的为后来加的,这样就能得到正确的答案的,原因何在呢,难道是定义的@PackMeanId,没有给他赋值,他为null,然后 set @PackMeanId =@PackMeanId + @PackWay就一直为null吗?不明?