gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:02:21 26 4
gpt4 key购买 nike

是否有一种方法可以“沉默”存储过程中的选择语句,以便不为该特定语句返回记录集?

据我所知,存储过程中的每个 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

最佳答案

您的意思是不是根据存储过程参数的值有条件地执行该 select 语句?

 Create procedure MyProc
@includeEmployeeData bit = 1
as

Select [Stuff]
From OneTable

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

Select [Stuff]
From SomeOtherTable

关于mysql - 管理 MySQL 存储过程中返回的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24912477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com