管理MySQL存储过程中返回的内容

时间:2014-07-23 13:57:12

标签: mysql stored-procedures

有没有办法让#沉默"在存储过程中选择语句,以便没有为该特定语句返回记录集?

据我所知,存储过程中的每个select语句都将返回一个记录集。

主要是,我有一些select语句用于初始化可以跳过/静默的变量,以便不返回记录集。

即。

-- select team and set variable
select @team_id := id 
from table ...

编辑#1

基本上,我认为问题在于找到另一种用数据库数据初始化变量的方法。这可能吗?

编辑#2

这是有问题的SP

CREATE DEFINER = 'admin'@'%'
PROCEDURE thebuggenie.cmdb_project_team_init(
  IN project_name VARCHAR(200), 
  IN project_key VARCHAR(200), 
  IN project_homepage VARCHAR(200), 
  IN team_name VARCHAR(200),
  OUT project_id INT(10))
BEGIN
  -- start transaction
  start transaction;

  -- init variables
  set @project_id = 0;
  set @team_id = 0;
  set @assoc_count = 0;
  set @scope_id = 1;

  -- select team and set variable
  select @team_id := id 
    from tbg3_teams 
    where name = team_name;

  -- if team_id = 0, insert team and set variable
  if @team_id is NULL or @team_id = '' or @team_id = 0 then
    -- insert new project
    insert into tbg3_teams(ondemand, name, scope) values(0, team_name, @scope_id);
    -- set team_id variable
    set @team_id = LAST_INSERT_ID();
  end if;

  -- select project and set variable
  select @project_id := id 
    from tbg3_projects 
    where name = project_name;

  -- if project_id = 0, insert project and set variable
  if @project_id is NULL or @project_id = '' or @project_id = 0 then
    -- insert project
    insert into tbg3_projects (name, locked, use_scrum, `key`, homepage, deleted, owner_team, scope, workflow_scheme_id, issuetype_scheme_id) values(project_name, 0, 1, project_key, project_homepage, 0, @team_id, @scope_id, 1, 1); 
    -- set project_id variable
    set @project_id = LAST_INSERT_ID();
  end if;

  select @assoc_count := count(*) 
    from tbg3_projectassignedteams 
    where uid = @team_id 
    and project_id = @project_id;

  if(@assoc_count = 0 and @project_id > 0 and @team_id > 0) then
    insert into tbg3_projectassignedteams (project_id, role_id, uid, scope) values(@project_id, 35, @team_id, @scope_id);
  end if;

  -- setup default views
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (101, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (102, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (110, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (105, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (106, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (111, 0, 0, @project_id, 2, 1);

  commit;

  -- return values
  select @project_id into project_id;
END

2 个答案:

答案 0 :(得分:0)

除了通过有条件地执行基于Stored proc参数值的select语句之外,你的意思是什么?

 Create procedure MyProc
 @includeEmployeeData bit = 1
 as

     Select [Stuff]
     From OneTable

     if @includeEmployeeData = 1
         Select [EmployeeStuff]
         From employees ...

     Select [Stuff]
     From SomeOtherTable

答案 1 :(得分:0)

不可能"沉默"选择存储过程中的语句。每个选择都将返回一个记录集,并且需要使用select来使用数据库中的数据初始化变量。