SQL to work in Script Agent

Hello - I have the code below that I was sent as SQL - I’ve tried to change it a bit to work in Notes, but my agent is just skipping right over the code starting with the qry.SQL = line and jumping right to the For j =1 line. Any ideas how to update this code?

The outcome is a tree-structured list that I need to display in a view so I can select from the view on a form.

Thanks in advance!

'Load cbudg from AIS warehouse - ibiscodesets - caccess1

'for Mnemonics field on DonorInfo subform

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase, codedb As NotesDatabase

Dim con As ODBCConnection

Dim qry As ODBCQuery

Dim res As ODBCResultSet

Dim idView As NotesView, codeview As NotesView

Dim doc As NotesDocument, iddoc As NotesDocument

Dim max As Double, id As String

Dim i As Double, j As Double

Dim bigArray() As String

Dim answer As Variant    

Dim newone As String



Set db = session.CurrentDatabase

Set idView=db.getView("(copyofMNIC)")

’ warehouse control

Dim wh_db As New NotesDatabase("","")

Dim wh_doc As NotesDocument

Dim wh_view As notesview

Set wh_db=session.getdatabase("DDAS2/Notes","warehousctacs.nsf")

Set wh_view=wh_db.getview("AISCT")

Dim c1 As String, c2 As String, c3 As String, c4 As String

Set wh_doc = wh_view.Getfirstdocument

If Not wh_Doc Is Nothing Then

	c1 = wh_doc.ColumnValues(0)

	c2 = wh_doc.ColumnValues(1)

	c3 = wh_doc.ColumnValues(2)

	c4 = wh_doc.ColumnValues(3)

End If



Set con = New ODBCConnection

Set qry = New ODBCQuery

Set res =  New ODBCResultSet

Call con.connectTo(c1,c2,c3,c4)

Set qry.Connection = con



'pyb2 changed when to if

          '+ "   when description like '__00000000000' then substring(description, 1, 2) + '%' "_



qry.SQL = "select rtrim(code_value) as Mnemonic, description as AccessCode,"_

+ "rtrim(case"_

+ "   if description like '__00000000000' then substring(description, 1, 2) + '%' "_

+ "   if description like '____000000000' then substring(description, 1, 4) + '%' "_

+ "   if description like '_____00000000' then substring(description, 1, 5) + '%' "_

+ "   if description like '______0000000' then substring(description, 1, 6) + '%' "_

+ "   if description like '_________0000' then substring(description, 1, 9) + '%' "_

+ "   if description like '__________000' then substring(description, 1, 10) + '%' "_

+ "  else description "_

+ " end) as AccessFilter, "_

+ " rtrim(cast(text1 as char(30))) as MDesc,  0 as MLevel"_

+ " into #t "_

+ " from	dbo.caccess1"_

+ "where getdate() between begin_date and end_date" _

+ "SELECT c.Mnemonic, c.AccessCode, c.AccessFilter, c.MDesc," _

+ " (SELECT COUNT(*) FROM #t p WHERE c.AccessCode <> p.AccessCode and c.AccessCode like p.AccessFilter) As MLevel, "_

+ "  (SELECT TOP 1 p.Mnemonic FROM #t p WHERE c.AccessCode <> p.AccessCode and c.AccessCode like p.AccessFilter ORDER BY p.AccessCode DESC) As Parent, " _

+ "CASE WHEN Exists (SELECT * FROM #t p WHERE c.AccessCode <> p.AccessCode and p.AccessCode like c.AccessFilter) THEN '1' ELSE '0' END As hasChildren INTO #t2 FROM " _

+ " #t c " _

+ "SELECT SPACE(MLevel * 2) + MDesc + '(' + Mnemonic + ')' As Mnemonic FROM #t2 ORDER BY AccessCode " _





Set res.Query = qry

res.Execute         

res.LastRow

max = res.NumRows                                                           



For j = 1 To max                                                    

	Set doc = db.CreateDocument

	doc.Form = "TreeMonic" 

	res.CurrentRow = j                                                                

	doc.Mnemonic = res.GetValue("Mnemonic")

	

	Call doc.Save(False,False)

	

Next

res.Close(DB_CLOSE)             

End Sub

Subject: RE: SQL to work in Script Agent

You debugged it and it skipped those statements? You put a print statement in among those statements and it doesn’t print?

Subject: RE: SQL to work in Script Agent

I ran it through debug and it skipped those statements. Where would I put a print statement? Can you show me?

Subject: RE: SQL to work in Script Agent

I’ve been working with Notes since before LotusScript was introduced, and I’ve never heard of statements being skipped for no reason. It just doesn’t happen. Your weird formatting, with a _ at the end of a line followed by a blank line, I suppose could conceivably have confused the debugger about which line was which, though even that seems unlikely, but either the lines are being executed, or there’s some part of your code that you’re not showing.

