MySQL存储过程中一直困扰的 の 变量中的@

在声明变量中
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

自信与努力 用心坚持
原文地址:https://www.cnblogs.com/kyxyes/p/3475278.html