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

MySQL存储过程中一直困扰的 の 变量中的@的更多相关文章

  1. Mysql存储过程查询结果赋值到变量的方法

    Mysql存储过程查询结果赋值到变量的方法   把查询结果赋值到变量,大部分情况下使用游标来完成,但是如果明确知道查询结果只有一行(例如统计记录的数量,某个字段求和等),其实可以使用set或into的 ...

  2. MySql存储过程批量删除多个数据库中同名表中的指定字段

    1. 创建存储过程batchDeleteField:删除所有名称为"MyDB_"开头的数据库中的指定字段 -- ---------------------------- -- Pr ...

  3. MySql存储过程批量给多个数据库中的同名表添加字段

    1 创建存储过程 batchAddField:给所有"MyDB_"开头的数据库添加新字段 -- ---------------------------- -- Procedure ...

  4. ubuntu将mysql、nginx添加到环境变量中

    vim /etc/profile 添加 export PATH="$PATH:/usr/local/mysql/bin" export PATH="$PATH:/usr/ ...

  5. html文件中jquery与velocity变量中的$冲突的解决方法

    1.使用jQuery代替$. 如:jQuery.ajax(); 缺点:不适合扩展,一旦替换成第三方库时,那就麻烦大发 2.使用jQuery.noConflict. 如:var j = jQuery.n ...

  6. Mysql存储过程查询结果赋值到变量

    # 使用的navicat  编辑的存储过程 CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_student_back`()BEGIN#定义max ...

  7. Mysql 存储过程查询结果赋值到变量的方法

    drop table if exists test_tbl; create table test_tbl (name varchar(20), status int(2)); insert into ...

  8. python raw String 获取字符串变量中的反斜杠

    常用的获取raw string的方式为: >>>r'\n' \n 不能用在字符串变量中,获取字符串变量中的反斜杠如下: tab = '\n' >>>tab.enco ...

  9. MySQL 存储过程参数

    MySQL  存储过程参数 MySQL存储过程参数简介 在现实应用中,开发的存储过程几乎都需要参数.这些参数使存储过程更加灵活和有用. 在MySQL中,参数有三种模式:IN,OUT或INOUT. IN ...

随机推荐

  1. codeforces 629C Famil Door and Brackets (dp + 枚举)

    题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...

  2. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E

    Description Bear Limak prepares problems for a programming competition. Of course, it would be unpro ...

  3. 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift

    题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...

  4. 1-20StringBuffer简介

    StringBuffer是一个字符串缓冲区,如果需要频繁的对字符串进行拼接时,建议使用StringBuffer. 工作原理 StringBuffer的底层是char数组,如果没有明确设定,则系统会默认 ...

  5. PHP连接数据操作步骤

    数据库的操作步骤: 端口号:0到65535 3306:mysql数据库的默认端口号(可修改) mysql_connect(“本机地址”,“用户名”,“密码”,); new_link:如果用同样的参数第 ...

  6. android开发学习 ------- 【转】Genymotion 小白安装

    参考 https://www.cnblogs.com/whycxb/p/6850454.html 很详细,全面

  7. postgresql版sde(10.4.1)新建数据库

    使用sde pg版,第一次建sde库就是新安装sde的方法 然而从第二次开始可以有其他方式 新建一般的pg数据再转为sde数据库: 首先用建一般的pg数据库 首先要新建数据库 数据库名:新建的数据库名 ...

  8. CocoaPods安装遇到的坑。

    //官方推荐地址 CocoaPods :http://code4app.com/article/cocoapods-install-usage cooped的安装  $(inherited) 报pod ...

  9. .htaccess重写规则失败

    开启mod_rewrite.so LoadModule rewrite_module libexec/apache2/mod_rewrite.so 重启服务 sudo apachectl restar ...

  10. Visual studio每次build自动增加版本号

    关键词:visual studio,rc file,VS_VERSION_INFO,FILEVERSION,PRODUCTVERSION 目标:希望每次在vs中编译项目时,生成的可执行程序版本号自动+ ...