Put a print statement at a point in the code where you want to guarantee that the statements at that point are executing. Heck, put a print statement before every line in that little block of four that you think it’s skipping. Print the numbers 1 thru 4.

If you have some error trapping code you’re not showing, comment it out. If there’s an error we want to see it.

Subject: RE: SQL to work in Script Agent

If I take the "_ and + " on the next line off Notes doesn’t like the code at all. Again, I was sent this code from a SQL programmer and just pasted it into Notes and ‘played’ with it to get Notes to at least let me save the code without errors. None of the code is missing, this is all of it. If I try to add print statements in the middle of the code - or after the first line - or wherever - Notes doesn’t like it and won’t save it. If I put it before this code then I do get my print statement, but can’t add it in the code anywhere.

Maybe this is bigger than me right now . . . I’ll keep playing with the code. Thanks!

Subject: SQL to work in Script Agent

Try this:

Write a little bit of LS in a button on a test form.

Dim msg as a sting.

Convert the query string to the variable msg

eg:

Dim Msg as String

Msg = "“select rtrim(code_value) as Mnemonic, description as AccessCode,”_ plus the rest of it.

See what happens. You’ll most likely find that there is a defect in the way that huge string is written and, once you find the typo, problem solved.

Enjoy.

Doug

Subject: RE: SQL to work in Script Agent

Thanks Doug, but this did nothing different. In debug it is acting like there just isn’t any code.

Subject: RE: SQL to work in Script Agent

Then you’re not doing what I suggested…if you do what I said, the code won’t even compile (at least in 6.54). The statement is not written properly. Hint, you can use the underscore character to tell LS ‘continue on next line’. A sting should never END in an underscore…

LOOK at the string really carefully…

Subject: SQL to work in Script Agent

Get rid of that trailing underscore. It shouldn’t even compile with that in there, but after a string that huge who knows what will happen. Maybe the compiler just gave up and let it through.

  • " #t c " _

  • "SELECT SPACE(MLevel * 2) + MDesc + ‘(’ + Mnemonic + ‘)’ As Mnemonic FROM #t2 ORDER BY AccessCode " _ ←

Set res.Query = qry

res.Execute

You should use the & instead of + for string concatenation. The + is overloaded for addition (numbers) and concatenation (strings). It’s a little more efficient to use the & so the compiler does not have to check your operands to see which flavor it’s supposed to use.

Subject: RE: SQL to work in Script Agent

Thomas - I have changed the + to & and removed the _ from the end. When I run through debug now, it jumps directly to the last line of the string& "Select Space(MLevel * 2) + . . . .

and I still get no result. Do you see anything else wrong in the code?

'Load cbudg from AIS warehouse - ibiscodesets - caccess1

'for Mnemonics field on DonorInfo subform

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase, codedb As NotesDatabase

Dim con As ODBCConnection

Dim qry As ODBCQuery

Dim res As ODBCResultSet

Dim idView As NotesView, codeview As NotesView

Dim doc As NotesDocument, iddoc As NotesDocument

Dim max As Double, id As String

Dim i As Double, j As Double

Dim bigArray() As String

Dim newone As String



Set db = session.CurrentDatabase

Set idView=db.getView("(copyofMNIC)")

’ warehouse control

Dim wh_db As New NotesDatabase("","")

Dim wh_doc As NotesDocument

Dim wh_view As notesview

Set wh_db=session.getdatabase("DDAS2/Notes","warehousctacs.nsf")

Set wh_view=wh_db.getview("AISCT")

Dim c1 As String, c2 As String, c3 As String, c4 As String

Set wh_doc = wh_view.Getfirstdocument

If Not wh_Doc Is Nothing Then

	c1 = wh_doc.ColumnValues(0)

	c2 = wh_doc.ColumnValues(1)

	c3 = wh_doc.ColumnValues(2)

	c4 = wh_doc.ColumnValues(3)

End If



Set con = New ODBCConnection

Set qry = New ODBCQuery

Set res =  New ODBCResultSet

Call con.connectTo(c1,c2,c3,c4)

Set qry.Connection = con



qry.SQL = "select rtrim(code_value) as Mnemonic, description as AccessCode, " _

& "rtrim(case"_

& "   if description like '__00000000000' then substring(description, 1, 2) + '%' "_

& "   if description like '____000000000' then substring(description, 1, 4) + '%' "_

& "   if description like '_____00000000' then substring(description, 1, 5) + '%' "_

& "   if description like '______0000000' then substring(description, 1, 6) + '%' "_

& "   if description like '_________0000' then substring(description, 1, 9) + '%' "_

& "   if description like '__________000' then substring(description, 1, 10) + '%' "_

& "  else description "_

