sql转换为mysql

yws3nbqq  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(355)

我正在将一个t-sql存储过程转换成mysql。我对t-sql不太熟悉,正在努力使自己对temp表和存储过程更加熟悉。提前谢谢你的帮助。
t-sql的原始版本如下所示(编辑:注意,这只是用于生成报表的原始过程的一部分):

DROP TABLE IF EXISTS #accounts;

SELECT  d.data_id                   
        ,p.pp_name AS name
        ,CONVERT(tinyint,1) AS flag             
        ,d.pd_date
        ,CONVERT(char(6),pd_date,112) AS date_period                
        ,CONVERT(varchar(3),0) AS n_phones                          
INTO    #accounts                   
from    table_detail d                  
    JOIN    table_pool p ON d.pp_id = p.pp_id                   
    JOIN    table_type t ON p.pp_type_id = t.pp_type_id                                                     
    JOIN    Inventory i ON d.data_id = i.data_id                    
    JOIN    Product pr ON i.product_id = pr.product_id  
WHERE   pp_name IN (SELECT name FROM Sandbox..desired_sandbox)                  
    AND DATEDIFF(MONTH,pd_date,GETDATE()) < 3

UPDATE  a                   
SET     a.flag = 0              
FROM    #accounts a                 
JOIN    table_detail d ON a.data_id = d.data_id                 
JOIN    table_pool p ON d.pp_id = p.pp_id                   
WHERE   d.pd_date < a.pd_date                   
    AND pp_name != 'error';

我目前的更新如下。我需要把这个包起来吗 CREATE TEMPORARY TABLE IF NOT EXISTS accounts AS (<insert query here>) 而不是 INTO #accounts ?

SELECT  d.data_id                   
        ,p.pp_name AS name                              
        ,CONVERT(tinyint,1) AS flag             
        ,d.pd_date
        ,DATE_FORMAT(pd_date,'%Y%m%d') AS date_period               
        ,CONVERT(varchar(3),0) AS n_phones                                  
FROM    table_detail d                  
    JOIN    table_pool p ON d.pp_id = p.pp_id                   
    JOIN    table_type t ON p.pp_type_id = t.pp_type_id                                 
    JOIN    Inventory i ON d.data_id = i.data_id                    
    JOIN    Product pr ON i.product_id = pr.product_id  
WHERE   pp_name IN (SELECT name FROM Sandbox..desired_sandbox)              
    AND TIMESTAMPDIFF(MONTH,pd_date,NOW()) < 3

然后在语法正确的情况下执行以下操作:

UPDATE  accounts a -- Is this the correct way to add an alias and update the temp table?                
    JOIN    table_detail d ON a.data_id = d.data_id                 
    JOIN    table_pool p ON d.pp_id = p.pp_id                   
SET     a.flag = 0              
WHERE   d.pd_date < a.pd_date                   
    AND pp_name != 'error';

最后,我假设我可以按照这篇文章将最终的查询 Package 成一个存储过程,对吗?要在帖子中总结代码:

drop procedure if exists procedure_name;
DELIMITER $$
create procedure procedure_name ()
BEGIN
    DROP TEMPORARY TABLE IF EXISTS accounts;

    CREATE TEMPORARY TABLE accounts AS (
    SELECT...
    FROM...
    WHERE...
    ;
    )

    UPDATE accounts
        JOIN ...
        JOIN ...
    SET...
    WHERE...;

    DROP TEMPORARY TABLE accounts; -- otherwise it survives the stored proc call
END
$$ -- signify end of block
DELIMITER ; -- reset to default delimiter
vof42yt1

vof42yt11#

我想发布的解决方案,最终为我的其他人可能有同样的问题。下面的代码解决了这个问题。

DROP PROCEDURE IF EXISTS procedure_name;

DELIMITER $$

CREATE PROCEDURE procedure_name ()
BEGIN
    DROP TEMPORARY TABLE IF EXISTS accounts;

    CREATE TEMPORARY TABLE accounts AS (
        SELECT  d.data_id                   
               ,p.pp_name AS name                              
               ,1 AS flag             
               ,d.pd_date
               ,DATE_FORMAT(pd_date,'%Y%m%d') AS date_period               
               ,CONVERT(varchar(3),0) AS n_phones                                  
       FROM    table_detail d                  
           JOIN    table_pool p ON d.pp_id = p.pp_id                   
           JOIN    table_type t ON p.pp_type_id = t.pp_type_id                                 
           JOIN    Inventory i ON d.data_id = i.data_id                    
           JOIN    Product pr ON i.product_id = pr.product_id  
       WHERE   pp_name IN (SELECT name FROM Sandbox..desired_sandbox)              
           AND TIMESTAMPDIFF(MONTH,pd_date,NOW()) < 3

    );

    -- Update the temp table based on specified criteria
    UPDATE accounts
        JOIN ...
        JOIN ...
    SET...
    WHERE...;

    -- Create final Query for report
    SELECT ...
    FROM accounts
    WHERE ...
    GROUP BY ... -- whatever you need for final query

    DROP TEMPORARY TABLE accounts; -- otherwise it survives the stored proc call
END
$$ -- signify end of block
DELIMITER ; -- reset to default delimiter

但是,必须注意mysql临时表的限制。虽然我在最初的帖子中没有指出,但后来,我尝试将临时表连接到它本身。我需要遵循这里概述的建议。基本上,如果需要引用临时表本身,则需要复制临时表: CREATE TEMPORARY TABLE accounts2 AS (SELECT * FROM accounts) . 然后可以将临时表连接到自身。

相关问题