關於sql中字串的疑惑

2021-08-29 17:14:14 字數 1427 閱讀 8872

今天早上寫了乙個儲存過程,如下

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 + '/'

endend

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 + '/'

endend

close packway_cursor      

deallocate packway_cursor

select @packmeanid

紅色的為後來加的,這樣就能得到正確的答案的,原因何在呢,難道是定義的@packmeanid,沒有給他賦值,他為null,然後 set @packmeanid =@packmeanid + @packway就一直為null嗎?不明?

關於字串常量的輸出疑惑

我的文章所寫內容,都是自己實際程式設計中遇到的,通常是在遇到這個問題時,在網上搜尋或者自己找到解決辦法。有時候,某些問題要花好久才找的到解決方案,因為網上資料較少,也有可能是我搜尋能力不強。寫下這些,一是希望能幫助到遇到同樣問題的朋友,二是記下之後,再次遇到這些問題,能記得解決辦法。由於平時工作都很...

關於在堆中建立字串物件的疑惑

方法1.直接賦值 nsstring str1 my string 方法2.類函式初始化生成 nsstring str2 nsstring stringwithstring str1 方法3.例項方法初始化生成 nsstring str3 nsstring alloc initwithstring m...

關於在堆中建立字串物件的疑惑

方法1.直接賦值 nsstring str1 my string 方法2.類函式初始化生成 nsstring str2 nsstring stringwithstring str1 方法3.例項方法初始化生成 nsstring str3 nsstring alloc initwithstring m...