wdbates
asked on
How to delete 3 lines of code in a sql script
I have a few hundred sql scripts where I need to delete 3 lines of codes and delete the very last line of code. The first 3 are:
DECLARE @dtStartCol datetime
DECLARE @dtEndCol datetime
DECLARE @dtLastUp datetime. The last line is garbage that was place in by accident.
All of these files are in 1 folder and ends with .sql.
DECLARE @dtStartCol datetime
DECLARE @dtEndCol datetime
DECLARE @dtLastUp datetime. The last line is garbage that was place in by accident.
All of these files are in 1 folder and ends with .sql.
if you are familiar with any language, then use to read the files using any type of file system object. then read the content line by line then check for the line you want to delete. then remove that line and save the file.
ASKER
I have relocated off the mainframe, DB2 to SQL2005. On the mf I would use REXX along with Xedit and loop through the files looking for the string or last line and delete that record or row. I am sure there is something in DOS or XP that could do the same.
You could try this VBScript. It doesn't do anything about the last line yet. Can you state some sort of rule about what it contains?
Dim fso, fld, fil, ts1, ts2, line, regx
Const BaseFolder = "c:\SQL Scripts"
Const PatternStr = "DECLARE +@dt(StartCol|EndCol|LastUp) + datetime *"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(BaseFolder) 'update
fso.CreateFolder BaseFolder & "\Clean"
Set regx = New RegExp
With regx
.IgnoreCase = True
.Pattern = PatternStr
End With
For Each fil In fld.Files
If UCase(Right(fil.Name, 4)) = ".SQL" Then
Set ts1 = fso.OpenTextFile(fil.Path)
Set ts2 = fso.CreateTextFile(BaseFolder & "\Clean\" & fil.Name)
Do Until ts1.AtEndOfStream
line = ts1.ReadLine
If Not regx.Test(line) Then
ts2.WriteLine line
End If
Loop
ts1.Close
ts2.Close
End If
Next
Set regx = Nothing
Set ts1 = Nothing
Set ts2 = Nothing
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing
Change:
Const PatternStr = "DECLARE +@dt(StartCol|EndCol|LastU p) + datetime *"
to:
Const PatternStr = "DECLARE +@dt(StartCol|EndCol|LastU p) +datetime *"
Const PatternStr = "DECLARE +@dt(StartCol|EndCol|LastU
to:
Const PatternStr = "DECLARE +@dt(StartCol|EndCol|LastU
ASKER
¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿°¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿4¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿(¿¿ ¿¿4¿¿¿4¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿°¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿4¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿(¿¿¿¿4¿¿ ¿4¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿°¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿4¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿(¿¿ ¿¿4¿¿¿4¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿°¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿4¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿(¿¿¿¿4¿¿ ¿4¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿°¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿4¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿(¿¿ ¿¿4¿¿¿4¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿°¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿4¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿(¿¿¿¿4¿¿ ¿4¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿°¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿4¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿(¿¿ ¿¿4¿¿¿4¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿°¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿4¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿(¿¿¿¿4¿¿ ¿4¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿°¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿4¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿(¿¿ ¿¿4¿¿¿4¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿°¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿4¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿(¿¿¿¿4¿¿ ¿4¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿°¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿4¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿(¿¿ ¿¿4¿¿¿4¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿¿¿¿¿ ¿°¿¿¿¿¿¿¿¿ ¿¿¿¿¿¿
?
ASKER
In Rexx or Xedit I could go to the end and delete. Is there something like that in DOS Xp?
When I create this VB Script what is the file type and in LINE 3, should I replace that with the correct file path?
When I create this VB Script what is the file type and in LINE 3, should I replace that with the correct file path?
This may dela with the last line as well:
Dim fso, fld, fil, ts1, ts2, line, regx, arr, i
Const BaseFolder = "c:\SQL Scripts"
Const PatternStr = "DECLARE +@dt(StartCol|EndCol|LastUp) + datetime *"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(BaseFolder) 'update
fso.CreateFolder BaseFolder & "\Clean"
Set regx = New RegExp
With regx
.IgnoreCase = True
.Pattern = PatternStr
End With
For Each fil In fld.Files
If UCase(Right(fil.Name, 4)) = ".SQL" Then
Set ts1 = fso.OpenTextFile(fil.Path)
arr = Split(ts1.ReadAll, vbCrLf)
Set ts2 = fso.CreateTextFile(BaseFolder & "\Clean\" & fil.Name)
For i = 0 To UBound(arr) - 1
line = arr(i)
If Not regx.Test(line) Then
ts2.WriteLine line
End If
Next
ts1.Close
ts2.Close
End If
Next
Set regx = Nothing
Set ts1 = Nothing
Set ts2 = Nothing
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing
ASKER
I figured out my last 2 questions about filetype and execution. I saved the vbs in the same folder with the files and changed line 3 from Const BaseFolder = "c:\SQL Scripts" to Const BaseFolder = "c:\ Data Scripts"
I open a DOS session and received a VBSCript runtime error: Path not found.
I open a DOS session and received a VBSCript runtime error: Path not found.
ASKER
The full path name is C:\Documents and Settings\sql_team\My Documents\SQL Server Management Studio\Projects\data
Then use that full path in the constant declaration. Case does not matter, but spelling does.
ASKER
What about the word Scripts after the path? With it in I receive a Path not found, when I take it out I receive a Invalid procedure call or argument. It creates the Clean folder and there is 1 file there, but empty.
ASKER
The command I use to execute is cscript /nologo delete3.vbs
My previous suggestion neglected to include the fix in the pattern string.
I tested it successfully on my end.
Be sure to update BaseFolder before running.
I tested it successfully on my end.
Be sure to update BaseFolder before running.
Dim fso, fld, fil, ts1, ts2, line, regx, arr, i
Const BaseFolder = "c:\SQL Scripts" 'update as needed
Const PatternStr = "DECLARE +@dt(StartCol|EndCol|LastUp) +datetime *"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(BaseFolder) 'update
fso.CreateFolder BaseFolder & "\Clean"
Set regx = New RegExp
With regx
.IgnoreCase = True
.Pattern = PatternStr
End With
For Each fil In fld.Files
If UCase(Right(fil.Name, 4)) = ".SQL" Then
Set ts1 = fso.OpenTextFile(fil.Path)
arr = Split(ts1.ReadAll, vbCrLf)
Set ts2 = fso.CreateTextFile(BaseFolder & "\Clean\" & fil.Name)
For i = 0 To UBound(arr) - 1
line = arr(i)
If Not regx.Test(line) Then
ts2.WriteLine line
End If
Next
ts1.Close
ts2.Close
End If
Next
Set regx = Nothing
Set ts1 = Nothing
Set ts2 = Nothing
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing
ASKER
Invalid procedure call or argument, Line 25 char 17
Please post the code exactly as you tried to implement it when you got that error.
ewangoya,
>>You can use an application like Notepad++ to do this sort of thing<<
On hundreds of files? Are you volunteering? :)
>>You can use an application like Notepad++ to do this sort of thing<<
On hundreds of files? Are you volunteering? :)
ASKER
Dim fso, fld, fil, ts1, ts2, line, regx, arr, i
Const BaseFolder = "C:\Documents and Settings\sql_team\My Documents\SQL Server Management Studio\Projects\data"
Const PatternStr = "DECLARE +@dt(StartCol|EndCol|LastU p) +datetime *"
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
Set fld = fso.GetFolder(BaseFolder) 'update
fso.CreateFolder BaseFolder & "\Clean"
Set regx = New RegExp
With regx
.IgnoreCase = True
.Pattern = PatternStr
End With
For Each fil In fld.Files
If UCase(Right(fil.Name, 4)) = ".SQL" Then
Set ts1 = fso.OpenTextFile(fil.Path)
arr = Split(ts1.ReadAll, vbCrLf)
Set ts2 = fso.CreateTextFile(BaseFol der & "\Clean\" & fil.Name)
For i = 0 To UBound(arr) - 1
line = arr(i)
If Not regx.Test(line) Then
ts2.WriteLine line
End If
Next
ts1.Close
ts2.Close
End If
Next
Set regx = Nothing
Set ts1 = Nothing
Set ts2 = Nothing
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing
Const BaseFolder = "C:\Documents and Settings\sql_team\My Documents\SQL Server Management Studio\Projects\data"
Const PatternStr = "DECLARE +@dt(StartCol|EndCol|LastU
Set fso = CreateObject("Scripting.Fi
Set fld = fso.GetFolder(BaseFolder) 'update
fso.CreateFolder BaseFolder & "\Clean"
Set regx = New RegExp
With regx
.IgnoreCase = True
.Pattern = PatternStr
End With
For Each fil In fld.Files
If UCase(Right(fil.Name, 4)) = ".SQL" Then
Set ts1 = fso.OpenTextFile(fil.Path)
arr = Split(ts1.ReadAll, vbCrLf)
Set ts2 = fso.CreateTextFile(BaseFol
For i = 0 To UBound(arr) - 1
line = arr(i)
If Not regx.Test(line) Then
ts2.WriteLine line
End If
Next
ts1.Close
ts2.Close
End If
Next
Set regx = Nothing
Set ts1 = Nothing
Set ts2 = Nothing
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing
I just ran that same code successfully. The only change I had to make was the specified directory for BaseFolder.
Can you upload some of the files here? You may have to change the extensions to .txt; I cannot remember if .sql is on EE's whitelist or not.
Can you upload some of the files here? You may have to change the extensions to .txt; I cannot remember if .sql is on EE's whitelist or not.
ASKER
Here is a file to use
LtbHomeCondition.sql
LtbHomeCondition.sql
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>I accepted my solution as it was the only one that worked.<<
I suspect you mean:
I accepted my solution as it was the only one that I could get to work.
I suspect you mean:
I accepted my solution as it was the only one that I could get to work.
ASKER
I accepted my solution as it was the only one that worked.