<?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 Write Data from Data Table to Database in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Write-Data-from-Data-Table-to-Database/m-p/783412#M96772</link>
    <description>&lt;P&gt;Hello JMP-Community,&lt;/P&gt;&lt;P&gt;I am searching your help here because I didn't found a sufficent solution for myself in other posts. It is about writing content of a data table into a table of a database.&lt;/P&gt;&lt;P&gt;For that I wrote this little function:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;myInsertInto = function({dt, dbConnection, destinationName},
	cols = dt &amp;lt;&amp;lt; GetColumnNames(String);
	rows = dt &amp;lt;&amp;lt; Select All Rows &amp;lt;&amp;lt; GetSelectedRows;
	show(cols, rows);

	//prepare sql head
	sqlHead = "Insert Into " || destinationName || " (";
	for each({col, index}, cols,
		if(index == Length(cols),
			sqlHead = sqlHead || col ||")",
			sqlHead = sqlHead || col ||",";
		)
		
	);

	//prepare sql body
	sqlBody = "Values\!n";
	ForEach({row, index}, rows,
		sqlBody = sqlBody || "(";
		show(row);
		values = dt[row,0];
		show(values);

		for each({val, index}, values,
			if(type(val) == "String",
				if(IsMissing(val), 
					tmpVal = "null",
					tmpVal = "'"||val||"'"
				)
				,
				if(IsMissing(val), 
					tmpVal = "null",
					tmpVal = char(val)
				)
			);
			if(index == Length(values),
				sqlBody = sqlBody || tmpVal ||")",
				sqlBody = sqlBody || tmpVal ||",";
			)
		);

		if(index == Length(rows),
			sqlBody = sqlBody || ";",
			sqlBody = sqlBody || ",\!n"
		);

	);

	sqlQurey = sqlHead || "\!n" || sqlBody;

	New SQL Query(
		Version( 130 ),
		Connection( myDbHandle ),
		QueryName( "messwert.test_struktur_dennis" ),
		Custom SQL(
			Substitute(sqlQurey,"\!n", "", "\!t", "")
		)
	)&amp;lt;&amp;lt; run;

	return(sqlQurey)
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The function does what it should, but there are limitations that I don't know how to solve. For example, we are using a postgresql database in which tables have timestamp, date or other formatted-columns. I don't have an idea how to include that into my function, because JMP is seeing all these formatted types as a number.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another issue is to handle data duplicates. With my function the content of a data table would be blindly inserted into our database. That can lead to duplicates, because I don't know what is already there. I found in another post this function:&lt;/P&gt;&lt;P&gt;dt &amp;lt;&amp;lt; Save Database(&lt;SPAN&gt;&amp;nbsp;dbc,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; "dbo.fake",&lt;/P&gt;&lt;P&gt;&amp;nbsp; Replace&lt;/P&gt;&lt;P&gt;);&lt;/P&gt;&lt;P&gt;Unfortunately, that would replace my whole table, which i can not use.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Therefore I'd like to ask the community for help. Does anyone know about an add-in or a script solution, that solves these issues?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 22 Aug 2024 13:38:48 GMT</pubDate>
    <dc:creator>DBo_94</dc:creator>
    <dc:date>2024-08-22T13:38:48Z</dc:date>
    <item>
      <title>Write Data from Data Table to Database</title>
      <link>https://community.jmp.com/t5/Discussions/Write-Data-from-Data-Table-to-Database/m-p/783412#M96772</link>
      <description>&lt;P&gt;Hello JMP-Community,&lt;/P&gt;&lt;P&gt;I am searching your help here because I didn't found a sufficent solution for myself in other posts. It is about writing content of a data table into a table of a database.&lt;/P&gt;&lt;P&gt;For that I wrote this little function:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;myInsertInto = function({dt, dbConnection, destinationName},
	cols = dt &amp;lt;&amp;lt; GetColumnNames(String);
	rows = dt &amp;lt;&amp;lt; Select All Rows &amp;lt;&amp;lt; GetSelectedRows;
	show(cols, rows);

	//prepare sql head
	sqlHead = "Insert Into " || destinationName || " (";
	for each({col, index}, cols,
		if(index == Length(cols),
			sqlHead = sqlHead || col ||")",
			sqlHead = sqlHead || col ||",";
		)
		
	);

	//prepare sql body
	sqlBody = "Values\!n";
	ForEach({row, index}, rows,
		sqlBody = sqlBody || "(";
		show(row);
		values = dt[row,0];
		show(values);

		for each({val, index}, values,
			if(type(val) == "String",
				if(IsMissing(val), 
					tmpVal = "null",
					tmpVal = "'"||val||"'"
				)
				,
				if(IsMissing(val), 
					tmpVal = "null",
					tmpVal = char(val)
				)
			);
			if(index == Length(values),
				sqlBody = sqlBody || tmpVal ||")",
				sqlBody = sqlBody || tmpVal ||",";
			)
		);

		if(index == Length(rows),
			sqlBody = sqlBody || ";",
			sqlBody = sqlBody || ",\!n"
		);

	);

	sqlQurey = sqlHead || "\!n" || sqlBody;

	New SQL Query(
		Version( 130 ),
		Connection( myDbHandle ),
		QueryName( "messwert.test_struktur_dennis" ),
		Custom SQL(
			Substitute(sqlQurey,"\!n", "", "\!t", "")
		)
	)&amp;lt;&amp;lt; run;

	return(sqlQurey)
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The function does what it should, but there are limitations that I don't know how to solve. For example, we are using a postgresql database in which tables have timestamp, date or other formatted-columns. I don't have an idea how to include that into my function, because JMP is seeing all these formatted types as a number.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another issue is to handle data duplicates. With my function the content of a data table would be blindly inserted into our database. That can lead to duplicates, because I don't know what is already there. I found in another post this function:&lt;/P&gt;&lt;P&gt;dt &amp;lt;&amp;lt; Save Database(&lt;SPAN&gt;&amp;nbsp;dbc,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; "dbo.fake",&lt;/P&gt;&lt;P&gt;&amp;nbsp; Replace&lt;/P&gt;&lt;P&gt;);&lt;/P&gt;&lt;P&gt;Unfortunately, that would replace my whole table, which i can not use.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Therefore I'd like to ask the community for help. Does anyone know about an add-in or a script solution, that solves these issues?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2024 13:38:48 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Write-Data-from-Data-Table-to-Database/m-p/783412#M96772</guid>
      <dc:creator>DBo_94</dc:creator>
      <dc:date>2024-08-22T13:38:48Z</dc:date>
    </item>
  </channel>
</rss>

