将SQL查询转换为mysql查询时出错

时间:2019-03-14 03:26:51

标签: mysql

我有一个TSQL查询,该查询曾用于在sql服务器中创建视图。 我有一个想要创建相同视图集的类似MySQL环境。 我使用SQLLines在线转换器将Mssql查询转换为mysql查询,因为我不太熟悉mysql的语法和功能。 但是,转换后的Mysql查询在变量声明本身处失败。

MSSQL查询

docker-compose.yaml

MySQL查询:

    USE DB
GO
DECLARE @qos varchar(255), @pos int, @r_table varchar(255), @view varchar(255), @cview varchar(2048)

DECLARE qos_cursor CURSOR FOR
SELECT DISTINCT qos,r_table FROM S_QOS_DATA
ORDER BY 2

OPEN qos_cursor

-- Perform the first fetch.
FETCH NEXT FROM qos_cursor INTO @qos, @r_table 

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN

                -- Check QOS name for '-' character & replace with '_' if exist
                SELECT @pos = CHARINDEX('-',@qos, 1);
                IF @pos != 0
                BEGIN
                                SELECT @qos = STUFF(@qos, @pos, 1, '_');
                END

                -- Check QOS name for '/' character & replace with '_' if exist
                SELECT @pos = CHARINDEX('/',@qos, 1);
                IF @pos != 0
                BEGIN
                                SELECT @qos = STUFF(@qos, @pos, 1, '_');
                END

                -- Check QOS name for '(' character & replace with '_' if exist
                SELECT @pos = CHARINDEX('(',@qos, 1);
                IF @pos != 0
                BEGIN
                                SELECT @qos = STUFF(@qos, @pos, 1, '_');
                END

                -- Check QOS name for ')' character & replace with '_' if exist
                SELECT @pos = CHARINDEX(')',@qos, 1);
                IF @pos != 0
                BEGIN
                                SELECT @qos = STUFF(@qos, @pos, 1, '_');
                END

                -- Create view
                SET @view = 'V_'+@qos
                SET @cview = 'CREATE VIEW '+@view+' AS '+
                                'SELECT Q.source,Q.target,Q.origin,Q.robot,Q.probe,D.sampletime,D.samplevalue,D.samplestdev,D.samplerate,D.tz_offset '+
                                'FROM S_QOS_DATA Q JOIN '+@r_table+' D ON Q.table_id=D.table_id';

                BEGIN TRY
                                exec ('DROP VIEW '+@view)
                END TRY
                BEGIN CATCH
                                -- Suppress Error message for Views that don't exist
                END CATCH
                BEGIN TRY
                                -- Create the View, Catch tables that don't have samplestdev & samplerate fields
                                exec (@cview);
                                PRINT 'Created View: ' + @view
                END TRY
                BEGIN CATCH
                                SET @cview = 'CREATE VIEW '+@view+' AS '+
                                'SELECT Q.source,Q.target,Q.origin,Q.robot,Q.probe,D.sampletime,D.samplevalue,D.tz_offset '+
                                'FROM S_QOS_DATA Q JOIN '+@r_table+' D ON Q.table_id=D.table_id';
                                exec (@cview);
                                PRINT 'Created View: ' + @view
                END CATCH

                -- PRINT 'qos: ' + @qos + ' ' +  @r_table+' '+@view
                -- PRINT @cview
                -- This is executed as long as the previous fetch succeeds.
                FETCH NEXT FROM qos_cursor INTO @qos, @r_table

END

CLOSE qos_cursor
DEALLOCATE qos_cursor
GO

看起来在线转换器已替换了适用于MySQL的功能,但是当我在MySQL 5.7.20中执行mysql查询时,我会得到

  

您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'DECLARE v_qos varchar(255)附近使用; DECLARE v_pos int;宣告v_r_table varchar(255); D'在第1行

我尝试声明像clarify @这样的变量,选择@都给我MySQL错误。

我很想探索自己,但时间紧迫,所以想在社区寻求专家意见。

谢谢。

1 个答案:

答案 0 :(得分:0)

您在最后一个;语句中忘记了分号DECLARE ...

;之后,您还需要用半冒号ORDER 2 ...

;之后的半冒号OPEN qos_cursor ...