DELIMITER $
CREATE PROCEDURE geographical(IN appl_name1 varchar(500),
IN appl_name2 varchar(500),
IN appl_name3 varchar(500),
IN appl_name4 varchar(500),
IN granted char(1),
IN oppose tinyint(4),
IN beginDate date,
IN endDate date,
IN ipc varchar(300))
BEGIN
DROP TABLE IF EXISTS first_ctry;
DROP TABLE IF EXISTS second_ctry;
DROP TABLE IF EXISTS third_ctry;
DROP TABLE IF EXISTS fourth_ctry;
DROP TABLE IF EXISTS ref_ctry;
SET @d1 = CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS first_ctry AS
(SELECT a.docdb_family_id, a.appln_auth, MIN(a.appln_filing_date) as first_date
FROM patents_prod1.tls201_appln a
INNER JOIN patents_prod1.tls207_pers_appln b ON b.appln_id = a.appln_id
INNER JOIN entity.combined c ON c.person_id = b.person_id
INNER JOIN patents_prod1.tls209_appln_ipc d ON d.appln_id = a.appln_id
WHERE c.name_friendly LIKE ', CONCAT('"', appl_name1, '"'),
' AND IF(',CONCAT('"',granted,'"'),' IS NOT NULL,a.granted LIKE ',CONCAT('"',granted,'"'),',1=1)
AND IF(',CONCAT('"',oppose,'"'),' IS NOT NULL,a.isOpposed = ',CONCAT('"',oppose,'"'),',1=1)
AND IF(',CONCAT('"',beginDate,'"'),' IS NOT NULL AND ',CONCAT('"',endDate,'"'),' IS NOT NULL,a.appln_filing_date BETWEEN ',CONCAT('"',beginDate,'"'),' and ',CONCAT('"',endDate,'"'),',1=1)
AND IF(',CONCAT('"',beginDate,'"'),' IS NOT NULL AND ',CONCAT('"',endDate,'"'),' IS NULL,a.appln_filing_date BETWEEN ',CONCAT('"',beginDate,'"'),' and CURDATE(),1=1)
AND IF(',CONCAT('"',ipc,'"'),' IS NOT NULL,FIND_IN_SET(d.ipc_class_symbol, ',CONCAT('"',ipc,'"'),'),1=1)
AND a.appln_filing_year < 9999
GROUP BY a.docdb_family_id, a.appln_auth
ORDER BY a.docdb_family_id, first_date)');
PREPARE stmtd1 FROM @d1;
EXECUTE stmtd1;
DEALLOCATE PREPARE stmtd1;
SET @s1 = CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS ref_ctry AS
(SELECT appln_auth, COUNT(distinct docdb_family_id) as ',CONCAT('"', appl_name1, '"'),' FROM first_ctry
GROUP BY appln_auth
ORDER BY ', CONCAT('"', appl_name1, '"'), ' DESC LIMIT 15)');
PREPARE stmt1 FROM @s1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
IF (appl_name2 IS NOT NULL OR appl_name2 <> '') THEN
SET @d2 = CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS second_ctry AS
(SELECT a.docdb_family_id, a.appln_auth, MIN(a.appln_filing_date) as first_date
FROM patents_prod1.tls201_appln a
INNER JOIN patents_prod1.tls207_pers_appln b ON b.appln_id = a.appln_id
INNER JOIN entity.combined c ON c.person_id = b.person_id
INNER JOIN patents_prod1.tls209_appln_ipc d ON d.appln_id = a.appln_id
WHERE c.name_friendly LIKE ', CONCAT('"', appl_name2, '"'),
' AND IF(',CONCAT('"',granted,'"'),' IS NOT NULL,a.granted LIKE ',CONCAT('"',granted,'"'),',1=1)
AND IF(',CONCAT('"',oppose,'"'),' IS NOT NULL,a.isOpposed = ',CONCAT('"',oppose,'"'),',1=1)
AND IF(',CONCAT('"',beginDate,'"'),' IS NOT NULL AND ',CONCAT('"',endDate,'"'),' IS NOT NULL,a.appln_filing_date BETWEEN ',CONCAT('"',beginDate,'"'),' and ',CONCAT('"',endDate,'"'),',1=1)
AND IF(',CONCAT('"',beginDate,'"'),' IS NOT NULL AND ',CONCAT('"',endDate,'"'),' IS NULL,a.appln_filing_date BETWEEN ',CONCAT('"',beginDate,'"'),' and CURDATE(),1=1)
AND IF(',CONCAT('"',ipc,'"'),' IS NOT NULL,FIND_IN_SET(d.ipc_class_symbol, ',CONCAT('"',ipc,'"'),'),1=1)
AND a.appln_filing_year < 9999
GROUP BY a.docdb_family_id, a.appln_auth
ORDER BY a.docdb_family_id, first_date)');
PREPARE stmtd2 FROM @d2;
EXECUTE stmtd2;
DEALLOCATE PREPARE stmtd2;
SET @s2 = CONCAT('ALTER TABLE ref_ctry
ADD COLUMN ', CONCAT('"', appl_name2, '"'), ' int NOT NULL DEFAULT 0');
PREPARE stmt2 FROM @s2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
SET @s3 = CONCAT('UPDATE ref_ctry a
SET ',CONCAT('"', appl_name2, '"'),' = (SELECT COUNT(distinct b.docdb_family_id) FROM second_ctry b WHERE b.appln_auth = a.appln_auth)');
PREPARE stmt3 FROM @s3;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
IF (appl_name3 IS NOT NULL OR appl_name3 <> '') THEN
-- ADD THIRD COUNTRY DATA
SET @d3 = CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS third_ctry AS
(SELECT a.docdb_family_id, a.appln_auth, MIN(a.appln_filing_date) as first_date
FROM patents_prod1.tls201_appln a
INNER JOIN patents_prod1.tls207_pers_appln b ON b.appln_id = a.appln_id
INNER JOIN entity.combined c ON c.person_id = b.person_id
INNER JOIN patents_prod1.tls209_appln_ipc d ON d.appln_id = a.appln_id
WHERE c.name_friendly LIKE ', CONCAT('"', appl_name3, '"'),
' AND IF(',CONCAT('"',granted,'"'),' IS NOT NULL,a.granted LIKE ',CONCAT('"',granted,'"'),',1=1)
AND IF(',CONCAT('"',oppose,'"'),' IS NOT NULL,a.isOpposed = ',CONCAT('"',oppose,'"'),',1=1)
AND IF(',CONCAT('"',beginDate,'"'),' IS NOT NULL AND ',CONCAT('"',endDate,'"'),' IS NOT NULL,a.appln_filing_date BETWEEN ',CONCAT('"',beginDate,'"'),' and ',CONCAT('"',endDate,'"'),',1=1)
AND IF(',CONCAT('"',beginDate,'"'),' IS NOT NULL AND ',CONCAT('"',endDate,'"'),' IS NULL,a.appln_filing_date BETWEEN ',CONCAT('"',beginDate,'"'),' and CURDATE(),1=1)
AND IF(',CONCAT('"',ipc,'"'),' IS NOT NULL,FIND_IN_SET(d.ipc_class_symbol, ',CONCAT('"',ipc,'"'),'),1=1)
AND a.appln_filing_year < 9999
GROUP BY a.docdb_family_id, a.appln_auth
ORDER BY a.docdb_family_id, first_date)');
PREPARE stmtd3 FROM @d3;
EXECUTE stmtd3;
DEALLOCATE PREPARE stmtd3;
SET @s4 = CONCAT('ALTER TABLE ref_ctry
ADD COLUMN ', CONCAT('"', appl_name3, '"'), ' int NOT NULL DEFAULT 0');
PREPARE stmt4 FROM @s4;
EXECUTE stmt4;
DEALLOCATE PREPARE stmt4;
SET @s5 = CONCAT('UPDATE ref_ctry a
SET ',CONCAT('"', appl_name3, '"'),' = (SELECT COUNT(distinct b.docdb_family_id) FROM third_ctry b WHERE b.appln_auth = a.appln_auth)');
PREPARE stmt5 FROM @s5;
EXECUTE stmt5;
DEALLOCATE PREPARE stmt5;
IF (appl_name4 IS NOT NULL OR appl_name4 <> '') THEN
-- ADD FOURTH COUNTRY DATA
SET @d4 = CONCAT('CREATE TEMPORARY TABLE IF NOT EXISTS fourth_ctry AS
(SELECT a.docdb_family_id, a.appln_auth, MIN(a.appln_filing_date) as first_date
FROM patents_prod1.tls201_appln a
INNER JOIN patents_prod1.tls207_pers_appln b ON b.appln_id = a.appln_id
INNER JOIN entity.combined c ON c.person_id = b.person_id
INNER JOIN patents_prod1.tls209_appln_ipc d ON d.appln_id = a.appln_id
WHERE c.name_friendly LIKE ', CONCAT('"', appl_name4, '"'),
' AND IF(',CONCAT('"',granted,'"'),' IS NOT NULL,a.granted LIKE ',CONCAT('"',granted,'"'),',1=1)
AND IF(',CONCAT('"',oppose,'"'),' IS NOT NULL,a.isOpposed = ',CONCAT('"',oppose,'"'),',1=1)
AND IF(',CONCAT('"',beginDate,'"'),' IS NOT NULL AND ',CONCAT('"',endDate,'"'),' IS NOT NULL,a.appln_filing_date BETWEEN ',CONCAT('"',beginDate,'"'),' and ',CONCAT('"',endDate,'"'),',1=1)
AND IF(',CONCAT('"',beginDate,'"'),' IS NOT NULL AND ',CONCAT('"',endDate,'"'),' IS NULL,a.appln_filing_date BETWEEN ',CONCAT('"',beginDate,'"'),' and CURDATE(),1=1)
AND IF(',CONCAT('"',ipc,'"'),' IS NOT NULL,FIND_IN_SET(d.ipc_class_symbol, ',CONCAT('"',ipc,'"'),'),1=1)
AND a.appln_filing_year < 9999
GROUP BY a.docdb_family_id, a.appln_auth
ORDER BY a.docdb_family_id, first_date)');
PREPARE stmtd4 FROM @d4;
EXECUTE stmtd4;
DEALLOCATE PREPARE stmtd4;
SET @s6 = CONCAT('ALTER TABLE ref_ctry
ADD COLUMN ', CONCAT('"', appl_name4, '"'), ' int NOT NULL DEFAULT 0');
PREPARE stmt6 FROM @s6;
EXECUTE stmt6;
DEALLOCATE PREPARE stmt6;
SET @s7 = CONCAT('UPDATE ref_ctry a
SET ',CONCAT('"', appl_name4, '"'),' = (SELECT COUNT(distinct b.docdb_family_id) FROM fourth_ctry b WHERE b.appln_auth = a.appln_auth)');
PREPARE stmt7 FROM @s7;
EXECUTE stmt7;
DEALLOCATE PREPARE stmt7;
-- FINAL TABLE FOR HORIZONTAL BAR CHARTS**
SELECT * FROM ref_ctry;
ELSE
SELECT * FROM ref_ctry;
END IF;
ELSE
SELECT * FROM ref_ctry;
END IF;
ELSE
SELECT * FROM ref_ctry;
END IF;
END$
DELIMITER ;
我正在将此程序创建为地域。 当我运行时,它给我错误
call geographical("VESTAS",null,null,null,null,null,null,null,null);
错误1064(42000):您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以在第1行的'NULL'附近使用正确的语法
我还编写了另一个过程,其中引号中没有语句,这给了我一个空集。
答案 0 :(得分:0)
您要将null隐含为字符串,这将导致null。 例如,存在CONCAT('“',appl_name2,'”'),其中appl_name2为null,这将导致结果为null,而不是“ null”(字符串)。
您可以尝试这样做以更好地理解:
select concat('first',null,'second');
我建议您使用COALESCE函数,例如:
select concat('first',coalesce(appl_name2,'NULL','notnull'),'second');
答案 1 :(得分:0)
没有理由让您在存储过程中使用准备好的语句。这样会使过程更难于编写/读取/维护,并且更容易出错。
在该过程中使用直接SQL,因此还可以避免在连接NULL值时出现问题。