simplify

Hello,

I am making a script to link data to a database. My script runs very well but I think it could be better and faster.

This is the complex part:


tell application "Adobe InDesign CS5"
			tell EasyCatalog object
				tell DataSource DataBron
					set s to 1
					set theSqlStatement to SqlQuery
					repeat until s > number of theQuerynums
						if s = 1 then
							set theSqlStatement to theSqlStatement & " WHERE"
						end if
						if s > 1 then
							set theSqlStatement to theSqlStatement & " OR"
						end if
						set theQueryCode to item s of theQuerynums
						set Prodcode to get (characters 1 thru 4 of theQueryCode)
						set whereStatement to " sap_code like '" & Prodcode & "" & "%' "
						set theSqlStatement to theSqlStatement & whereStatement
						set s to s + 1
					end repeat
					odbc connection statement theSqlStatement
					synchronize with data source
				end tell
				try
					tell DataSource DataBron
						purge deleted
					end tell
				end try
			end tell

The problem is that “theQueryCode” can be the same, so the final statement could be
… where sap_code like ‘SENW.%’ or sap_code like ‘SENW.%’ or sap_code like ‘SENW.%’ or sap_code like ‘SENW.%’ or sap_code like ‘SENW.%’

My question is: Is it possible to write some lines to detect if there are same “theQueryCode” so the whereStatement doesn’t has to repeat the same “theQueryCode”?

I hope my question is clear :rolleyes:

thanks in advanced

Hi, I would keep a list of things I added to the query, then if a new item was not in the list then I add it to the query… something like this…

set tempList to {}
repeat until s > number of theQuerynums
	set theQueryCode to item s of theQuerynums
	set Prodcode to text 1 thru 4 of theQueryCode
	
	if Prodcode is not in tempList then
		set end of tempList to Prodcode
		
		if s = 1 then
			set theSqlStatement to theSqlStatement & " WHERE"
		end if
		if s > 1 then
			set theSqlStatement to theSqlStatement & " OR"
		end if
		
		set whereStatement to " sap_code like '" & Prodcode & "" & "%' "
		set theSqlStatement to theSqlStatement & whereStatement
	end if
	
	set s to s + 1
end repeat

Thanks

I will try it on monday. Ik hope it will work. :slight_smile: