<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Stored Procedure Optimization Tips &#8211; Best Practices</title>
	<atom:link href="http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Sun, 12 Feb 2012 09:22:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: janani</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-246898</link>
		<dc:creator><![CDATA[janani]]></dc:creator>
		<pubDate>Thu, 02 Feb 2012 04:20:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-246898</guid>
		<description><![CDATA[Thanks for the excellent Tips..]]></description>
		<content:encoded><![CDATA[<p>Thanks for the excellent Tips..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: conconada</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-243659</link>
		<dc:creator><![CDATA[conconada]]></dc:creator>
		<pubDate>Wed, 25 Jan 2012 21:47:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-243659</guid>
		<description><![CDATA[Hello Pinal Dave, thanks you  for your blog, is a very good source for me.

My cuestion is when I execute the select directly in the SP does it reuse the 
execution plan ?, like this

CREATE PROC dbo.ProcName @p1 int
AS
SET NOCOUNT ON;
--Procedure code here
SELECT column1 FROM dbo.TblTable1 where ProcID = @p1
-- Reset SET NOCOUNT to OFF
SET NOCOUNT OFF;
GO]]></description>
		<content:encoded><![CDATA[<p>Hello Pinal Dave, thanks you  for your blog, is a very good source for me.</p>
<p>My cuestion is when I execute the select directly in the SP does it reuse the<br />
execution plan ?, like this</p>
<p>CREATE PROC dbo.ProcName @p1 int<br />
AS<br />
SET NOCOUNT ON;<br />
&#8211;Procedure code here<br />
SELECT column1 FROM dbo.TblTable1 where ProcID = @p1<br />
&#8211; Reset SET NOCOUNT to OFF<br />
SET NOCOUNT OFF;<br />
GO</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-242774</link>
		<dc:creator><![CDATA[Michael]]></dc:creator>
		<pubDate>Mon, 23 Jan 2012 21:45:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-242774</guid>
		<description><![CDATA[Would you suggest using the schema name for all object references in a sproc including joins and conditional clauses?

For example:
SELECT b.BatchID, b.BatchName, bt.BatchTransactionID, bt.BatchTransactionName
FROM dbo.Batch
INNER JOIN dbo.BatchTrx WITH(NOLOCK) ON dbo.BatchTrx.BatchID = dbo.Batch.BatchID
WHERE dbo.Batch.StatusID = 1
AND dbo.BatchTrx.StatusID = 1]]></description>
		<content:encoded><![CDATA[<p>Would you suggest using the schema name for all object references in a sproc including joins and conditional clauses?</p>
<p>For example:<br />
SELECT b.BatchID, b.BatchName, bt.BatchTransactionID, bt.BatchTransactionName<br />
FROM dbo.Batch<br />
INNER JOIN dbo.BatchTrx WITH(NOLOCK) ON dbo.BatchTrx.BatchID = dbo.Batch.BatchID<br />
WHERE dbo.Batch.StatusID = 1<br />
AND dbo.BatchTrx.StatusID = 1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deb</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-236279</link>
		<dc:creator><![CDATA[Deb]]></dc:creator>
		<pubDate>Thu, 12 Jan 2012 23:24:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-236279</guid>
		<description><![CDATA[ugh..I have a complex stor proc that I could so use all of your help on.  It runs fine but takes over 8 hours to run!  Any suggestions would be so greatly appreciated.
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    /*
    IF OBJECT_ID(&#039;tempdb..#tempAgingRecords&#039;) IS NOT NULL 
	drop table #tempAgingRecords
	*/
	
	declare @itemid varchar(40),
		@dataareaid varchar(10)

	--Master records
	select distinct t4.product_number itemid,
		t3.company_id dataareaid,
		1 processed
	into #tempAgingRecords
	from edw.dbo.f_inventory_transaction t1
		left outer join edw.dbo.dim_inventory_trans_type t2 on
			t1.transaction_type_key = t2.transaction_type_key
		left outer join edw.dbo.dim_company t3 on
			t1.company_key = t3.company_key
		left outer join edw.dbo.dim_product t4 on
			t1.product_key = t4.product_key
		left outer join edw.dbo.dim_date t5 on
			t1.date_key = t5.date_key
	where --t2.transaction_type not in (&#039;Profit/Loss&#039;, &#039;Transfer&#039;, &#039;Quarantine Order&#039;, &#039;Transfer Order Shipment&#039;, &#039;Transfer Order Receive&#039;) 
		t2.issues_status in (&#039;None&#039;, &#039;Sold&#039;, &#039;Deducted&#039;, &#039;Picked&#039;)
		and t2.receipt_status in (&#039;None&#039;, &#039;Purchased&#039;, &#039;Received&#039;, &#039;Registered&#039;) 
		and t1.transaction_quantity &lt; 0
		--and t4.product_number = &#039;Meiban CM&#039;
		and t4.product_key is not null
		and t5.[date]  &#039;1/1/1900&#039;
	order by t4.product_number
	
	--Variables for aging process
	declare @negTable table (
		negDatestatus datetime,
		negQty decimal(28,12),
		negProcessed int,
		negRecid bigint,
		negInventoryAmount decimal(28,12)
		)

	declare @posTable table (
		posDatestatus datetime,
		posQty decimal(28,12),
		posProcessed int,
		posRecid bigint,
		posInventoryAmount decimal(28,12)
		)

	declare @negDatestatus datetime,
		@negQty decimal(28,12),
		@posDatestatus datetime,
		@posQty decimal(28,12),
		@negRecid bigint,
		@posRecid bigint,
		@negInventoryAmount decimal(28,12),
		@posInventoryAmount decimal(28,12)

	truncate table work_invent_trans_aging

	while (select sum(processed) from #tempAgingRecords)  0
		begin
			select @itemid = itemid, @dataareaid = dataareaid from #tempAgingRecords where processed  0
			
			insert into @negTable
			select t5.[date] datestatus,
				sum(t1.transaction_quantity) qty,
				1 processed,
				rec_id,
				sum(case when t3.company_id = &#039;rel&#039;
					then isnull(case when issues_status in (&#039;None&#039;, &#039;Sold&#039;, &#039;Deducted&#039;,&#039;Picked&#039;) 
						and receipt_status in (&#039;None&#039;, &#039;Purchased&#039;, &#039;Received&#039;, &#039;Registered&#039;)
						then t1.transaction_quantity * t4.unit_cost end, 0)
					else isnull(case when issues_status in (&#039;None&#039;, &#039;Sold&#039;, &#039;Deducted&#039;,&#039;Picked&#039;) 
						and receipt_status in (&#039;None&#039;, &#039;Purchased&#039;, &#039;Received&#039;, &#039;Registered&#039;)
						then t1.posted_cost_amount end, 0) +
						isnull(case when issues_status in (&#039;None&#039;, &#039;Sold&#039;, &#039;Deducted&#039;,&#039;Picked&#039;) 
						and receipt_status in (&#039;None&#039;, &#039;Purchased&#039;, &#039;Received&#039;, &#039;Registered&#039;)
						then t1.adjustment_cost_amount end, 0) +
						isnull(case when issues_status in (&#039;Deducted&#039;,&#039;Picked&#039;) 
						or receipt_status in (&#039;Received&#039;, &#039;Registered&#039;)
						then t1.physical_cost_amount end, 0) end) as InventoryAmount
			from edw.dbo.f_inventory_transaction t1
				left outer join edw.dbo.dim_inventory_trans_type t2 on
					t1.transaction_type_key = t2.transaction_type_key
				left outer join edw.dbo.dim_company t3 on
					t1.company_key = t3.company_key
				left outer join edw.dbo.dim_product t4 on
					t1.product_key = t4.product_key
				left outer join edw.dbo.dim_date t5 on
					t1.date_key = t5.date_key
			where t4.product_number = @itemid
				and t3.company_id = @dataareaid
				--and t2.transaction_type not in (&#039;Profit/Loss&#039;, &#039;Transfer&#039;, &#039;Quarantine Order&#039;, &#039;Transfer Order Shipment&#039;, &#039;Transfer Order Receive&#039;) 
				and t2.issues_status in (&#039;None&#039;, &#039;Sold&#039;, &#039;Deducted&#039;, &#039;Picked&#039;)
				and t2.receipt_status in (&#039;None&#039;, &#039;Purchased&#039;, &#039;Received&#039;, &#039;Registered&#039;) 
				and t1.transaction_quantity &lt; 0
				--and t4.product_number  is not null
				and t5.[date]  &#039;1/1/1900&#039;
			group by t5.[date],
				rec_id
			having sum(t1.transaction_quantity)  0
				--and t4.product_number  is not null
				and t5.[date]  &#039;1/1/1900&#039;
			group by t5.[date],
				rec_id
			having sum(t1.transaction_quantity) &gt; 0
			
			--Get the first positive quantity
			select @posRecid = posRecid, @posDatestatus = posDatestatus, @posQty = posQty, @posInventoryAmount = posInventoryAmount from @posTable p1 where posProcessed  0 order by posDatestatus desc, posRecid desc

			while (select isnull(sum(negProcessed), 0) from @negTable n1)  0
				begin
					--Get negative quantity
					select @negRecid = negRecid, @negDatestatus = negDatestatus, @negQty = negQty, @negInventoryAmount = negInventoryAmount from @negTable n2 where negProcessed  0 order by negDatestatus desc, negRecid desc
					
					if (select isnull(sum(posProcessed), 0) from @posTable) = 0
						begin
							--Insert negative value if there are no positive quantities
							insert into work_invent_trans_aging
							values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, &#039;1/1/1900&#039;, @negQty, &#039;New1&#039;, @negInventoryAmount)
							
							set @negQty = 0
							set @negInventoryAmount = 0
						end
					else
						begin
							if @posQty &gt; (@negQty * -1)
								begin
									--Write into result table the consumed negative amount
									insert into work_invent_trans_aging
									values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, @posDatestatus, @negQty, &#039;Original&#039;, @negInventoryAmount)
									
									set @posQty = @posQty + @negQty
									set @posInventoryAmount = @posInventoryAmount + @negInventoryAmount
								end
							else
								--If the negative quantity has left to consume, get another positive record
								while @negQty &lt; 0
									begin
										--If there are no more positive record (still negatives left), then insert and end
										if (select isnull(sum(posProcessed), 0) from @posTable) = 0
											begin
												insert into work_invent_trans_aging
												values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, &#039;1/1/1900&#039;, @negQty, &#039;New2&#039;, @negInventoryAmount)
												
												set @negQty = 0
												set @negInventoryAmount = 0
											end
										else
											begin
												if (@negQty * -1) &lt; @posQty
													begin
														insert into work_invent_trans_aging
														values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, @posDatestatus, @negQty, &#039;New3&#039;, @negInventoryAmount)
														
														set @posQty = @posQty + @negQty
														set @posInventoryAmount = @posInventoryAmount + @negInventoryAmount
														
														set @negQty = 0
														set @negInventoryAmount = 0
													end
												else
													begin
														--Write into result table the consumed positive amount
														insert into work_invent_trans_aging
														values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, @posDatestatus, (@posQty * -1), &#039;New4&#039;, (@posInventoryAmount * -1))
														
														set @negQty = @negQty + @posQty
														set @negInventoryAmount = @negInventoryAmount + @posInventoryAmount
														
														update @posTable
														set posProcessed = 0
														where posDatestatus = @posDatestatus
															and posRecid = @posRecid
														
														select @posRecid = posRecid, @posDatestatus = posDatestatus, @posQty = posQty, @posInventoryAmount = posInventoryAmount  from @posTable p1 where posProcessed  0 order by posDatestatus desc, posRecid desc
													end
											end
									end
						end
					
					update @negTable
					set negProcessed = 0
					where negDatestatus = @negDatestatus
						and negRecid = @negRecid
					
					set @negDatestatus = NULL
					set @negQty = NULL
					set @negRecid = NULL
				end
			
			--select * from @posTable t1 order by t1.posDatestatus, posRecid
			--select * from @negTable t1 order by t1.negDatestatus, negRecid
			
			--Clear aging variables
			set @negDatestatus = NULL
			set @negQty = NULL
			set @negRecid = NULL
			set @negInventoryAmount = NULL
			
			set @posDatestatus = NULL
			set @posQty = NULL
			set @posRecid = NULL
			set @posInventoryAmount = NULL
			
			delete @negTable
			delete @posTable
			
			update #tempAgingRecords
			set processed = 0
			where itemid = @itemid
				and dataareaid = @dataareaid
		end
END

GO]]></description>
		<content:encoded><![CDATA[<p>ugh..I have a complex stor proc that I could so use all of your help on.  It runs fine but takes over 8 hours to run!  Any suggestions would be so greatly appreciated.<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>    /*<br />
    IF OBJECT_ID(&#8216;tempdb..#tempAgingRecords&#8217;) IS NOT NULL<br />
	drop table #tempAgingRecords<br />
	*/</p>
<p>	declare @itemid varchar(40),<br />
		@dataareaid varchar(10)</p>
<p>	&#8211;Master records<br />
	select distinct t4.product_number itemid,<br />
		t3.company_id dataareaid,<br />
		1 processed<br />
	into #tempAgingRecords<br />
	from edw.dbo.f_inventory_transaction t1<br />
		left outer join edw.dbo.dim_inventory_trans_type t2 on<br />
			t1.transaction_type_key = t2.transaction_type_key<br />
		left outer join edw.dbo.dim_company t3 on<br />
			t1.company_key = t3.company_key<br />
		left outer join edw.dbo.dim_product t4 on<br />
			t1.product_key = t4.product_key<br />
		left outer join edw.dbo.dim_date t5 on<br />
			t1.date_key = t5.date_key<br />
	where &#8211;t2.transaction_type not in (&#8216;Profit/Loss&#8217;, &#8216;Transfer&#8217;, &#8216;Quarantine Order&#8217;, &#8216;Transfer Order Shipment&#8217;, &#8216;Transfer Order Receive&#8217;)<br />
		t2.issues_status in (&#8216;None&#8217;, &#8216;Sold&#8217;, &#8216;Deducted&#8217;, &#8216;Picked&#8217;)<br />
		and t2.receipt_status in (&#8216;None&#8217;, &#8216;Purchased&#8217;, &#8216;Received&#8217;, &#8216;Registered&#8217;)<br />
		and t1.transaction_quantity &lt; 0<br />
		&#8211;and t4.product_number = &#039;Meiban CM&#039;<br />
		and t4.product_key is not null<br />
		and t5.[date]  &#8217;1/1/1900&#8242;<br />
	order by t4.product_number</p>
<p>	&#8211;Variables for aging process<br />
	declare @negTable table (<br />
		negDatestatus datetime,<br />
		negQty decimal(28,12),<br />
		negProcessed int,<br />
		negRecid bigint,<br />
		negInventoryAmount decimal(28,12)<br />
		)</p>
<p>	declare @posTable table (<br />
		posDatestatus datetime,<br />
		posQty decimal(28,12),<br />
		posProcessed int,<br />
		posRecid bigint,<br />
		posInventoryAmount decimal(28,12)<br />
		)</p>
<p>	declare @negDatestatus datetime,<br />
		@negQty decimal(28,12),<br />
		@posDatestatus datetime,<br />
		@posQty decimal(28,12),<br />
		@negRecid bigint,<br />
		@posRecid bigint,<br />
		@negInventoryAmount decimal(28,12),<br />
		@posInventoryAmount decimal(28,12)</p>
<p>	truncate table work_invent_trans_aging</p>
<p>	while (select sum(processed) from #tempAgingRecords)  0<br />
		begin<br />
			select @itemid = itemid, @dataareaid = dataareaid from #tempAgingRecords where processed  0</p>
<p>			insert into @negTable<br />
			select t5.[date] datestatus,<br />
				sum(t1.transaction_quantity) qty,<br />
				1 processed,<br />
				rec_id,<br />
				sum(case when t3.company_id = &#8216;rel&#8217;<br />
					then isnull(case when issues_status in (&#8216;None&#8217;, &#8216;Sold&#8217;, &#8216;Deducted&#8217;,'Picked&#8217;)<br />
						and receipt_status in (&#8216;None&#8217;, &#8216;Purchased&#8217;, &#8216;Received&#8217;, &#8216;Registered&#8217;)<br />
						then t1.transaction_quantity * t4.unit_cost end, 0)<br />
					else isnull(case when issues_status in (&#8216;None&#8217;, &#8216;Sold&#8217;, &#8216;Deducted&#8217;,'Picked&#8217;)<br />
						and receipt_status in (&#8216;None&#8217;, &#8216;Purchased&#8217;, &#8216;Received&#8217;, &#8216;Registered&#8217;)<br />
						then t1.posted_cost_amount end, 0) +<br />
						isnull(case when issues_status in (&#8216;None&#8217;, &#8216;Sold&#8217;, &#8216;Deducted&#8217;,'Picked&#8217;)<br />
						and receipt_status in (&#8216;None&#8217;, &#8216;Purchased&#8217;, &#8216;Received&#8217;, &#8216;Registered&#8217;)<br />
						then t1.adjustment_cost_amount end, 0) +<br />
						isnull(case when issues_status in (&#8216;Deducted&#8217;,'Picked&#8217;)<br />
						or receipt_status in (&#8216;Received&#8217;, &#8216;Registered&#8217;)<br />
						then t1.physical_cost_amount end, 0) end) as InventoryAmount<br />
			from edw.dbo.f_inventory_transaction t1<br />
				left outer join edw.dbo.dim_inventory_trans_type t2 on<br />
					t1.transaction_type_key = t2.transaction_type_key<br />
				left outer join edw.dbo.dim_company t3 on<br />
					t1.company_key = t3.company_key<br />
				left outer join edw.dbo.dim_product t4 on<br />
					t1.product_key = t4.product_key<br />
				left outer join edw.dbo.dim_date t5 on<br />
					t1.date_key = t5.date_key<br />
			where t4.product_number = @itemid<br />
				and t3.company_id = @dataareaid<br />
				&#8211;and t2.transaction_type not in (&#8216;Profit/Loss&#8217;, &#8216;Transfer&#8217;, &#8216;Quarantine Order&#8217;, &#8216;Transfer Order Shipment&#8217;, &#8216;Transfer Order Receive&#8217;)<br />
				and t2.issues_status in (&#8216;None&#8217;, &#8216;Sold&#8217;, &#8216;Deducted&#8217;, &#8216;Picked&#8217;)<br />
				and t2.receipt_status in (&#8216;None&#8217;, &#8216;Purchased&#8217;, &#8216;Received&#8217;, &#8216;Registered&#8217;)<br />
				and t1.transaction_quantity &lt; 0<br />
				&#8211;and t4.product_number  is not null<br />
				and t5.[date]  &#8217;1/1/1900&#8242;<br />
			group by t5.[date],<br />
				rec_id<br />
			having sum(t1.transaction_quantity)  0<br />
				&#8211;and t4.product_number  is not null<br />
				and t5.[date]  &#8217;1/1/1900&#8242;<br />
			group by t5.[date],<br />
				rec_id<br />
			having sum(t1.transaction_quantity) &gt; 0</p>
<p>			&#8211;Get the first positive quantity<br />
			select @posRecid = posRecid, @posDatestatus = posDatestatus, @posQty = posQty, @posInventoryAmount = posInventoryAmount from @posTable p1 where posProcessed  0 order by posDatestatus desc, posRecid desc</p>
<p>			while (select isnull(sum(negProcessed), 0) from @negTable n1)  0<br />
				begin<br />
					&#8211;Get negative quantity<br />
					select @negRecid = negRecid, @negDatestatus = negDatestatus, @negQty = negQty, @negInventoryAmount = negInventoryAmount from @negTable n2 where negProcessed  0 order by negDatestatus desc, negRecid desc</p>
<p>					if (select isnull(sum(posProcessed), 0) from @posTable) = 0<br />
						begin<br />
							&#8211;Insert negative value if there are no positive quantities<br />
							insert into work_invent_trans_aging<br />
							values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, &#8217;1/1/1900&#8242;, @negQty, &#8216;New1&#8242;, @negInventoryAmount)</p>
<p>							set @negQty = 0<br />
							set @negInventoryAmount = 0<br />
						end<br />
					else<br />
						begin<br />
							if @posQty &gt; (@negQty * -1)<br />
								begin<br />
									&#8211;Write into result table the consumed negative amount<br />
									insert into work_invent_trans_aging<br />
									values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, @posDatestatus, @negQty, &#8216;Original&#8217;, @negInventoryAmount)</p>
<p>									set @posQty = @posQty + @negQty<br />
									set @posInventoryAmount = @posInventoryAmount + @negInventoryAmount<br />
								end<br />
							else<br />
								&#8211;If the negative quantity has left to consume, get another positive record<br />
								while @negQty &lt; 0<br />
									begin<br />
										&#8211;If there are no more positive record (still negatives left), then insert and end<br />
										if (select isnull(sum(posProcessed), 0) from @posTable) = 0<br />
											begin<br />
												insert into work_invent_trans_aging<br />
												values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, &#039;1/1/1900&#039;, @negQty, &#039;New2&#039;, @negInventoryAmount)</p>
<p>												set @negQty = 0<br />
												set @negInventoryAmount = 0<br />
											end<br />
										else<br />
											begin<br />
												if (@negQty * -1) &lt; @posQty<br />
													begin<br />
														insert into work_invent_trans_aging<br />
														values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, @posDatestatus, @negQty, &#039;New3&#039;, @negInventoryAmount)</p>
<p>														set @posQty = @posQty + @negQty<br />
														set @posInventoryAmount = @posInventoryAmount + @negInventoryAmount</p>
<p>														set @negQty = 0<br />
														set @negInventoryAmount = 0<br />
													end<br />
												else<br />
													begin<br />
														&#8211;Write into result table the consumed positive amount<br />
														insert into work_invent_trans_aging<br />
														values (@negRecid, @posRecid, @itemid, @dataareaid, @negDatestatus, @posDatestatus, (@posQty * -1), &#039;New4&#039;, (@posInventoryAmount * -1))</p>
<p>														set @negQty = @negQty + @posQty<br />
														set @negInventoryAmount = @negInventoryAmount + @posInventoryAmount</p>
<p>														update @posTable<br />
														set posProcessed = 0<br />
														where posDatestatus = @posDatestatus<br />
															and posRecid = @posRecid</p>
<p>														select @posRecid = posRecid, @posDatestatus = posDatestatus, @posQty = posQty, @posInventoryAmount = posInventoryAmount  from @posTable p1 where posProcessed  0 order by posDatestatus desc, posRecid desc<br />
													end<br />
											end<br />
									end<br />
						end</p>
<p>					update @negTable<br />
					set negProcessed = 0<br />
					where negDatestatus = @negDatestatus<br />
						and negRecid = @negRecid</p>
<p>					set @negDatestatus = NULL<br />
					set @negQty = NULL<br />
					set @negRecid = NULL<br />
				end</p>
<p>			&#8211;select * from @posTable t1 order by t1.posDatestatus, posRecid<br />
			&#8211;select * from @negTable t1 order by t1.negDatestatus, negRecid</p>
<p>			&#8211;Clear aging variables<br />
			set @negDatestatus = NULL<br />
			set @negQty = NULL<br />
			set @negRecid = NULL<br />
			set @negInventoryAmount = NULL</p>
<p>			set @posDatestatus = NULL<br />
			set @posQty = NULL<br />
			set @posRecid = NULL<br />
			set @posInventoryAmount = NULL</p>
<p>			delete @negTable<br />
			delete @posTable</p>
<p>			update #tempAgingRecords<br />
			set processed = 0<br />
			where itemid = @itemid<br />
				and dataareaid = @dataareaid<br />
		end<br />
END</p>
<p>GO</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jalal Choker</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-231890</link>
		<dc:creator><![CDATA[Jalal Choker]]></dc:creator>
		<pubDate>Thu, 05 Jan 2012 15:05:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-231890</guid>
		<description><![CDATA[Wonderful article Pinal.
Thank you!]]></description>
		<content:encoded><![CDATA[<p>Wonderful article Pinal.<br />
Thank you!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SUBBAREDDY</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-231468</link>
		<dc:creator><![CDATA[SUBBAREDDY]]></dc:creator>
		<pubDate>Wed, 04 Jan 2012 22:14:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-231468</guid>
		<description><![CDATA[Hi Pinal,

These tips are very nice and helping me allot

Thanks]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>These tips are very nice and helping me allot</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Khurram</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-222858</link>
		<dc:creator><![CDATA[Khurram]]></dc:creator>
		<pubDate>Wed, 21 Dec 2011 11:36:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-222858</guid>
		<description><![CDATA[Does adding Comments in stored procedure or function causes ANY KIND of burden on SQL Server performance ?]]></description>
		<content:encoded><![CDATA[<p>Does adding Comments in stored procedure or function causes ANY KIND of burden on SQL Server performance ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Subin</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-215871</link>
		<dc:creator><![CDATA[Subin]]></dc:creator>
		<pubDate>Mon, 12 Dec 2011 07:36:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-215871</guid>
		<description><![CDATA[Hi Pinal,
Nice set of tips]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,<br />
Nice set of tips</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nakul Vachhrajani</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-197889</link>
		<dc:creator><![CDATA[Nakul Vachhrajani]]></dc:creator>
		<pubDate>Thu, 17 Nov 2011 18:36:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-197889</guid>
		<description><![CDATA[Contrary to your comment, SET NOCOUNT ON is quite important. What it ensures is that the number of rows affected is not returned to the caller.

You may be surprised to know, but it is true that not having SET NOCOUNT ON may result in the application not behaving as it should.

In my opinion, removing &quot;SET NOCOUNT ON&quot; is not making things simple - it&#039;s making things suicidal!]]></description>
		<content:encoded><![CDATA[<p>Contrary to your comment, SET NOCOUNT ON is quite important. What it ensures is that the number of rows affected is not returned to the caller.</p>
<p>You may be surprised to know, but it is true that not having SET NOCOUNT ON may result in the application not behaving as it should.</p>
<p>In my opinion, removing &#8220;SET NOCOUNT ON&#8221; is not making things simple &#8211; it&#8217;s making things suicidal!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shahid</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-195906</link>
		<dc:creator><![CDATA[shahid]]></dc:creator>
		<pubDate>Tue, 15 Nov 2011 08:11:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-195906</guid>
		<description><![CDATA[Why do u make things complex by adding odd things like &quot;SET NOCOUNT ON;&quot;
 try keep things simple.

&quot;CEATE PROCEDURE proc_name
as
your query&quot;

thats it]]></description>
		<content:encoded><![CDATA[<p>Why do u make things complex by adding odd things like &#8220;SET NOCOUNT ON;&#8221;<br />
 try keep things simple.</p>
<p>&#8220;CEATE PROCEDURE proc_name<br />
as<br />
your query&#8221;</p>
<p>thats it</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CRUD Generator</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-189244</link>
		<dc:creator><![CDATA[CRUD Generator]]></dc:creator>
		<pubDate>Sun, 06 Nov 2011 03:33:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-189244</guid>
		<description><![CDATA[Hi Yashwant,

I think T4S Stored Procedure Generator is good but will it improve performance of my SPs or not?]]></description>
		<content:encoded><![CDATA[<p>Hi Yashwant,</p>
<p>I think T4S Stored Procedure Generator is good but will it improve performance of my SPs or not?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-178839</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Fri, 14 Oct 2011 13:44:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-178839</guid>
		<description><![CDATA[SQL Server takes some extra time to determine the schema of the object if it omitted. This time is really very mimimum that you cannot find a difference at all]]></description>
		<content:encoded><![CDATA[<p>SQL Server takes some extra time to determine the schema of the object if it omitted. This time is really very mimimum that you cannot find a difference at all</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joshi</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-177911</link>
		<dc:creator><![CDATA[Joshi]]></dc:creator>
		<pubDate>Wed, 12 Oct 2011 09:59:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-177911</guid>
		<description><![CDATA[Hi Pinal,

very good article and informative too.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>very good article and informative too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dallas</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-177283</link>
		<dc:creator><![CDATA[dallas]]></dc:creator>
		<pubDate>Mon, 10 Oct 2011 23:02:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-177283</guid>
		<description><![CDATA[Re: Swapnil,

For this situation to work, as Pinal is suggesting, you will have to take note of the time taken before the cached plan is found.  Having the qualified schema name reduces the time taken to search for the correct plan.

Obviously we&#039;re splitting hairs here, but in the world of DBA&#039;s that&#039;s often what we do if there&#039;s a requirement to attain the fastest possible response.
Dallas]]></description>
		<content:encoded><![CDATA[<p>Re: Swapnil,</p>
<p>For this situation to work, as Pinal is suggesting, you will have to take note of the time taken before the cached plan is found.  Having the qualified schema name reduces the time taken to search for the correct plan.</p>
<p>Obviously we&#8217;re splitting hairs here, but in the world of DBA&#8217;s that&#8217;s often what we do if there&#8217;s a requirement to attain the fastest possible response.<br />
Dallas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Swapnil Kamble</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-176343</link>
		<dc:creator><![CDATA[Swapnil Kamble]]></dc:creator>
		<pubDate>Sat, 08 Oct 2011 06:04:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-176343</guid>
		<description><![CDATA[Use schema name with object name:

Hi sir  i have some doubt regarding the above explanation

I try select * from dbo.Categories in NorthWInd db
execution plan  showing Clustred index scan Estimated cpu cost 0.003125
and select cached plan size 9B

after that i try select * from Categories 
execution plan  showing Clustred index scan Estimated cpu cost 0.003125
and select cached plan size 9B

both the execution plans are same.

so why we need to put schema name with object name 

regards,
swapnil K]]></description>
		<content:encoded><![CDATA[<p>Use schema name with object name:</p>
<p>Hi sir  i have some doubt regarding the above explanation</p>
<p>I try select * from dbo.Categories in NorthWInd db<br />
execution plan  showing Clustred index scan Estimated cpu cost 0.003125<br />
and select cached plan size 9B</p>
<p>after that i try select * from Categories<br />
execution plan  showing Clustred index scan Estimated cpu cost 0.003125<br />
and select cached plan size 9B</p>
<p>both the execution plans are same.</p>
<p>so why we need to put schema name with object name </p>
<p>regards,<br />
swapnil K</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kamlesh Gupta</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-174571</link>
		<dc:creator><![CDATA[Kamlesh Gupta]]></dc:creator>
		<pubDate>Mon, 03 Oct 2011 10:05:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-174571</guid>
		<description><![CDATA[Hello Pinal Sir,

Good Artical.

Kamlesh Gupta]]></description>
		<content:encoded><![CDATA[<p>Hello Pinal Sir,</p>
<p>Good Artical.</p>
<p>Kamlesh Gupta</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nakulvachhrajani</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-173220</link>
		<dc:creator><![CDATA[nakulvachhrajani]]></dc:creator>
		<pubDate>Thu, 29 Sep 2011 15:06:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-173220</guid>
		<description><![CDATA[Hello, Amit!

User-schema separation introduced in SQL Server 2005 was meant to avoid this situation that you are talking about. Users are no longer directly linked to an object.

In the qualified object name that Pinal demonstrates, the &quot;dbo&quot; is not the user name, but the default schema name.]]></description>
		<content:encoded><![CDATA[<p>Hello, Amit!</p>
<p>User-schema separation introduced in SQL Server 2005 was meant to avoid this situation that you are talking about. Users are no longer directly linked to an object.</p>
<p>In the qualified object name that Pinal demonstrates, the &#8220;dbo&#8221; is not the user name, but the default schema name.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amit Saxena</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-172943</link>
		<dc:creator><![CDATA[Amit Saxena]]></dc:creator>
		<pubDate>Wed, 28 Sep 2011 20:39:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-172943</guid>
		<description><![CDATA[Hi Pinal,

Nice artical.

But as you mentioned there in your article-
&quot;always refer the objects with qualified name in the stored procedure&quot; like:
---------------------------------------------------------------------------------------------------
SELECT * FROM dbo.MyTable -- Preferred method
-- Instead of
SELECT * FROM MyTable -- Avoid this method
--And finally call the stored procedure with qualified name like:
EXEC dbo.MyProc -- Preferred method
--Instead of
EXEC MyProc -- Avoid this method
---------------------------------------------------------------------------------------------------
we could face problem when we have more than one database position like a test database server and a live database and database admin are different or database admin can be changed time to time, in that case to maintain the synchronization we need to change the query regarding database admin, which is quite impossible for a developer.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>Nice artical.</p>
<p>But as you mentioned there in your article-<br />
&#8220;always refer the objects with qualified name in the stored procedure&#8221; like:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
SELECT * FROM dbo.MyTable &#8212; Preferred method<br />
&#8211; Instead of<br />
SELECT * FROM MyTable &#8212; Avoid this method<br />
&#8211;And finally call the stored procedure with qualified name like:<br />
EXEC dbo.MyProc &#8212; Preferred method<br />
&#8211;Instead of<br />
EXEC MyProc &#8212; Avoid this method<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
we could face problem when we have more than one database position like a test database server and a live database and database admin are different or database admin can be changed time to time, in that case to maintain the synchronization we need to change the query regarding database admin, which is quite impossible for a developer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rahul</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-166526</link>
		<dc:creator><![CDATA[Rahul]]></dc:creator>
		<pubDate>Thu, 08 Sep 2011 11:24:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-166526</guid>
		<description><![CDATA[nice tips pinal thks]]></description>
		<content:encoded><![CDATA[<p>nice tips pinal thks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-160930</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Tue, 23 Aug 2011 09:31:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-160930</guid>
		<description><![CDATA[What is the purpose of this? This cannot be automated using a trigger. You should use a job to do this]]></description>
		<content:encoded><![CDATA[<p>What is the purpose of this? This cannot be automated using a trigger. You should use a job to do this</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: roopali pandey</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-158723</link>
		<dc:creator><![CDATA[roopali pandey]]></dc:creator>
		<pubDate>Thu, 18 Aug 2011 07:02:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-158723</guid>
		<description><![CDATA[Hii Pinal,

Nice artical.
Thanks for the information or tips you provide for optimazation in sql.]]></description>
		<content:encoded><![CDATA[<p>Hii Pinal,</p>
<p>Nice artical.<br />
Thanks for the information or tips you provide for optimazation in sql.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kavi</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-154976</link>
		<dc:creator><![CDATA[kavi]]></dc:creator>
		<pubDate>Mon, 08 Aug 2011 09:47:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-154976</guid>
		<description><![CDATA[Hi sir

please give a very importance point of store procedures and function Difference]]></description>
		<content:encoded><![CDATA[<p>Hi sir</p>
<p>please give a very importance point of store procedures and function Difference</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kavi</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-154974</link>
		<dc:creator><![CDATA[kavi]]></dc:creator>
		<pubDate>Mon, 08 Aug 2011 09:47:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-154974</guid>
		<description><![CDATA[Hi sir

    please give a very importance point of store procedures and   function Difference]]></description>
		<content:encoded><![CDATA[<p>Hi sir</p>
<p>    please give a very importance point of store procedures and   function Difference</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Coupons</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-154861</link>
		<dc:creator><![CDATA[Coupons]]></dc:creator>
		<pubDate>Mon, 08 Aug 2011 05:17:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-154861</guid>
		<description><![CDATA[That&#039;s why I say

SQLAuthority rocks!!!]]></description>
		<content:encoded><![CDATA[<p>That&#8217;s why I say</p>
<p>SQLAuthority rocks!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comment-153121</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Wed, 03 Aug 2011 11:08:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000#comment-153121</guid>
		<description><![CDATA[Have a look at For xml path in SQL Server help file]]></description>
		<content:encoded><![CDATA[<p>Have a look at For xml path in SQL Server help file</p>
]]></content:encoded>
	</item>
</channel>
</rss>

