数据库迁移mssql to pgsql之函数转换

简介: 数据库迁移mssql to pgsql之函数转换

将MS SQL数据库中的存储过程转换为PG SQL的函数,我编了一段代码,将大部分区别转换了一下,之后再手工整理,还是能快一些。

当然也可以使用一些工具来处理这一问题。

下面是一段VB.NET代码,仅供参考。

Private Sub MakeFunc(ByVal FNname As String, ByVal Gc As Integer)
'转换过程
Dim BjStr As String = ""    '代码行
Try
    Dim dtdsql As String = ""
    Dim dqtext As String = ""
    Dim nexttext As String = ""
    Dim nextnext As String = ""
    Dim utext As String = ""
    Dim intotext As String = ""
    Dim fulltext As String = ""
    Dim dqas As Integer = 0
    BjStr += "--" & FNname & vbCrLf
    Dim dtd As New DataTable
    dtdsql = "exec sys.sp_helptext '" & FNname & "'"
    dtd = hyDB.DtRead(dtdsql).Tables(0)
    For L As Integer = 0 To dtd.Rows.Count - 1
        fulltext += dtd.Rows(L).Item("text")
    Next
    fulltext = ""
    fulltext = Nothing
    Dim dtp As New DataTable    '获取参数
    dtp = hyDB.DtRead("EXEC DBO.GETPT '" & FNname & "'").Tables(0)
    Dim pt(15, 1) As String
    For C As Integer = 0 To 15
        pt(C, 0) = ""
    Next
    If Not IsNothing(dtp) Then
        If dtp.Rows.Count > 0 Then
            If Gc = 0 Then
                For C As Integer = 0 To dtp.Rows.Count - 1
                    If dtp.Rows(C).Item("ParamType") = "OUTPUT" Then
                        dtp.Rows(C).Delete()
                    End If
                Next
            End If
            dtp.AcceptChanges()
            For C As Integer = 0 To dtp.Rows.Count - 1
                pt(C, 0) = dtp.Rows(C).Item("ParamName")
                pt(C, 1) = "$" & (C + 1).ToString.Trim
            Next
        End If
    End If
    For L As Integer = 0 To dtd.Rows.Count - 1
        dqtext = UCase(dtd.Rows(L).Item("text"))
        If L < dtd.Rows.Count - 1 Then
            nexttext = dtd.Rows(L + 1).Item("text")
        Else
            nexttext = ""
        End If
        If L < dtd.Rows.Count - 2 Then
            nextnext = dtd.Rows(L + 2).Item("text")
        Else
            nextnext = ""
        End If
        dqtext = Replace(dqtext, "  ", " ")
        dqtext = Replace(dqtext, "  ", " ")
        dqtext = Replace(dqtext, "  ", " ")
        dqtext = Replace(dqtext, "  ", " ")
        dqtext = Replace(dqtext, "[", "")
        dqtext = Replace(dqtext, "]", "")
        utext = dqtext
        If InStr(utext, "BEGIN CATCH") > 0 Then
            Exit For
        End If
        If UCase(Strings.Left(utext, 2)) = "GO" Then
            Continue For
        End If
        If Trim(dqtext) = vbCrLf Then
            Continue For
        End If
        If InStr(utext, "NOCOUNT") > 0 Then
            Continue For
        End If
        If InStr(utext, "BEGIN TRY") > 0 Then
            Continue For
        End If
        If InStr(utext, "TRANS") > 0 Then
            Continue For
        End If
        If InStr(utext, "COMMIT") > 0 Then
            Continue For
        End If
        If InStr(utext, "END TRY") > 0 Then
            Continue For
        End If
        If InStr(utext, "DEALLOCATE") > 0 Then
            Continue For
        End If
        If InStr(utext, "FETCH ") > 0 Then
            Continue For
        End If
        If InStr(dqtext, "ErrorLogID ") > 0 Then
            Continue For
        End If
        If InStr(utext, "BEGIN") > 0 And InStr(utext, "CASE") <= 0 Then
            Continue For
        End If
        If InStr(utext, "CREATE") > 0 Then
            dqtext = Replace(dqtext, "[", "")
            dqtext = Replace(dqtext, "]", "")
            dqtext = Replace(dqtext, "PROCEDURE", "FUNCTION")
        End If
        If InStr(utext, "AS") > 0 And dqas = 0 Then
            dqas = L
            dqtext = " RETURNS VARCHAR" & vbCrLf & dqtext
            dqtext = Replace(dqtext, "AS", " AS $$ ") & vbCrLf & vbCrLf
            dqtext += "-- ===========================================================================" & vbCrLf
            dqtext += "-- Author: " & vbCrLf
            dqtext += "-- Create date: " & Format(Now, "yyyy-MM-dd") & vbCrLf
            dqtext += "-- Description:  " & vbCrLf
            dqtext += "-- ===========================================================================" & vbCrLf
            dqtext += "DECLARE " & vbCrLf
            'dqtext += vbTab & "ssmsg text;ssdet text;sstname text;sshint text;ssctt text; --错误处理 " & vbCrLf
            dqtext += vbTab & "--RD RECORD;" & vbCrLf & vbCrLf
            dqtext += "BEGIN" & vbCrLf
            For C As Integer = 0 To 15
                If pt(C, 0) <> "" Then
                    BjStr = Replace(BjStr, "@", "SS")
                End If
            Next
            BjStr += dqtext
            Continue For
        End If
        If InStr(utext, "INTO") > 0 And InStr(utext, "SELECT") <= 0 And InStr(utext, "INSERT") <= 0 And
                InStr(utext, "@") > 0 And L > dqas And dqas > 0 Then
            intotext = Replace(utext, "INTO", "")
            intotext = Replace(intotext, " ", "")
            intotext = Replace(intotext, vbTab, "")
            intotext = Replace(intotext, ";", "")
            intotext = Replace(intotext, vbCrLf, "")
        End If
        If InStr(utext, "SELECT") <= 0 And InStr(utext, "INSERT") <= 0 And InStr(utext, "INTO") > 0 Then
            dqtext = Replace(dqtext, "INTO", "--INTO")
        End If
        If InStr(utext, "SELECT") > 0 And InStr(utext, "=") > 0 And InStr(utext, "@") > 0 And InStr(utext, "FROM") <= 0 Then
            dqtext = Replace(dqtext, "SELECT", "")
        End If
        '查询赋值
        If InStr(utext, "SELECT") > 0 Then
            Dim Si As Integer = 0
            Dim Fi As Integer = 0
            Si = InStr(dqtext, "SELECT")
            Fi = InStr(dqtext, "FROM")
            If Fi > 0 And InStr(utext, "=") > 0 And Fi > InStr(utext, "=") And InStr(utext, "EXISTS") <= 0 And
                InStr(utext, "@") > 0 And Fi > InStr(utext, "@") Then
                Dim fzstr As String
                fzstr = Mid(dqtext, Si + 6, Fi - Si - 6).Trim
                Dim fz() As String = Split(fzstr, ",")
                Dim fzcd As Integer = fz.Length
                Dim ly As String = ""
                Dim bl As String = ""
                For I As Integer = 0 To fzcd - 1
                    Dim dqz As String
                    dqz = fz(I)
                    If InStr(dqz, "=") > 0 Then
                        Dim xz() As String = Split(dqz, "=")
                        If xz.Length = 2 Then
                            ly += xz(1) & ","
                            bl += xz(0) & ","
                        End If
                    End If
                Next
                If ly.Length > 2 Then
                    ly = Strings.Left(ly, ly.Length - 1)
                End If
                If bl.Length > 2 Then
                    bl = Strings.Left(bl, bl.Length - 1)
                End If
                Dim fzth As String
                fzth = " " & ly & " INTO " & bl & " "
                dqtext = Replace(dqtext, fzstr, fzth)
            End If
        End If
        If InStr(utext, "END") > 0 And InStr(utext, "IF") <= 0 And InStr(utext, "CASE") <= 0 Then
            dqtext = ""
            If InStr(nexttext, "ELSE") <= 0 Then
                Dim kg As Integer = 0
                Dim jstext As String = utext
                While Strings.Left(jstext, 3) <> "END"
                    jstext = Strings.Replace(jstext, vbTab, "", 1, 1)
                    kg += 1
                    If kg > 7 Then
                        Exit While
                    End If
                End While
                If nexttext <> "" And InStr(nexttext, "CLOSE") > 0 Then
                    If kg > 2 Then
                        dqtext += Strings.StrDup(kg - 1, vbTab) & "END LOOP;" & vbCrLf
                    Else
                        dqtext += vbTab & "END LOOP;" & vbCrLf
                    End If
                Else
                    If kg > 2 Then
                        dqtext += Strings.StrDup(kg - 1, vbTab) & "END IF;" & vbCrLf
                    Else
                        dqtext += vbTab & "END IF;" & vbCrLf
                    End If
                End If
            End If
        End If
        If InStr(utext, "CHAR(13)+CHAR(10)") > 0 Then
            dqtext = Replace(dqtext, "CHAR(13)+CHAR(10)", "CHR(13)+CHR(10)")
        End If
        If InStr(utext, "UPDATE") <= 0 And InStr(utext, "IF") > 0 And InStr(utext, "IIF(") < 0 And InStr(utext, "SET") > 0 Then
            dqtext = Replace(dqtext, "SET", " THEN ")
        End If
        If InStr(utext, "IF ") <= 0 And InStr(utext, "IIF(") <= 0 And InStr(utext, "UPDATE") <= 0 And InStr(utext, "SET") > 0 Then
            dqtext = Replace(dqtext, "SET ", "")
        End If
        If InStr(utext, "IF") > 0 And InStr(utext, "END") <= 0 And InStr(utext, "IIF(") <= 0 Then
            dqtext = Replace(dqtext, vbCrLf, " THEN" & vbCrLf)
        End If
        If nexttext <> "" And InStr(utext, "IF") > 0 And InStr(utext, "IIF(") <= 0 
            And InStr(nexttext, "BEGIN") <= 0 And InStr(nexttext, ";") > 0 Then
            dtd.Rows(L + 1).Item("text") += Strings.StrDup(Tabs(utext), vbTab) & "END IF;" & vbCrLf
        End If
        If InStr(utext, "IIF(") > 0 Then
            dqtext = Replace(dqtext, "IIF(", "X9_IIF(")
        End If
        If InStr(utext, "DECLARE") > 0 Then
            If InStr(utext, "@") > 0 Then
                dqtext = Replace(dqtext, ",", ";")
            End If
            dqtext = Replace(dqtext, "DECLARE ", "")
            dqtext = Replace(dqtext, "DECLARE", "")
        End If
        If InStr(utext, "=") > 0 Then
            If (InStr(utext, "SELECT") > 0 Or InStr(utext, "SET") > 0) Then
                If InStr(utext, "WHERE") <= 0 And InStr(utext, " AND ") <= 0 And
                    InStr(utext, " OR ") <= 0 And InStr(utext, "UPDATE") <= 0 And InStr(utext, "FROM") <= 0 And
                    InStr(utext, "IF ") <= 0 And (InStr(nexttext, "FROM") <= 0 Or InStr(nexttext, "FETCH") > 0) Then
                    dqtext = Replace(dqtext, "=", ":=")
                End If
            End If
        End If
        If InStr(utext, "FORMAT(") > 0 Then
            dqtext = Replace(dqtext, "FORMAT", "TO_CHAR(")
        End If
        If InStr(utext, "CONVERT(") > 0 Then
            dqtext = Replace(dqtext, "CONVERT(", "CAST(")
        End If
        If InStr(utext, "CURSOR") > 0 And InStr(utext, "LOCAL") > 0 Then
            dqtext = Replace(dqtext, "LOCAL", "")
        End If
        If InStr(utext, "FORMAT(") > 0 Then
            dqtext = Replace(dqtext, "FORMAT", "TO_CHAR(")
        End If
        If InStr(utext, "CHARINDEX(") > 0 Then
            dqtext = Replace(dqtext, "CHARINDEX(", "position(")
        End If
        If InStr(utext, "DELETE") > 0 Then
            dqtext = Replace(dqtext, "DELETE", "DELETE FROM")
        End If
        If InStr(utext, "(MAX)") > 0 Then
            dqtext = Replace(dqtext, "(MAX)", "")
        End If
        If InStr(utext, "LEN(") > 0 Then
            dqtext = Replace(dqtext, "LEN(", "LENGTH(")
        End If
        If InStr(utext, "CONVERT(") > 0 Then
            dqtext = Replace(dqtext, "CONVERT(", "CAST(")
        End If
        Dim lstext = Replace(utext, " ", "")
        If InStr(lstext, "'+") > 0 Or InStr(lstext, "+'") > 0 Then
            dqtext = Replace(dqtext, "' +", "'+")
            dqtext = Replace(dqtext, "+ '", "+'")
            dqtext = Replace(dqtext, "'+", "'||")
            dqtext = Replace(dqtext, "+'", "||'")
        End If
        If InStr(utext, "WHILE ") > 0 Then
            Dim kg As Integer = 0
            kg = Tabs(utext)
            dqtext = ""
            dqtext += Strings.StrDup(kg, vbTab) & "LOOP" & vbCrLf
            dqtext += Strings.StrDup(kg + 2, vbTab) & "FETCH CS_DJM INTO RD;" & vbCrLf
            dqtext += Strings.StrDup(kg + 2, vbTab) & "EXIT WHEN NOT FOUND;" & vbCrLf
        End If
        dqtext = Replace(dqtext, "--", "-- ")
        dqtext = Replace(dqtext, "DECIMAL", "NUMERIC")
        dqtext = Replace(dqtext, "(16;2)", "(16,2)")
        dqtext = Replace(dqtext, "(16;4)", "(16,4)")
        dqtext = Replace(dqtext, "(18;2)", "(16,2)")
        dqtext = Replace(dqtext, "DBO.", "")
        dqtext = Replace(dqtext, "NVARCHAR", "VARCHAR")
        dqtext = Replace(dqtext, "GETDATE()", "CURRENT_DATE")
        BjStr = BjStr & dqtext
    Next
    For C As Integer = 0 To 15
        If pt(C, 0) <> "" Then
            BjStr = Replace(BjStr, pt(C, 0), pt(C, 1))
        End If
    Next
    If intotext <> "" Then
        Dim Ybbl() As String = Split(intotext, ",")
        For Y As Integer = 0 To Ybbl.Length - 1
            Dim dqbl As String = Ybbl(Y).Trim
            Dim thbl As String = ""
            If InStr(dqbl, "@") > 0 Then
                thbl = Strings.Replace(dqbl, "@", "RD.")
                BjStr = Replace(BjStr, dqbl, thbl)
            End If
        Next
    End If
    BjStr = Replace(BjStr, vbTab & vbTab, vbTab)
    BjStr = Replace(BjStr, "@", "SS")
    BjStr = BjStr.ToLower
    'BjStr += "EXCEPTION WHEN OTHERS THEN" & vbCrLf
    'BjStr += vbTab & "GET STACKED DIAGNOSTICS ssmsg = MESSAGE_TEXT,ssdet = PG_EXCEPTION_DETAIL," & vbCrLf
    'BjStr += vbTab & vbTab & "sstname = TABLE_NAME, sshint = PG_EXCEPTION_HINT, ssctt = PG_EXCEPTION_CONTEXT;" & vbCrLf
    'BjStr += vbTab & "CALL x9_jlproc(ssmsg, ssdet, sstname, sshint, ssctt); " & vbCrLf
    BjStr += "END;" & vbCrLf
    BjStr = BjStr & "$$ LANGUAGE plpgsql;" & vbCrLf
    MM1.Text += BjStr & vbCrLf
