Quy trình sau đây cho tôi một lỗi khi tôi gọi nó bằng cách sử dụng câu lệnh CALL:
CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
END
Lỗi cho biết "Bảng không xác định 'hiệu suất'"."Unknown table 'performance' ".
Đây là lần đầu tiên tôi thực sự sử dụng các thủ tục được lưu trữ và tôi đã nhận được nguồn từ Google. Tôi chỉ không thể tìm ra những gì tôi đang làm sai.
Đã hỏi ngày 15 tháng 3 năm 2011 lúc 8:07Mar 15, 2011 at 8:07
Tôi đã dọn dẹp nó một chút cho bạn và thêm mã ví dụ. Tôi luôn giữ tên tham số của mình giống như các trường mà chúng đại diện nhưng tiền tố với P_ ngăn chặn các vấn đề. Tôi làm tương tự với các biến được khai báo trong phần thân Sproc nhưng tiền tố với V_.
Bạn có thể tìm thấy một trong những ví dụ khác của tôi ở đây:
Tạo cây dựa trên độ sâu từ dữ liệu phân cấp trong MySQL [không có CTE]
drop procedure if exists emp_performance;
delimiter #
create procedure emp_performance
[
in p_employee_id varchar[10]
]
begin
declare v_counter int unsigned default 0;
create temporary table tmp engine=memory select time_in, time_out
from attendance where employee_id = p_employee_id;
-- do stuff with tmp...
select count[*] into v_counter from tmp;
-- output and cleanup
select * from tmp order by time_in;
drop temporary table if exists tmp;
end#
delimiter ;
call emp_performance['E123456789'];
Đã trả lời ngày 15 tháng 3 năm 2011 lúc 8:16Mar 15, 2011 at 8:16
Jon Blackjon BlackJon Black
16K5 Huy hiệu vàng41 Huy hiệu bạc42 Huy hiệu đồng5 gold badges41 silver badges42 bronze badges
4
Theo mặc định Biến cấu hình MySQL SQL_NOTES được đặt thành 1.
Điều đó có nghĩa là
DROP TEMPORARY TABLE IF EXISTS performance;
tăng cảnh báo_count bởi một và bạn nhận được cảnh báo khi một thủ tục lưu trữ kết thúc.
Bạn có thể đặt biến SQL_NOTES thành 0 trong quy trình được lưu trữ của My.cnf hoặc viết lại như thế:
CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
Đã trả lời ngày 3 tháng 6 năm 2013 lúc 7:47Jun 3, 2013 at 7:47
BroonobroonobrooNo
4964 Huy hiệu bạc8 Huy hiệu đồng4 silver badges8 bronze badges
13.1.20.2 & nbsp; Tạo câu lệnh bảng tạm thời
Bạn có thể sử dụng từ khóa TEMPORARY
khi tạo bảng. Một bảng TEMPORARY
chỉ hiển thị trong phiên hiện tại và được bỏ tự động khi phiên được đóng. Điều này có nghĩa là hai phiên khác nhau có thể sử dụng cùng một tên bảng tạm thời mà không mâu thuẫn với nhau hoặc với một bảng không có cùng tên hiện có. [Bảng hiện tại được ẩn cho đến khi bảng tạm thời bị bỏ.]
InnoDB
không hỗ trợ các bảng tạm thời nén. Khi innodb_strict_mode
được bật [mặc định],
drop procedure if exists emp_performance;
delimiter #
create procedure emp_performance
[
in p_employee_id varchar[10]
]
begin
declare v_counter int unsigned default 0;
create temporary table tmp engine=memory select time_in, time_out
from attendance where employee_id = p_employee_id;
-- do stuff with tmp...
select count[*] into v_counter from tmp;
-- output and cleanup
select * from tmp order by time_in;
drop temporary table if exists tmp;
end#
delimiter ;
call emp_performance['E123456789'];
0 sẽ trả về lỗi nếu drop procedure if exists emp_performance;
delimiter #
create procedure emp_performance
[
in p_employee_id varchar[10]
]
begin
declare v_counter int unsigned default 0;
create temporary table tmp engine=memory select time_in, time_out
from attendance where employee_id = p_employee_id;
-- do stuff with tmp...
select count[*] into v_counter from tmp;
-- output and cleanup
select * from tmp order by time_in;
drop temporary table if exists tmp;
end#
delimiter ;
call emp_performance['E123456789'];
1 hoặc drop procedure if exists emp_performance;
delimiter #
create procedure emp_performance
[
in p_employee_id varchar[10]
]
begin
declare v_counter int unsigned default 0;
create temporary table tmp engine=memory select time_in, time_out
from attendance where employee_id = p_employee_id;
-- do stuff with tmp...
select count[*] into v_counter from tmp;
-- output and cleanup
select * from tmp order by time_in;
drop temporary table if exists tmp;
end#
delimiter ;
call emp_performance['E123456789'];
2 được chỉ định. Nếu innodb_strict_mode
bị vô hiệu hóa, các cảnh báo sẽ được ban hành và bảng tạm thời được tạo bằng định dạng hàng không nén. Tùy chọn drop procedure if exists emp_performance;
delimiter #
create procedure emp_performance
[
in p_employee_id varchar[10]
]
begin
declare v_counter int unsigned default 0;
create temporary table tmp engine=memory select time_in, time_out
from attendance where employee_id = p_employee_id;
-- do stuff with tmp...
select count[*] into v_counter from tmp;
-- output and cleanup
select * from tmp order by time_in;
drop temporary table if exists tmp;
end#
delimiter ;
call emp_performance['E123456789'];
4 không ảnh hưởng đến việc tạo bảng tạm thời InnoDB
.
drop procedure if exists emp_performance;
delimiter #
create procedure emp_performance
[
in p_employee_id varchar[10]
]
begin
declare v_counter int unsigned default 0;
create temporary table tmp engine=memory select time_in, time_out
from attendance where employee_id = p_employee_id;
-- do stuff with tmp...
select count[*] into v_counter from tmp;
-- output and cleanup
select * from tmp order by time_in;
drop temporary table if exists tmp;
end#
delimiter ;
call emp_performance['E123456789'];
6 gây ra một cam kết ngầm, ngoại trừ khi được sử dụng với từ khóa TEMPORARY
. Xem Phần & NBSP; 13.3.3, Các câu lệnh gây ra một cam kết ngầm. TEMPORARY
Bảng có mối quan hệ rất lỏng lẻo với cơ sở dữ liệu [lược đồ]. Việc bỏ một cơ sở dữ liệu không tự động bỏ bất kỳ bảng TEMPORARY
nào được tạo trong cơ sở dữ liệu đó.
Để tạo một bảng tạm thời, bạn phải có đặc quyền
CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
0. Sau khi một phiên đã tạo một bảng tạm thời, máy chủ không thực hiện kiểm tra đặc quyền nào trên bảng. Phiên tạo có thể thực hiện bất kỳ hoạt động nào trên bảng, chẳng hạn như CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
1, CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
2, CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
3 hoặc CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
4.Một hàm ý của hành vi này là một phiên có thể điều khiển các bảng tạm thời của nó ngay cả khi người dùng hiện tại không có đặc quyền tạo chúng. Giả sử rằng người dùng hiện tại không có đặc quyền
CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
0 nhưng có thể thực thi quy trình được lưu trữ trong bối cảnh xác định thực thi với các đặc quyền của người dùng có CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
0 và tạo ra một bảng tạm thời. Trong khi thủ tục thực thi, phiên sử dụng các đặc quyền của người dùng xác định. Sau khi thủ tục trả về, các đặc quyền hiệu quả trở lại với những người dùng hiện tại, vẫn có thể thấy bảng tạm thời và thực hiện bất kỳ hoạt động nào trên đó.Bạn không thể sử dụng
CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
7 để tạo một bảng trống dựa trên định nghĩa của một bảng nằm trong không gian bảng CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`[id VARCHAR[10]]
BEGIN
SET @@session.sql_notes = 0;
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
SET @@session.sql_notes = 1;
END
8, không gian bảng hệ thống InnoDB
[CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl LIMIT 0;
0] hoặc không gian bảng chung. Định nghĩa không gian bảng cho một bảng như vậy bao gồm thuộc tính CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl LIMIT 0;
1 xác định không gian bảng nơi bảng cư trú và các không gian bảng đã nói ở trên không hỗ trợ các bảng tạm thời. Để tạo một bảng tạm thời dựa trên định nghĩa của bảng như vậy, hãy sử dụng cú pháp này thay thế:CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl LIMIT 0;
Ghi chú
Hỗ trợ cho các điều khoản
CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl LIMIT 0;
2 và CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl LIMIT 0;
3 với CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl LIMIT 0;
4 không được dùng để theo MySQL 8.0.13; Mong đợi nó được loại bỏ trong một phiên bản tương lai của MySQL.