& " end) as AccessFilter, "_

& " rtrim(cast(text1 as char(30))) as MDesc,  0 as MLevel"_

& " into #t "_

& " from	dbo.caccess1"_

& "where getdate() between begin_date and end_date" _

& "SELECT c.Mnemonic, c.AccessCode, c.AccessFilter, c.MDesc," _

& " (SELECT COUNT(*) FROM #t p WHERE c.AccessCode <> p.AccessCode and c.AccessCode like p.AccessFilter) As MLevel, "_

& "  (SELECT TOP 1 p.Mnemonic FROM #t p WHERE c.AccessCode <> p.AccessCode and c.AccessCode like p.AccessFilter ORDER BY p.AccessCode DESC) As Parent, " _

& "CASE WHEN Exists (SELECT * FROM #t p WHERE c.AccessCode <> p.AccessCode and p.AccessCode like c.AccessFilter) THEN '1' ELSE '0' END As hasChildren INTO #t2 FROM " _

& " #t c " _

& "SELECT SPACE(MLevel * 2) + MDesc + '(' + Mnemonic + ')' As Mnemonic FROM #t2 ORDER BY AccessCode " 



Set res.Query = qry

res.Execute         

res.LastRow

max = res.NumRows                                                           



For j = 1 To max                                                    

	Set doc = db.CreateDocument

	doc.Form = "TreeMonic" 

	res.CurrentRow = j                                                                

	doc.Mnemonic = res.GetValue("Mnemonic")

	

	Call doc.Save(False,False)

	

Next

res.Close(DB_CLOSE)             

End Sub

Subject: RE: SQL to work in Script Agent

The debugger will stop once on each statement of code as is about to execute that statement – not on each line of a multi-line statement. If a statement is 23 lines long, it will stop once in those 23 lines, pointing to the last line of the statement. This is working as expected.

You never said what happened when you put in print statements as I suggested. I’m very sure, unless there’s some code you aren’t showing, that it is executing every statement. You say you “get no result” – that’s not true. Time moved on past the point where you were running the agent, so you got a result. You just didn’t get the result you wanted. But since you don’t describe in detail the result you get, it’s hard for us to know what’s going wrong.

I assume what you mean by “got no result” is that max ends up being 0. To me, this sounds consistent with no records in the database matching your query. Rather than assuming this represents a failure of the language to execute your statements, or the failure of a method to work as documented, it’s generally more productive to start by checking your inputs. Do you have a way to check the exact query your code is generating to see what result set it does return? Some other tool where you can paste in the query and see the result? Not the query you started out with and turned into a Lotusscript statement – the actual string generated by your code. Use Msgbox to display it and you can copy it to the clipboard from there using Ctrl+C.

BTW It’s unnecessary to load all the records to count how many there are, before you read all the records again to process them. You don’t need to know how many records there are in advance. Just read the first record, and process it, and read the next one, and so on until you run out. That will be faster – once you get the rest of it working.

Subject: RE: SQL to work in Script Agent

Actually, that’s not true. The & operator forces a Cstr on the operands, so it wastes time doing an unnecessary cast if all of the operands are strings. The + operator is more efficient if you are working with strings. (And if you are doing a lot of concatenation over many lines of code – in a loop, say – it’s more efficient still to create a temporary rich text item and use the AppendText method, then take the text of the item when you’re done.)

Subject: RE: SQL to work in Script Agent

I just did a timing test and didn’t find any difference between performance of + and &, neither when working with variables Dim’ed as strings, nor with variants. If there’s a difference, it’s too small to measure easily, 1/100,000th of a second or less. The increased readability and decreased chance of error of making your intention clear with &, is well worth the performance cost if any.

Plus, the operation of & is predictable whereas when using + for concatenation, the result is not always obvious unless you refer to the variable declarations. Consider the following:

a = 7

b = “8”

print b + a

Does this print 15 or 87? Would you believe it depends whether b is declared as string or variant? Try it and see.

Then there’s always the chance that the datatype of what you’re concatenating might be different from what you expected (say it comes from a field and the form has been edited since you wrote your agent), resulting in a type mismatch if you use +, versus reasonable operation if you use &.

Subject: RE: SQL to work in Script Agent

Jeez, I wish you guys would leave the machine alone sometimes – my “authoritative” reply came about as the result of going through this same [expletive deleted] question with Thomas Gumz sometime in the 6.0.x time frame.

Subject: IT work is all about change, dude.

Subject: RE: SQL to work in Script Agent

When I run the code in the SQL Query Analyzer I get a result of 2157 records in the tree structure that I am after.

What I need to do with this code is run it so it displays on a form that I can display in a view to then have as a selection for our end-users. The user requesting the project wants the list in the tree-structured look that I get when running in SQL.

If I’m getting any type of result then where do I see it?