在聲明變量中
CREATE function Get_StrArrayLength
(
@str varchar(1024), --要分割的字符串
@split varchar(10) --分隔符號
)
returns int
as
begin
declare @location int
declare @start int
declare @length int
set @str=ltrim(rtrim(@str))
set @location=charindex(@split,@str)
set @length=1
while @location<>0
begin
set @start=@location+1
set @location=charindex(@split,@str,@start)
set @length=@length+1
end
return @length
end
/***********為什么下面這種可以不用 decalre*************************/
CREATE DEFINER=`root`@`%` PROCEDURE `test`()
BEGIN
#目標字符串
set @a = ’1,2,3,4,5,6,12‘;
# 分隔符
set @c = ',';
# 存儲風格后的字符串
set @b = '';
REPEAT
# 調用上面的存儲過程
CALL SPLIT_SUB_STR0(@a, ',', @c);
#將取得的字符串拼接,測試用
set @b = concat(@b, @c);
#當目標字符串為空時,停止循環
UNTIL @a = ''
END REPEAT;
# 查看結果
select @a, @c, @b;
END;
/******************************為什么還有的沒有@****************************/
BEGIN
-- Get the separated string.
declare cnt int default 0;
declare i int default 0;
set cnt = func_get_split_string_total(f_string,f_delimiter);
drop table if exists tmp_print;
create temporary table tmp_print (num int not null);
while i < cnt
do
set i = i + 1;
insert into tmp_print(num) values (func_get_split_string(f_string,f_delimiter,i));
end while;
select * from tmp_print;
END
求教這三種區別
/**************************************答案***********************************/
變量的作用范圍同編程里面類似,在這里一般是在對應的begin和end之間。在end之后這個變量就沒有作用了,不能使用了。這個同編程一樣。
另外有種變量叫做會話變量(session variable),也叫做用戶定義的變量(user defined variable)。這種變量要在變量名稱前面加上“@”符號,叫做會話變量,代表整個會話過程他都是有作用的,這個有點類似於全局變量一樣。這種變量用途比較廣,因為只要在一個會話內(就是某個應用的一個連接過程中),這個變量可以在被調用的存儲過程或者代碼之間共享數據。
詳見:http://blog.csdn.net/rdarda/article/details/7878836