<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Column value for SQL input in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218317#M43669</link>
    <description>&lt;P&gt;One more comment - you are using the LIKE operator but not using wildcards.&amp;nbsp; Perhaps you meant something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;"select vcLotCode, vcWaferCode, dAvg
   from fr.database
  where vcLotCode like '%^lotcode^%'");&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 22 Jul 2019 11:43:50 GMT</pubDate>
    <dc:creator>pmroz</dc:creator>
    <dc:date>2019-07-22T11:43:50Z</dc:date>
    <item>
      <title>Column value for SQL input</title>
      <link>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218173#M43628</link>
      <description>&lt;P&gt;I feel like this should be simple, but after trying various methods over the past few days, searching previous discussions, and referencing the scripting index, I've come up empty.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to loop through the values from the data table and use them as inputs for the SQL query, where the data table values are "lot codes" and the SQL query references "where vcLotCode like...".&amp;nbsp; I'll deal with the looping later, I'm struggling to get even the first value to work.&amp;nbsp; I've tried various iterations of "select", "set", "get", etc....&amp;nbsp; Any help is appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;// Data table

dt=New Table ("Lots",
	Add Rows(5),
	New Column ("vcLotCode",
		Character,
		Nominal,
		Set Values ({"lcode.0123","12345.4321","dcode.0123","54321.0123","lotcd.4567"})
	),
);

// One attempt to set lot code to values from table

dt&amp;lt;&amp;lt;Select Rows(1);
lotcode = :vcLotCode &amp;lt;&amp;lt;Get Selected;

// Attempting to pull data from database using lot code as lookup variable

DSNString = "Driver={SQL Server}; SERVER=rpt; DATABASE=FAB; UID=rrpt; ";

sqlStr=
"
select vcLotCode, vcWaferCode, dAvg
from fr.database
where vcLotCode like '"||lotcode||"'
";

dt2 = Eval(Substitute(Expr(dt=Open Database(DataSource,sqlcode,"Data");),Expr(sqlcode),sqlstr,Expr(DataSource),DSNString));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Jul 2019 20:37:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218173#M43628</guid>
      <dc:creator>smithwoosley</dc:creator>
      <dc:date>2019-07-19T20:37:26Z</dc:date>
    </item>
    <item>
      <title>Re: Column value for SQL input</title>
      <link>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218207#M43631</link>
      <description>&lt;P&gt;There are two points to consider. First, the code can be greatly simplified. This change will reduce the opportunity for errors and make them easier to find. Second, you are manipulating character strings for the arguments to Open Database() function. The double quotation mark is the delimiter for strings. Embedded double quotes must therefore be identified with the escape sequence "\!" (back slash, bang).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test this script. (I can't.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = New Table( "Lots",
	Add Rows( 5 ),
	New Column( "vcLotCode", Character, Nominal, Set Values( {"lcode.0123", "12345.4321", "dcode.0123", "54321.0123", "lotcd.4567"} ) )

);

For( r = 1, r &amp;lt;= N Row( dt ), r++,
	Eval(
		Parse(
			Substitute(
				"dt2 = Open Database(
					\!"Driver={SQL Server}; SERVER=rpt; DATABASE=FAB; UID=rrpt; \!",
					\!"select vcLotCode, vcWaferCode, dAvg from fr.database where vcLotCode like '\!"LLLLL\!"'\!",
					\!"Data\!"
				)",
				"LLLLL", dt:vcLotCode[r]
			)
		)
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 20 Jul 2019 13:00:19 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218207#M43631</guid>
      <dc:creator>Mark_Bailey</dc:creator>
      <dc:date>2019-07-20T13:00:19Z</dc:date>
    </item>
    <item>
      <title>Re: Column value for SQL input</title>
      <link>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218315#M43667</link>
      <description>&lt;P&gt;A few things:&lt;/P&gt;
&lt;P&gt;o You are using Open Database.&amp;nbsp; I prefer &lt;STRONG&gt;create database connection&lt;/STRONG&gt; + &lt;STRONG&gt;execute SQL&lt;/STRONG&gt; + &lt;STRONG&gt;close database connection&lt;/STRONG&gt;.&amp;nbsp; The advantage of &lt;STRONG&gt;execute SQL&lt;/STRONG&gt; is that you create one connection to the database and can execute multiple SQL statements with it.&amp;nbsp; Open database creates a new connection for every SQL statement&lt;/P&gt;
&lt;P&gt;o You don't need to use all of those evals, substitute etc.&amp;nbsp; In addition there are variables that are not defined like datasource and sqlcode.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;// Data table
dt = New Table( "Lots",
	Add Rows( 5 ),
	New Column( "vcLotCode",
		Character,
		Nominal,
		Set Values( {"lcode.0123", "12345.4321", "dcode.0123", "54321.0123", "lotcd.4567"} )
	),
);

// Pull data from database using lot code as lookup variable
DSNString = "Driver={SQL Server}; SERVER=rpt; DATABASE=FAB; UID=rrpt; ";

// Create a connection to SQL Server
dbc = create database connection(dsnstring);

// Loop over all lot numbers
for (i = 1, i &amp;lt;= nrows(dt), i++,
	lotcode = dt:vclotcode[i];
	sqlStr = evalinsert(
"select vcLotCode, vcWaferCode, dAvg
   from fr.database
  where vcLotCode like '^lotcode^'");
	print(sqlstr);

// Create a separate table for each lot number
	dt2 = execute sql(dbc, sqlstr);
);
close database connection(dbc);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Jul 2019 11:42:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218315#M43667</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2019-07-22T11:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: Column value for SQL input</title>
      <link>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218317#M43669</link>
      <description>&lt;P&gt;One more comment - you are using the LIKE operator but not using wildcards.&amp;nbsp; Perhaps you meant something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;"select vcLotCode, vcWaferCode, dAvg
   from fr.database
  where vcLotCode like '%^lotcode^%'");&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Jul 2019 11:43:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218317#M43669</guid>
      <dc:creator>pmroz</dc:creator>
      <dc:date>2019-07-22T11:43:50Z</dc:date>
    </item>
    <item>
      <title>Re: Column value for SQL input</title>
      <link>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218321#M43673</link>
      <description>&lt;P&gt;I agree that in this case, there is an advantage to breaking the three steps out into separate actions as you are interatiing over the queries.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using this approach, you could also just use Substitute() on the boilerplate sqlstr.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2019 12:00:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/218321#M43673</guid>
      <dc:creator>Mark_Bailey</dc:creator>
      <dc:date>2019-07-22T12:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: Column value for SQL input</title>
      <link>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/219782#M43975</link>
      <description>&lt;P&gt;Perfect.&amp;nbsp; Thank you so much!&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 19:17:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Column-value-for-SQL-input/m-p/219782#M43975</guid>
      <dc:creator>smithwoosley</dc:creator>
      <dc:date>2019-08-01T19:17:31Z</dc:date>
    </item>
  </channel>
</rss>

