<?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>TechBlog &#187; SP</title>
	<atom:link href="http://techblog.byllemos.com/tag/sp/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.byllemos.com</link>
	<description>Accelerating into the Future with Wisdom about Technology! Ingrid Byllemos</description>
	<lastBuildDate>Tue, 05 Jan 2010 00:04:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Stored Procedures and Navision</title>
		<link>http://techblog.byllemos.com/2008/02/store-procedures-and-navision/</link>
		<comments>http://techblog.byllemos.com/2008/02/store-procedures-and-navision/#comments</comments>
		<pubDate>Wed, 20 Feb 2008 23:07:46 +0000</pubDate>
		<dc:creator>iby</dc:creator>
				<category><![CDATA[ADO]]></category>
		<category><![CDATA[Automations]]></category>
		<category><![CDATA[Navision]]></category>
		<category><![CDATA[SP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Stored Procedures]]></category>

		<guid isPermaLink="false">http://techblog.byllemos.com/?p=29</guid>
		<description><![CDATA[<p>If you are executing a specific SQL query very often, it would be usefully to have it as Stored Procedure on the SQL Server.</p>
<p>Stored Procedures is a collection of SQL statements.</p>
<p>There are 2 ways to create a store&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>If you are executing a specific SQL query very often, it would be usefully to have it as Stored Procedure on the SQL Server.</p>
<p>Stored Procedures is a collection of SQL statements.</p>
<p>There are 2 ways to create a store procedure:</p>
<ol>
<li>In the SQL Server Management Studio &#8211; you can right click on Stored Procedures and chose to create a new  Stored Procedure. It will then create a template for you, which you just have to fill out and execute</li>
<li>The second way is to build a script by yourself</li>
</ol>
<p><span id="more-29"></span><br />
A script to create a Stored Procedure could look like this:</p>
<blockquote><p> SET ANSI_NULLS ON<br />
GO<br />
SET QUOTED_IDENTIFIER ON<br />
GO<br />
&#8211; =============================================<br />
&#8211; Author:          Ingrid Byllemos<br />
&#8211; Create date:<br />
&#8211; Description:    Test of SP<br />
&#8211; =============================================<br />
CREATE PROCEDURE Count_Calls<br />
&#8211; Add the parameters for the stored procedure here<br />
@FromDate DateTime,<br />
@ToDate DateTime<br />
AS<br />
BEGIN<br />
&#8211; SET NOCOUNT ON added to prevent extra result sets from<br />
&#8211; interfering with SELECT statements.<br />
SET NOCOUNT ON;</p>
<p>&#8211; Insert statements for procedure here<br />
SELECT COUNT(*) FROM [Call]<br />
WHERE [Start Date] BETWEEN @FromDate and @ToDate</p>
<p>END<br />
GO</p></blockquote>
<p>When the script first is created, then simply execute it and a stored procedure is created on the SQL server.</p>
<p>Let us now take a look on how to use the stored procedure from Navision. Here we will be using <a href="http://techblog.byllemos.com/?p=28" title="ADO" target="_blank">ADO</a> as described in an earlier article.</p>
<p>It is a bit difference from earlier described. First you have to open the connection to the SQL server:</p>
<blockquote><p> CREATE(ADOConnection);</p>
<p>ConnectionString := <code>'</code>PROVIDER=SQLOLEDB;SERVER=SQLServer;DATABASE=SQLDatabase;<code>'</code>+<br />
<blankspace>&#8230;&#8230;&#8230;..</blankspace><code>'</code>UID=SQLUserID;PWD=SQLPwd<code>'</code>;<br />
ADOConnection.ConnectionString(ConnectionString);<br />
ADOConnection.Open;</p></blockquote>
<p>This you should already have known how to do. Now we have to setup and open a ADO Command.</p>
<p>First we create the ADO Command and sets the ActiveConnection. Notice we use a Variant to transfer the connection from ADOConnection to ADOCommand.</p>
<blockquote><p> CREATE(ADOCommand);</p>
<p>ActiveConnection := ADOConnection;<br />
//ActiveConnection is type Variant</p>
<p>ADOCommand.ActiveConnection := ActiveConnection;</p></blockquote>
<p>Next we have to setup the actual stored procedure, which has to  be executed:</p>
<blockquote><p> ADOCommand.CommandText := &#8216;[Count_Calls]&#8216;;<br />
ADOCommand.CommandType := 4;<br />
ADOCommand.CommandTimeout := 0;</p></blockquote>
<p>And the add Parameters to the command:</p>
<blockquote><p>ADOParameter := ADOCommand.CreateParameter(&#8216;@FromDate&#8217;,7,1,0,&#8217;05-01-2007&#8242;);<br />
ADOCommand.Parameters.Append(ADOParameter);</p>
<p>ADOParameter := ADOCommand.CreateParameter(&#8216;@ToDate&#8217;,7,1,0,&#8217;13-01-2007&#8242;);<br />
//(name = @ToDate,type = 7 (date), direction = 1 (in), size = 0 (default), value = &#8217;13-01-2007&#8242;)</p>
<p>ADOCommand.Parameters.Append(ADOParameter);</p></blockquote>
<p>Notice we are using CreateParameter to add the parameters to the command.</p>
<p>Now execute the command and fetch the query result into a recordset:</p>
<blockquote><p> ADOCommand.Execute;</p>
<p>CREATE(ADORecordset);<br />
ADORecordset.ActiveConnection := ADOConnection;<br />
ADORecordset.Open(ADOCommand);<br />
ADORecordset.MoveFirst;</p>
<p>MESSAGE(FORMAT(ADORecordset.Fields.Item(0).Value));</p></blockquote>
<p>Finally close the connection:</p>
<blockquote><p> ADORecordset.Close;<br />
ADOConnection.Close;<br />
CLEAR(ADOConnection);</p></blockquote>
<p>In the example we have been using the following automations:</p>
<ul>
<li> &#8216;Microsoft ActiveX Data Objects 2.8 Library&#8217;.Connection</li>
<li> &#8216;Microsoft ActiveX Data Objects 2.8 Library&#8217;.Command</li>
<li> &#8216;Microsoft ActiveX Data Objects 2.8 Library&#8217;.Parameter</li>
<li> &#8216;Microsoft ActiveX Data Objects Recordset 2.8 Library&#8217;.Recordset</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://techblog.byllemos.com/2008/02/store-procedures-and-navision/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