Catch ex As Exception
    MM1.Text = ""
    MessageBox.Show(ex.Message)
End Try
End Sub


相关文章
|
3月前
|
弹性计算 关系型数据库 数据库
手把手带你从自建 MySQL 迁移到云数据库,一步就能脱胎换骨
阿里云瑶池数据库来开课啦!自建数据库迁移至云数据库 RDS原来只要一步操作就能搞定!点击阅读原文完成实验就可获得一本日历哦~
|
3月前
|
关系型数据库 MySQL 数据库
|
3月前
|
关系型数据库 MySQL 数据库
RDS MySQL灾备服务协同解决方案构建问题之数据库备份数据的云上云下迁移如何解决
RDS MySQL灾备服务协同解决方案构建问题之数据库备份数据的云上云下迁移如何解决
|
25天前
|
算法 大数据 数据库
云计算与大数据平台的数据库迁移与同步
本文详细介绍了云计算与大数据平台的数据库迁移与同步的核心概念、算法原理、具体操作步骤、数学模型公式、代码实例及未来发展趋势与挑战。涵盖全量与增量迁移、一致性与异步复制等内容,旨在帮助读者全面了解并应对相关技术挑战。
33 3
|
1月前
|
SQL 数据库 数据库管理
数据库SQL函数应用技巧与方法
在数据库管理中,SQL函数是处理和分析数据的强大工具
|
2月前
|
存储 SQL 关系型数据库
一篇文章搞懂MySQL的分库分表,从拆分场景、目标评估、拆分方案、不停机迁移、一致性补偿等方面详细阐述MySQL数据库的分库分表方案
MySQL如何进行分库分表、数据迁移?从相关概念、使用场景、拆分方式、分表字段选择、数据一致性校验等角度阐述MySQL数据库的分库分表方案。
404 15
一篇文章搞懂MySQL的分库分表,从拆分场景、目标评估、拆分方案、不停机迁移、一致性补偿等方面详细阐述MySQL数据库的分库分表方案
|
2月前
|
SQL Java 数据库连接
数据库迁移不再难:Flyway 与 Liquibase 大比拼,哪个才是你的真命天子?
【9月更文挑战第3天】数据库迁移在软件开发中至关重要,尤其在使用 ORM 框架如 Hibernate 时。为确保部署时能顺利应用最新的数据库变更,开发者常使用自动化工具。Flyway 和 Liquibase 是当前流行的两种选择,均能有效管理数据库版本控制。Flyway 采用 SQL 脚本表示变更,简单易用;Liquibase 支持多种脚本格式,功能更强大,适合复杂项目。本文将对比这两种工具的特点,并通过示例展示各自的优缺点,帮助开发者根据项目需求做出合适的选择。
490 1
|
3月前
|
Linux 数据库 数据安全/隐私保护
|
2月前
|
SQL 存储 数据库
MSSQL遍历数据库根据列值查询数据
【9月更文挑战第12天】在 SQL Server 中,可以通过游标或临时表遍历数据库并根据列值查询数据。示例展示了如何创建临时表存储数据库名,并通过循环遍历这些名称来执行特定查询。需替换 `YourTableName`、`YourColumnName` 和 `YourValue` 为实际值。此方法要求有足够权限访问各数据库。若无跨库权限,需分别执行查询。
|
3月前
|
JSON NoSQL Ubuntu
在Ubuntu 14.04上如何备份、恢复和迁移MongoDB数据库
在Ubuntu 14.04上如何备份、恢复和迁移MongoDB数据库
91 1