将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