<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Art Of Creation - Dynamics AX Blog &#187; SQL</title>
	<atom:link href="http://www.artofcreation.be/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artofcreation.be</link>
	<description>The everyday life of a Dynamics AX developer</description>
	<lastBuildDate>Thu, 02 Feb 2012 12:20:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Linked server sql statements</title>
		<link>http://www.artofcreation.be/2010/07/23/linked-server-sql-statements/</link>
		<comments>http://www.artofcreation.be/2010/07/23/linked-server-sql-statements/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 16:29:55 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Linked server]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=826</guid>
		<description><![CDATA[Hi all, hope you are well. Some time ago, I talked about executing direct sql statements, and now I want to share some sql statements that I used to manage linked server connections. What follows are 4 sql statements that allow you to add en remove linked servers on a database at runtime. Some assumptions: [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all, hope you are well. </p>
<p>Some time ago, I talked about <a href="http://www.artofcreation.be/2010/06/21/executing-direct-sql-statements/">executing direct sql statements</a>, and now I want to share some sql statements that I used to manage linked server connections. </p>
<p>What follows are 4 sql statements that allow you to add en remove linked servers on a database at runtime. </p>
<p>Some assumptions:<br />
- There is a str variable named &#8220;query&#8221; that will contain the query<br />
- there is a parm method on the class that return the sql server (&#8220;server&#8221; or &#8220;server\instance&#8221;)<br />
- there is a parm method that returns a username<br />
- there is a parm method that returns a password</p>
<p><strong>Check if linked server exist</strong><br />
First check if the linked server doesn&#8217;t exist yet, or you will get an error when you try to add one that already exists.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">query <span style="color: #00007f;">=</span> <span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;select top 1 * from sys.servers where name = '%1'&quot;</span><span style="color: #00007f;">,</span> this.<span style="color: #000000;">parmServer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div></div>
<p><strong>Add linked server</strong><br />
When the linked server doesn&#8217;t exist, add it.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">query <span style="color: #00007f;">=</span> <span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;EXEC sp_addLinkedServer @server = '%1', @srvproduct=N'SQL Server'&quot;</span><span style="color: #00007f;">,</span> this.<span style="color: #000000;">parmServer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div></div>
<p><strong>Add linked server login</strong><br />
Optionally, you can add a login that will be used to connect to the linked server.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">query <span style="color: #00007f;">=</span> <span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;sp_addlinkedsrvlogin @rmtsrvname = '%1' ,@useself = FALSE, @locallogin = NULL, @rmtuser = '%2', @rmtpassword = '%3'&quot;</span><span style="color: #00007f;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #000000;">parmServer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #000000;">parmUsername</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.<span style="color: #000000;">parmPassword</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div></div>
<p><strong>Remove linked server</strong><br />
Optionally, you can remove the linked server.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">query <span style="color: #00007f;">=</span> <span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">&quot;EXEC sp_dropserver '%1', 'droplogins'&quot;</span><span style="color: #00007f;">,</span> this.<span style="color: #000000;">parmServer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/07/23/linked-server-sql-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Executing direct SQL statements</title>
		<link>http://www.artofcreation.be/2010/06/21/executing-direct-sql-statements/</link>
		<comments>http://www.artofcreation.be/2010/06/21/executing-direct-sql-statements/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 16:35:22 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Datasource]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=796</guid>
		<description><![CDATA[Hi everyone, Today I want to talk about executing SQL statements in X++ on both the AX database and external databases. This is something probably every AX developer will have to do at some point. You&#8217;ll want to do this for many reasons; to execute stored procedures, to improve performance, to get data from an [...]]]></description>
			<content:encoded><![CDATA[<p>Hi everyone, </p>
<p>Today I want to talk about executing SQL statements in X++ on both the AX database and external databases. This is something probably every AX developer will have to do at some point.</p>
<p>You&#8217;ll want to do this for many reasons; to execute stored procedures, to improve performance, to get data from an external database, and so on. </p>
<p>I will provide samples for two classes:<br />
- Connection (Execute SQL statement on current AX database)<br />
- ODBCConnection (Execute SQL statement on external database)</p>
<p>I will not cover the ADO connection (CCADOConnection class), because it doesn&#8217;t work when you run it on server (or in batch), and I don&#8217;t like that. If you do, try to convince me ;-). </p>
<h3>Executing direct SQL on the current AX database</h3>
<p>When you execute a SQL statement, there are two options:<br />
- either you did a select and you expect a result to be returned<br />
- or you did insert/update/delete and you don&#8217;t expect a result. </p>
<p>The first sample is for a SQL statement that returns a result:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">server</span> <span style="color: #0000ff;">void</span> main<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Connection &nbsp; &nbsp; &nbsp;connection;<br />
&nbsp; &nbsp; Statement &nbsp; &nbsp; &nbsp; statement;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query;<br />
&nbsp; &nbsp; Resultset &nbsp; &nbsp; &nbsp; resultSet;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// create connection object</span><br />
&nbsp; &nbsp; connection <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> Connection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// create statement</span><br />
&nbsp; &nbsp; statement <span style="color: #00007f;">=</span> connection.<span style="color: #000000;">createStatement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// Set the SQL statement</span><br />
&nbsp; &nbsp; query <span style="color: #00007f;">=</span> <span style="color: #ff0000;">'select name from CustTable'</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// assert SQL statement execute permission</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">new</span> SqlStatementExecutePermission<span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// when the query returns result,</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// loop all results for processing</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">//BP Deviation documented</span><br />
&nbsp; &nbsp; resultSet <span style="color: #00007f;">=</span> statement.<span style="color: #000000;">executeQuery</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>resultSet.<span style="color: #0000ff;">next</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// do something with the result</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; info<span style="color: #000000;">&#40;</span>resultSet.<span style="color: #000000;">getString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// limit the scope of the assert call</span><br />
&nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">revertAssert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>Note: this is a main method, put it in a class. Also note that it has to run on server. </p>
<p>Now if you do an update/delete/insert, you will want to do something like this:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">server</span> <span style="color: #0000ff;">void</span> main<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Connection &nbsp; &nbsp; &nbsp;connection;<br />
&nbsp; &nbsp; Statement &nbsp; &nbsp; &nbsp; statement;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// create connection object</span><br />
&nbsp; &nbsp; connection <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> Connection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// create statement</span><br />
&nbsp; &nbsp; statement <span style="color: #00007f;">=</span> connection.<span style="color: #000000;">createStatement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// Set the SQL statement</span><br />
&nbsp; &nbsp; query <span style="color: #00007f;">=</span> <span style="color: #ff0000;">&quot;insert into CustTable (AccountNum, Name, RecId) values ('demo', 'demo', 2)&quot;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// assert SQL statement execute permission</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">new</span> SqlStatementExecutePermission<span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">//BP Deviation documented</span><br />
&nbsp; &nbsp; statement.<span style="color: #000000;">executeUpdate</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// limit the scope of the assert call</span><br />
&nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">revertAssert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>You can find more info about the executeQuery() and executeUpdate() methods on msdn:<br />
<a href="http://msdn.microsoft.com/en-us/library/aa861335.aspx">Statement Class</a></p>
<h3>Executing direct SQL on an external database using ODBC</h3>
<p>Again, we have to differentiate between queries that return a result and those that don&#8217;t. </p>
<p>The following code sample retrieves records from an external database and processes the result:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">server</span> <span style="color: #0000ff;">void</span> main<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Statement &nbsp; &nbsp; &nbsp; statement;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query;<br />
&nbsp; &nbsp; Resultset &nbsp; &nbsp; &nbsp; resultSet;<br />
&nbsp; &nbsp; LoginProperty &nbsp; loginProperty;<br />
&nbsp; &nbsp; OdbcConnection &nbsp;odbcConnection;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; loginProperty <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> LoginProperty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; loginProperty.<span style="color: #000000;">setDSN</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'YOURDSN'</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; odbcConnection <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> OdbcConnection<span style="color: #000000;">&#40;</span>loginProperty<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// Create new Statement instance</span><br />
&nbsp; &nbsp; statement <span style="color: #00007f;">=</span>odbcConnection.<span style="color: #000000;">CreateStatement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// Set the SQL statement</span><br />
&nbsp; &nbsp; query <span style="color: #00007f;">=</span> <span style="color: #ff0000;">'select name from CustTable'</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// assert SQL statement execute permission</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">new</span> SqlStatementExecutePermission<span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// when the query returns result,</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// loop all results for processing by handler</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">//BP Deviation documented</span><br />
&nbsp; &nbsp; resultSet <span style="color: #00007f;">=</span> statement.<span style="color: #000000;">executeQuery</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>resultSet.<span style="color: #0000ff;">next</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// do something with the result</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; info<span style="color: #000000;">&#40;</span>resultSet.<span style="color: #000000;">getString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// limit the scope of the assert call</span><br />
&nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">revertAssert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>As you can see, the code is pretty similar. The main difference is that we are using ODBC classes, including the LoginProperty class. </p>
<p>In this example, I use a DSN (Data Source Name) that I configured on the AOS server. The DSN contains a reference to the server and database you want to connect to, and also what user credentials should be used to connect to the database. This is a lot safer than storing them in AX.<br />
If you don&#8217;t know how to create a DSN, there are plenty of <a href="http://www.google.com/search?q=create+ODBC+Data+Source+Name">tutorials on the web</a>.</p>
<p>To update/delete/update, the code is more or less the same:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">server</span> <span style="color: #0000ff;">void</span> main<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Statement &nbsp; &nbsp; &nbsp; statement;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query;<br />
&nbsp; &nbsp; LoginProperty &nbsp; loginProperty;<br />
&nbsp; &nbsp; OdbcConnection &nbsp;odbcConnection;<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; loginProperty <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> LoginProperty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; loginProperty.<span style="color: #000000;">setDSN</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'YOURDSN'</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; odbcConnection <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> OdbcConnection<span style="color: #000000;">&#40;</span>loginProperty<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// Create new Statement instance</span><br />
&nbsp; &nbsp; statement <span style="color: #00007f;">=</span>odbcConnection.<span style="color: #000000;">CreateStatement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// Set the SQL statement</span><br />
&nbsp; &nbsp; query <span style="color: #00007f;">=</span> <span style="color: #ff0000;">&quot;insert into CustTable (AccountNum, Name, RecId) values ('demo', 'demo', 2)&quot;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// assert SQL statement execute permission</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">new</span> SqlStatementExecutePermission<span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span>.<span style="color: #000000;">assert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// when the query returns result,</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// loop all results for processing by handler</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">//BP Deviation documented</span><br />
&nbsp; &nbsp; statement.<span style="color: #000000;">executeUpdate</span><span style="color: #000000;">&#40;</span>query<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// limit the scope of the assert call</span><br />
&nbsp; &nbsp; CodeAccessPermission<span style="color: #00007f;">::</span><span style="color: #000000;">revertAssert</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>If you feel that something is missing in these examples, just ask. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/06/21/executing-direct-sql-statements/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Split and Add ranges on datasource</title>
		<link>http://www.artofcreation.be/2010/04/09/split-and-add-ranges-on-datasource/</link>
		<comments>http://www.artofcreation.be/2010/04/09/split-and-add-ranges-on-datasource/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 08:43:10 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[Datasource]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=653</guid>
		<description><![CDATA[When creating a query and adding ranges, you might notice that there is a limit on how the range string can be. This limit is defined by the extended data type Range that you can find in the AOT. You can try extending the size to a bigger stringsize, but you can&#8217;t go too high [...]]]></description>
			<content:encoded><![CDATA[<p>When creating a query and adding ranges, you might notice that there is a limit on how the range string can be. This limit is defined by the extended data type <em>Range</em> that you can find in the AOT. You can try extending the size to a bigger stringsize, but you can&#8217;t go too high before you run into limits that the sql database will have (I think this value is around 1700 characters). </p>
<p>What you can do is add multiple smaller ranges on the same field in stead of one long range. </p>
<p>I&#8217;ve written a method that splits a long range and adds it to you queryBuildDataSource:</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> splitAndAddRange<span style="color: #000000;">&#40;</span>QueryBuildDataSource &nbsp; _qbds<span style="color: #00007f;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldId &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _fieldId<span style="color: #00007f;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">str</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _sRange<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; #AOT<br />
&nbsp; &nbsp; <span style="color: #007f00;">// used to split and build ranges</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; splitPos; &nbsp; <span style="color: #007f00;">// max position for splitting</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cPos; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// position of ','</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fRange; &nbsp; &nbsp; <span style="color: #007f00;">// first part of the range</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lRange; &nbsp; &nbsp; <span style="color: #007f00;">// last part of the range</span><br />
&nbsp; &nbsp; QueryBuildRange &nbsp; &nbsp; qbr; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #007f00;">// the extra qbr if needed</span><br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// define max lenght</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// note: make this function none static and get the stringsize property only once while initialising your class</span><br />
&nbsp; &nbsp; <span style="color: #007f00;">// to speed up performance</span><br />
&nbsp; &nbsp; splitPos <span style="color: #00007f;">=</span> treenode<span style="color: #00007f;">::</span><span style="color: #000000;">findNode</span><span style="color: #000000;">&#40;</span>#ExtendedDataTypesPath <span style="color: #00007f;">+</span> #AOTRootPath <span style="color: #00007f;">+</span> <span style="color: #ff0000;">'Range'</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">AOTgetProperty</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'StringSize'</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #007f00;">// define range string</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007f00;">// init cPos</span><br />
&nbsp; &nbsp; cPos <span style="color: #00007f;">=</span> <span style="color: #00007f;">-</span><span style="color: #000000;">1</span>;<br />
&nbsp; &nbsp; <span style="color: #007f00;">// if range is too long -&gt; split (3 should be 1500)</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">strlen</span><span style="color: #000000;">&#40;</span>_sRange<span style="color: #000000;">&#41;</span> <span style="color: #00007f;">&gt;</span> splitPos<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// while no comma found</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span>cPos <span style="color: #00007f;">==</span> <span style="color: #00007f;">-</span><span style="color: #000000;">1</span> <span style="color: #00007f;">&amp;&amp;</span> splitPos <span style="color: #00007f;">&gt;</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// if char at current position is ',', split position found</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">substr</span><span style="color: #000000;">&#40;</span>_sRange<span style="color: #00007f;">,</span> splitPos<span style="color: #00007f;">,</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #00007f;">==</span> <span style="color: #ff0000;">','</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cPos <span style="color: #00007f;">=</span> splitPos;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// else try prev char</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; splitPos<span style="color: #00007f;">--</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// get the first part of the range</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fRange <span style="color: #00007f;">=</span> <span style="color: #0000ff;">substr</span><span style="color: #000000;">&#40;</span>_sRange<span style="color: #00007f;">,</span> <span style="color: #000000;">0</span><span style="color: #00007f;">,</span> splitPos <span style="color: #00007f;">-</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// make new range and add it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; qbr <span style="color: #00007f;">=</span> _qbds.<span style="color: #000000;">addRange</span><span style="color: #000000;">&#40;</span>_fieldId<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; qbr.<span style="color: #000000;">value</span><span style="color: #000000;">&#40;</span>fRange<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// get the remaining part of the range</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lRange <span style="color: #00007f;">=</span> <span style="color: #0000ff;">substr</span><span style="color: #000000;">&#40;</span>_sRange<span style="color: #00007f;">,</span> splitpos<span style="color: #00007f;">,</span> <span style="color: #0000ff;">strlen</span><span style="color: #000000;">&#40;</span>_sRange<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// split the rest of the range</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">strlen</span><span style="color: #000000;">&#40;</span>lRange<span style="color: #000000;">&#41;</span> <span style="color: #00007f;">&gt;</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KLFSplitAndAddRangeTest<span style="color: #00007f;">::</span><span style="color: #000000;">splitAndAddRange</span><span style="color: #000000;">&#40;</span>_qbds<span style="color: #00007f;">,</span> _fieldId<span style="color: #00007f;">,</span> lRange<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007f00;">// range is not too long, don't split, just add</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; qbr <span style="color: #00007f;">=</span> _qbds.<span style="color: #000000;">addRange</span><span style="color: #000000;">&#40;</span>_fieldId<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; qbr.<span style="color: #000000;">value</span><span style="color: #000000;">&#40;</span>_sRange<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>When you put this method in a class along with the following one, you can test the method.</p>
<div class="codecolorer-container xpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main<span style="color: #000000;">&#40;</span>Args _args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Query &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query;<br />
&nbsp; &nbsp; QueryBuildDataSource &nbsp; &nbsp;qbds;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">str</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accountNumRange;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #007f00;">// used for info-ing</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> rangeLength;<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> rangeLengthAOT;<br />
&nbsp; &nbsp; #AOT<br />
&nbsp; &nbsp; ;<br />
<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'A0000000001,A0000000002,A0000000003,A0000000004,A0000000005,A0000000006,A0000000007,A0000000008'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000009,A0000000010,A0000000011,A0000000012,A0000000013,A0000000014,A0000000015,A0000000016'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000017,A0000000018,A0000000019,A0000000020,A0000000021,A0000000022,A0000000023,A0000000024'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000025,A0000000026,A0000000027,A0000000028,A0000000029,A0000000030,A0000000031,A0000000032'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000033,A0000000034,A0000000035,A0000000036,A0000000037,A0000000038,A0000000039,A0000000040'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000041,A0000000042,A0000000043,A0000000044,A0000000045,A0000000046,A0000000047,A0000000048'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000049,A0000000050,A0000000051,A0000000052,A0000000053,A0000000054,A0000000055,A0000000056'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000057,A0000000058,A0000000059,A0000000060,A0000000061,A0000000062,A0000000063,A0000000064'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000065,A0000000066,A0000000067,A0000000068,A0000000069,A0000000070,A0000000071,A0000000072'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000073,A0000000074,A0000000075,A0000000076,A0000000077,A0000000078,A0000000079,A0000000080'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000081,A0000000082,A0000000083,A0000000084,A0000000085,A0000000086,A0000000087,A0000000088'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000089,A0000000090,A0000000091,A0000000092,A0000000093,A0000000094,A0000000095,A0000000096'</span>;<br />
&nbsp; &nbsp; accountNumRange <span style="color: #00007f;">+=</span> <span style="color: #ff0000;">'0000000097,A0000000098,A0000000099,A0000000100'</span>;<br />
<br />
&nbsp; &nbsp; query <span style="color: #00007f;">=</span> <span style="color: #0000ff;">new</span> Query<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; rangeLength <span style="color: #00007f;">=</span> <span style="color: #0000ff;">strlen</span><span style="color: #000000;">&#40;</span>accountNumRange<span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; rangeLengthAOT <span style="color: #00007f;">=</span> treenode<span style="color: #00007f;">::</span><span style="color: #000000;">findNode</span><span style="color: #000000;">&#40;</span>#ExtendedDataTypesPath <span style="color: #00007f;">+</span> #AOTRootPath <span style="color: #00007f;">+</span> <span style="color: #ff0000;">'Range'</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">AOTgetProperty</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'StringSize'</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'Range length: %1'</span><span style="color: #00007f;">,</span>rangeLength<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'range length AOT: %1'</span><span style="color: #00007f;">,</span> rangeLengthAOT<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'There should be %1 ranges'</span><span style="color: #00007f;">,</span> roundup<span style="color: #000000;">&#40;</span>rangeLength <span style="color: #00007f;">/</span> rangeLengthAOT<span style="color: #00007f;">,</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; qbds <span style="color: #00007f;">=</span> Query.<span style="color: #000000;">addDataSource</span><span style="color: #000000;">&#40;</span><span style="color: #0000ff;">tablenum</span><span style="color: #000000;">&#40;</span>CustTable<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; KLFSplitAndAddRangeTest<span style="color: #00007f;">::</span><span style="color: #000000;">splitAndAddRange</span><span style="color: #000000;">&#40;</span>qbds<span style="color: #00007f;">,</span> <span style="color: #0000ff;">fieldnum</span><span style="color: #000000;">&#40;</span>CustTable<span style="color: #00007f;">,</span> AccountNum<span style="color: #000000;">&#41;</span><span style="color: #00007f;">,</span> accountNumRange<span style="color: #000000;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; info<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">strfmt</span><span style="color: #000000;">&#40;</span><span style="color: #ff0000;">'Range count: %1'</span><span style="color: #00007f;">,</span>qbds.<span style="color: #000000;">rangeCount</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div></div>
<p>The output is:</p>
<blockquote><p>Range length: 1175<br />
range length AOT: 250<br />
There should be 5,00 ranges<br />
Range count: 5</p></blockquote>
<p>Using a tool I have I can also print out the query for a closer look:<br />
<a href="http://www.artofcreation.be/wp-content/uploads/querydump.png"><img src="http://www.artofcreation.be/wp-content/uploads/querydump-300x247.png" alt="Query dump" title="Query dump" width="300" height="247" class="aligncenter size-medium wp-image-661" /></a><br />
You can clearly see that the 5 ranges have been added. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/04/09/split-and-add-ranges-on-datasource/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rename an AX company on SQL</title>
		<link>http://www.artofcreation.be/2010/02/24/rename-an-ax-company-on-sql/</link>
		<comments>http://www.artofcreation.be/2010/02/24/rename-an-ax-company-on-sql/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 21:57:25 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=530</guid>
		<description><![CDATA[A while ago, I talked about deleting an AX company on SQL, but of course, you can also use the sp_MSforeachtable stored procedure to rename a company (= change the DataAreaId of a company). In the next example, I rename CEE to CEA: exec sp_MSforeachtable 'update ? set DataAreaID = &#34;CEA&#34; where ?.DataAreaID = &#34;CEE&#34;' [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I talked about <a href="http://www.artofcreation.be/2010/02/03/delete-an-ax-company-on-sql/">deleting an AX company on SQL</a>, but of course, you can also use the sp_MSforeachtable stored procedure to rename a company (= change the DataAreaId of a company).</p>
<p>In the next example, I rename CEE to CEA:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">exec sp_MSforeachtable <span style="color: #ff0000;">'update ? set DataAreaID = &quot;CEA&quot; where ?.DataAreaID = &quot;CEE&quot;'</span><br />
<br />
<span style="color: #993333; font-weight: bold;">UPDATE</span> DataArea <span style="color: #993333; font-weight: bold;">SET</span> ID <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'CEA'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> DataArea<span style="color: #66cc66;">.</span>ID <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'CEE'</span><br />
<span style="color: #993333; font-weight: bold;">UPDATE</span> CompanyDomainList <span style="color: #993333; font-weight: bold;">SET</span> CompanyID <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'CEA'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> CompanyID <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'CEE'</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/02/24/rename-an-ax-company-on-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delete an AX company on SQL</title>
		<link>http://www.artofcreation.be/2010/02/03/delete-an-ax-company-on-sql/</link>
		<comments>http://www.artofcreation.be/2010/02/03/delete-an-ax-company-on-sql/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 20:03:53 +0000</pubDate>
		<dc:creator>Klaas Deforche</dc:creator>
				<category><![CDATA[Dynamics AX]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.artofcreation.be/?p=432</guid>
		<description><![CDATA[This week, we were shrinking a database of a development environment by deleting some companies. Here a nice little SQL statement that uses the sp_MSforeachtable stored procedure to delete all records of a specific company (CEU in this case) from all tables. exec sp_MSforeachtable 'delete from ? where ?.DataAreaID = &#34;CEU&#34;' Certainly fast(er than AX) [...]]]></description>
			<content:encoded><![CDATA[<p>This week, we were shrinking a database of a development environment by deleting some companies.<br />
Here a nice little SQL statement that uses the sp_MSforeachtable stored procedure to delete all records of a specific company (CEU in this case) from all tables.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">exec sp_MSforeachtable <span style="color: #ff0000;">'delete from ? where ?.DataAreaID = &quot;CEU&quot;'</span></div></div>
<p>Certainly fast(er than AX) and gets the job done.<br />
Use at your own risk ;-)</p>
<p><strong>Update:</strong><br />
You&#8217;ll want do do some cleaning up to:<br />
delete the company id from the DataArea table and from the CompanyDomainList table</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> DataArea <span style="color: #993333; font-weight: bold;">WHERE</span> DataArea<span style="color: #66cc66;">.</span>ID <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'CEU'</span><br />
<span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> CompanyDomainList <span style="color: #993333; font-weight: bold;">WHERE</span> CompanyDomainList<span style="color: #66cc66;">.</span>CompanyID <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'CEU'</span></div></div>
<p><strong>Update 2:</strong><br />
When you want to delete all companies execpt a few (like DAT), just use this:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">exec sp_MSforeachtable <span style="color: #ff0000;">'delete from ? where ?.DataAreaID &lt;&gt; &quot;DAT&quot; AND ?.DataAreaID &lt;&gt; &quot;DEMO&quot;'</span><br />
<span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> DataArea <span style="color: #993333; font-weight: bold;">WHERE</span> DataArea<span style="color: #66cc66;">.</span>ID <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #ff0000;">'DAT'</span> <span style="color: #993333; font-weight: bold;">AND</span> DataArea<span style="color: #66cc66;">.</span>ID <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #ff0000;">'DEMO'</span><br />
<span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> CompanyDomainList <span style="color: #993333; font-weight: bold;">WHERE</span> CompanyDomainList<span style="color: #66cc66;">.</span>CompanyID <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #ff0000;">'DAT'</span> <span style="color: #993333; font-weight: bold;">AND</span> CompanyDomainList<span style="color: #66cc66;">.</span>CompanyID <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #ff0000;">'DEMO'</span></div></div>
<p>This will delete all companies except DAT en DEMO. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.artofcreation.be/2010/02/03/delete-an-ax-company-on-sql/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

