Monday, June 8, 2015

DB2 SQL needs less than 80 characters for mainframe FTP whatever

'This routine fix the input file which just SQL for DB2 and split them into less than 80 characters per 'line

Imports System.IO
Module Module1

    Sub Main()
        'Dim inputfile As String = "c:\aurorasql.txt"
        'Dim outputfile As String = "c:\out.txt"
        Dim inputfile As String = "c:\printer.txt"
        Dim outputfile As String = "c:\outprinter.txt"

        ' split SQL statement into less than length 80
        ' first separate by ","
        ' the first line has SQL command "insert" and it is default lenght is less than 80 so no need of comma
        ' others than need comma

        Using sw As StreamWriter = New StreamWriter(outputfile, True)

            For Each line As String In File.ReadLines(inputfile)
                'Console.WriteLine(line)
                Dim len = 80
                Dim arr = line.Split(",")
                For Each Str As String In arr
                    If Str.Length > 80 Then
                        sw.Write(",")
                        Dim arr1 = Str.Split(" ")
                        Dim str2 As String = String.Empty
                        For Each str1 As String In arr1
                            str2 = str2 & str1 & " "
                            If str2.Length > 70 Then 'this is just make sure total is less than 80
                                sw.WriteLine(str2)
                                str2 = String.Empty
                            End If
                        Next

                        sw.WriteLine(str2)
                        str2 = String.Empty
                    Else
                        If Str.Contains("INSERT") Then
                            sw.WriteLine(Str)
                        Else
                            sw.WriteLine(" ," & Str)
                        End If
                    End If
                Next

            Next
        End Using
        Console.WriteLine("done")
    End Sub

End Module

No comments:

Post a Comment