BEGIN
DROP TEMPORARY TABLE IF EXISTS T;
DROP TEMPORARY TABLE IF EXISTS Temp;
CREATE TEMPORARY TABLE Temp(UserID varchar(20));
CREATE TEMPORARY TABLE T(id int,deptid varchar(20));
SET @str=str; /*要分割的字符串*/
SET @split=split;/*分割标志*/
SET @num = 0;
SET @i=LOCATE(@split,@str);
WHILE(@i>0) DO
SET @substr=substring(@str,1,@i-1);
INSERT INTO T VALUES(@num,@substr);
SET @str = INSERT(@str,1,@i,'');
SET @i=LOCATE(@split,@str);
SET @num=@num+1;
END WHILE;
INSERT INTO T VALUES(@num,@str);
SET @count = 0;
WHILE(@count <= @num) DO
SELECT @DeptID:=deptid FROM T WHERE id=@count; IF(LEFT(@DeptID,3)='SMG') THEN /*从自定义用户组中取用户*/
SELECT @UserID:=USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*通过用户组ID查找用户ID*/
IF (@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*用户组下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*将该ID作为用户ID*/
END IF;
ELSE
SELECT @UserID:=USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*通过部门ID查找用户ID*/
IF(@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*部门下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*该ID作为用户ID */
END IF;
END IF;
SET @UserID = NULL; /*清空变量必须*/
SET @count=@count+1;
END WHILE;
SELECT DISTINCT UserID from Temp; DROP TEMPORARY TABLE T;
DROP TEMPORARY TABLE Temp;
END
5 个解决方案
#1
个别地方颜色标红了,但是没变,重新贴一下
BEGIN
DROP TEMPORARY TABLE IF EXISTS T;
DROP TEMPORARY TABLE IF EXISTS Temp;
CREATE TEMPORARY TABLE Temp(UserID varchar(20));
CREATE TEMPORARY TABLE T(id int,deptid varchar(20));
SET @str=str; /*要分割的字符串*/
SET @split=split;/*分割标志*/
SET @num = 0;
SET @i=LOCATE(@split,@str);
WHILE(@i>0) DO
SET @substr=substring(@str,1,@i-1);
INSERT INTO T VALUES(@num,@substr);
SET @str = INSERT(@str,1,@i,'');
SET @i=LOCATE(@split,@str);
SET @num=@num+1;
END WHILE;
INSERT INTO T VALUES(@num,@str);
SET @count = 0;
WHILE(@count <= @num) DO
SELECT @DeptID:=deptid FROM T WHERE id=@count; IF(LEFT(@DeptID,3)='SMG') THEN /*从自定义用户组中取用户*/
SELECT @UserID:=USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*通过用户组ID查找用户ID*/
IF (@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*用户组下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*将该ID作为用户ID*/
END IF;
ELSE
SELECT @UserID:=USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*通过部门ID查找用户ID*/
IF(@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*部门下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*该ID作为用户ID */
END IF;
END IF;
SET @UserID = NULL; /*清空变量必须*/
SET @count=@count+1;
END WHILE;
SELECT DISTINCT UserID from Temp; DROP TEMPORARY TABLE T;
DROP TEMPORARY TABLE Temp;
END
BEGIN
DROP TEMPORARY TABLE IF EXISTS T;
DROP TEMPORARY TABLE IF EXISTS Temp;
CREATE TEMPORARY TABLE Temp(UserID varchar(20));
CREATE TEMPORARY TABLE T(id int,deptid varchar(20));
SET @str=str; /*要分割的字符串*/
SET @split=split;/*分割标志*/
SET @num = 0;
SET @i=LOCATE(@split,@str);
WHILE(@i>0) DO
SET @substr=substring(@str,1,@i-1);
INSERT INTO T VALUES(@num,@substr);
SET @str = INSERT(@str,1,@i,'');
SET @i=LOCATE(@split,@str);
SET @num=@num+1;
END WHILE;
INSERT INTO T VALUES(@num,@str);
SET @count = 0;
WHILE(@count <= @num) DO
SELECT @DeptID:=deptid FROM T WHERE id=@count; IF(LEFT(@DeptID,3)='SMG') THEN /*从自定义用户组中取用户*/
SELECT @UserID:=USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*通过用户组ID查找用户ID*/
IF (@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*用户组下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*将该ID作为用户ID*/
END IF;
ELSE
SELECT @UserID:=USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*通过部门ID查找用户ID*/
IF(@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*部门下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*该ID作为用户ID */
END IF;
END IF;
SET @UserID = NULL; /*清空变量必须*/
SET @count=@count+1;
END WHILE;
SELECT DISTINCT UserID from Temp; DROP TEMPORARY TABLE T;
DROP TEMPORARY TABLE Temp;
END
#2
select @xx:=xx,xx from table
#3
插入表中,从表中取数
#4
找到解决方法了,select赋值应该这样select xx into @xx from table,这样的select赋值语句就不会产生结果集了。
#5
楼主,我试了下,不行啊,只有当select 返回的结果集只有一行数据才能这样,如果有多行就不行了。
#1
个别地方颜色标红了,但是没变,重新贴一下
BEGIN
DROP TEMPORARY TABLE IF EXISTS T;
DROP TEMPORARY TABLE IF EXISTS Temp;
CREATE TEMPORARY TABLE Temp(UserID varchar(20));
CREATE TEMPORARY TABLE T(id int,deptid varchar(20));
SET @str=str; /*要分割的字符串*/
SET @split=split;/*分割标志*/
SET @num = 0;
SET @i=LOCATE(@split,@str);
WHILE(@i>0) DO
SET @substr=substring(@str,1,@i-1);
INSERT INTO T VALUES(@num,@substr);
SET @str = INSERT(@str,1,@i,'');
SET @i=LOCATE(@split,@str);
SET @num=@num+1;
END WHILE;
INSERT INTO T VALUES(@num,@str);
SET @count = 0;
WHILE(@count <= @num) DO
SELECT @DeptID:=deptid FROM T WHERE id=@count; IF(LEFT(@DeptID,3)='SMG') THEN /*从自定义用户组中取用户*/
SELECT @UserID:=USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*通过用户组ID查找用户ID*/
IF (@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*用户组下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*将该ID作为用户ID*/
END IF;
ELSE
SELECT @UserID:=USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*通过部门ID查找用户ID*/
IF(@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*部门下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*该ID作为用户ID */
END IF;
END IF;
SET @UserID = NULL; /*清空变量必须*/
SET @count=@count+1;
END WHILE;
SELECT DISTINCT UserID from Temp; DROP TEMPORARY TABLE T;
DROP TEMPORARY TABLE Temp;
END
BEGIN
DROP TEMPORARY TABLE IF EXISTS T;
DROP TEMPORARY TABLE IF EXISTS Temp;
CREATE TEMPORARY TABLE Temp(UserID varchar(20));
CREATE TEMPORARY TABLE T(id int,deptid varchar(20));
SET @str=str; /*要分割的字符串*/
SET @split=split;/*分割标志*/
SET @num = 0;
SET @i=LOCATE(@split,@str);
WHILE(@i>0) DO
SET @substr=substring(@str,1,@i-1);
INSERT INTO T VALUES(@num,@substr);
SET @str = INSERT(@str,1,@i,'');
SET @i=LOCATE(@split,@str);
SET @num=@num+1;
END WHILE;
INSERT INTO T VALUES(@num,@str);
SET @count = 0;
WHILE(@count <= @num) DO
SELECT @DeptID:=deptid FROM T WHERE id=@count; IF(LEFT(@DeptID,3)='SMG') THEN /*从自定义用户组中取用户*/
SELECT @UserID:=USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*通过用户组ID查找用户ID*/
IF (@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SF_MessageGroupUser WHERE SMG_ID = @DeptID; /*用户组下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*将该ID作为用户ID*/
END IF;
ELSE
SELECT @UserID:=USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*通过部门ID查找用户ID*/
IF(@UserID IS NOT NULL) THEN
INSERT INTO Temp SELECT USER_ID FROM SY_User WHERE DEPT_ID = @DeptID; /*部门下有用户并且可能有多个用户*/
ELSE
INSERT INTO Temp values(@DeptID); /*该ID作为用户ID */
END IF;
END IF;
SET @UserID = NULL; /*清空变量必须*/
SET @count=@count+1;
END WHILE;
SELECT DISTINCT UserID from Temp; DROP TEMPORARY TABLE T;
DROP TEMPORARY TABLE Temp;
END
#2
select @xx:=xx,xx from table
#3
插入表中,从表中取数
#4
找到解决方法了,select赋值应该这样select xx into @xx from table,这样的select赋值语句就不会产生结果集了。
#5
楼主,我试了下,不行啊,只有当select 返回的结果集只有一行数据才能这样,如果有多行就不行了。