简体   繁体   English

使用临时表将 T-SQL 转换为 MySQL

[英]Converting T-SQL to MySQL with temp tables

I am working on converting a T-SQL stored procedure into MySQL. I am not familiar with T-SQL and am working on becoming more familiar with temp tables and stored procedures.我正在将 T-SQL 存储过程转换为 MySQL。我不熟悉 T-SQL,正在努力变得更加熟悉临时表和存储过程。 Thanks for your help in advance.提前感谢您的帮助。

The T-SQL original looks like this ( EDIT: note this is only a portion of the original procedure used to produce a report): 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';     

My current update is below.我当前的更新如下。 Do I need to wrap this in a CREATE TEMPORARY TABLE IF NOT EXISTS accounts AS (<insert query here>) instead of the INTO #accounts ?我是否需要将其包装在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

Then do something like this assuming the syntax is right:然后在假设语法正确的情况下做这样的事情:

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'; 

Finally, I assume I could follow this post to wrap the final query into a stored procedure, correct?最后,我假设我可以按照这篇文章将最终查询包装到存储过程中,对吗? To summarize the code in the post:总结帖子中的代码:

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

I wanted to post the solution that finally worked for me for others that might be having the same issue.我想发布最终对我有用的解决方案,供其他可能遇到相同问题的人使用。 The code below resolves this.下面的代码解决了这个问题。

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

However, it is important to note the limits of MySQL temporary tables.但是,重要的是要注意 MySQL 个临时表的限制。 Although I did not indicate it in my original post, later, I was attempting to join the temporary table onto itself.虽然我没有在我的原始帖子中指出它,但后来,我试图将临时表连接到它自己上。 I needed to follow the suggestion outlined here .我需要遵循此处概述的建议。 Essentially, if you need to refer to a temp table with itself you need to make a copy of the temp table: CREATE TEMPORARY TABLE accounts2 AS (SELECT * FROM accounts) .本质上,如果您需要引用临时表本身,则需要复制临时表: CREATE TEMPORARY TABLE accounts2 AS (SELECT * FROM accounts) Then you can join a temp table to itself.然后你可以加入一个临时表到它自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM