<?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/"
	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>Kuldip Bhatt&#039;s Blog About SQL</title>
	<atom:link href="http://sqlmca.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlmca.wordpress.com</link>
	<description>If any thing is useful then give me feedback - Kuldip Bhatt</description>
	<lastBuildDate>Fri, 23 Dec 2011 13:00:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sqlmca.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Kuldip Bhatt&#039;s Blog About SQL</title>
		<link>http://sqlmca.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sqlmca.wordpress.com/osd.xml" title="Kuldip Bhatt&#039;s Blog About SQL" />
	<atom:link rel='hub' href='http://sqlmca.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Sqlserver Query optimization Tips</title>
		<link>http://sqlmca.wordpress.com/2011/12/23/sqlserver-query-optimization-tips/</link>
		<comments>http://sqlmca.wordpress.com/2011/12/23/sqlserver-query-optimization-tips/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 13:00:36 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Tsql]]></category>
		<category><![CDATA[Procedure]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[Dynamic Sql]]></category>
		<category><![CDATA[cursor]]></category>
		<category><![CDATA[Query optimization]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=52</guid>
		<description><![CDATA[Hello friends, I have learn some useful tips that i fill i need to put on blog so i am sharing with you, so below is the list of Query optimization Tips. 1. Restrict the Query result. Means return the required row and column from the query, This helps in reducing network traffic. 2. User [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=52&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hello friends, I have learn some useful tips that i fill i need to put on blog so i am sharing with you, so below is the list of Query optimization Tips.</p>
<p>1. Restrict the Query result. Means return the required row and column from the query, This helps in reducing network traffic.</p>
<p>2. User Store procedure in place of the Queries. Store procedure is compile object so next time i will use execution plan created by the sqlserver engine.</p>
<p>3. Avoid Cursor that will improve your performance. Cursor fetches records row by row so it will make round trips.<br />
<a href="http://sqlmca.wordpress.com/2009/07/29/how-to-convert-cursor-into-while-loop-in-sqlserver/" title="Convert your cursor to While loop for Optimization" target="_blank"></a></p>
<p>4. Avoid Trigger if not needed. It will execute every time when data gets updated.</p>
<p>5. Use Table variable in place of the Temporary table. Temporary table will create in Tempdb it&#8217;s overhead  to create table. Table variable is doesn&#8217;t need to create in the memory and it&#8217;s automatically remove when  transaction complete.</p>
<p>6. Use &#8220;where&#8221; in place of Having clause.</p>
<p>7. Use &#8220;SET NOCOUNT ON&#8221; in store procedure, It&#8217;s stops sending message of rowcount and reduce traffic.</p>
<p>8.Use Dynamic Query with this syntax.Check below link<br />
<a href="http://sqlmca.wordpress.com/2010/11/10/use-of-dynamic-sqlquery-and-its-optimization/" title="Dynamic Query with Optimization " target="_blank">http://sqlmca.wordpress.com/2010/11/10/use-of-dynamic-sqlquery-and-its-optimization/</a></p>
<p>9. Use indexing where you need.(use it carefully)</p>
<p>10. Update your statistics in regular interval. Update Statistics will rebuild your indexes and give your good performance.</p>
<p>11. Check your execution plan for the No table scan.</p>
<p>I hope that will be useful tips for you and you can use it practically.<br />
If you like it or any query give your valuable time by writing comments.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/52/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=52&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2011/12/23/sqlserver-query-optimization-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
		<item>
		<title>USE of XML datatype, Read XML and Write XML in Sqlserver</title>
		<link>http://sqlmca.wordpress.com/2010/11/10/use-of-xml-datatype-read-xml-and-write-xml-in-sqlserver/</link>
		<comments>http://sqlmca.wordpress.com/2010/11/10/use-of-xml-datatype-read-xml-and-write-xml-in-sqlserver/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 07:43:11 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[Procedure]]></category>
		<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Tsql]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=48</guid>
		<description><![CDATA[ML data type is very useful in SQLserver 2005 and 2008. Check the below script and learn how to use XML data type. Create Table Patient(FirstName Varchar(20),LastName Varchar(20)) insert into Patient values(&#8216;Jack&#8217;,'Sparrow&#8217;) insert into Patient values(&#8216;Peter&#8217;,'Parker&#8217;) insert into Patient values(&#8216;John&#8217;,'Black&#8217;) Below is the simple Feature of the XML. You can set your table data in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=48&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ML data type is very useful in SQLserver 2005 and 2008.</p>
<p>Check the below script and learn how to use XML data type.</p>
<p>Create Table Patient(FirstName Varchar(20),LastName Varchar(20))<br />
insert into Patient<br />
values(&#8216;Jack&#8217;,'Sparrow&#8217;)<br />
insert into Patient<br />
values(&#8216;Peter&#8217;,'Parker&#8217;)<br />
insert into Patient<br />
values(&#8216;John&#8217;,'Black&#8217;)</p>
<p>Below is the simple Feature of the XML.<br />
You can set your table data in the XML type of variables<br />
And read that.<br />
Important feature is you can pass that XML type variables<br />
To any procedure and you can read and process that data.<br />
It’s like you pass table from one procedure to another procedure.</p>
<p>DECLARE @xmlDoc XML<br />
SET @xmlDoc =<br />
(<br />
SELECT FirstName,LastName FROM Patient<br />
FOR XML AUTO<br />
)<br />
SELECT @xmlDoc</p>
<p>Below code will fetch data from xml variable created above.</p>
<p>select<br />
SelCat.CatDet.value(&#8216;@FirstName&#8217;, &#8216;varchar(20)&#8217;) as FirstName,<br />
SelCat.CatDet.value(&#8216;@LastName &#8216;, &#8216;varchar(20)&#8217;) as LastName<br />
from<br />
@xmlDoc.nodes(&#8216;Patient&#8217;) as SelCat(CatDet)</p>
<p>Below is the example of Write the rows to the XML TYPE<br />
And you can read that as rows in any procedure.<br />
This is useful when you have to pass block of rows<br />
Without defining the table name.<br />
Below is the example of that.</p>
<p>DECLARE @xmlDoc XML<br />
SET @xmlDoc =<br />
(<br />
SELECT FirstName,LastName FROM Patient<br />
FOR XML RAW<br />
)</p>
<p>Below code will fetch data from xml variable created above.</p>
<p>SELECT<br />
SelXML.xmlDetail.value(&#8216;@FirstName&#8217;, &#8216;Varchar(20)&#8217;) AS FirstName,<br />
SelXML.xmlDetail.value(&#8216;@LastName&#8217;, &#8216;varchar(20)&#8217;) AS LastName<br />
FROM<br />
@xmlDoc.nodes(&#8216;row&#8217;) AS SelXML(xmlDetail)<br />
drop table Patient</p>
<p>I hope this post is useful for you and upgrade your knowledge.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=48&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2010/11/10/use-of-xml-datatype-read-xml-and-write-xml-in-sqlserver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
		<item>
		<title>Use of Dynamic SqlQuery and its Optimization</title>
		<link>http://sqlmca.wordpress.com/2010/11/10/use-of-dynamic-sqlquery-and-its-optimization/</link>
		<comments>http://sqlmca.wordpress.com/2010/11/10/use-of-dynamic-sqlquery-and-its-optimization/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 07:40:27 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[Dynamic Sql]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[Procedure]]></category>
		<category><![CDATA[Sqlserver]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=45</guid>
		<description><![CDATA[Before writing this article I have many misconceptions for using the dynamic sql in sqlserver. We are basically using the Execute statement for executing the dynamic query in sqlserver. There are two options to execute the dynamic query 1) Execute @SqlQuery 2) Execute Sp_executesql @Sqlquery In this article I will explore the Second option and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=45&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Before writing this article I have many misconceptions   for using the dynamic sql in sqlserver.<br />
We are basically using the Execute statement for executing the dynamic query in sqlserver.<br />
There are two options to execute the dynamic query<br />
1)	Execute @SqlQuery<br />
2)	Execute Sp_executesql @Sqlquery<br />
In this article I will explore the Second option and how useful that option in Development.<br />
Below is the example of Sp_executesql.</p>
<p>DECLARE @IntPatientID int;</p>
<p>DECLARE @SQLString nvarchar(500);</p>
<p>DECLARE @ParmDefinition nvarchar(500);</p>
<p>DECLARE @EncouterCount int;</p>
<p>SET @IntPatientID = 286;<br />
SET @SQLString = N&#8217;Select @EncouterCnt=count(EncounterID) from Pat_Encounter where PatientID = @PatientID&#8217;;<br />
SET @ParmDefinition = N&#8217;@PatientID int, @EncouterCnt int OUTPUT&#8217;;</p>
<p>EXECUTE sp_executesql @SQLString, @ParmDefinition, @PatientID = @IntPatientID, @EncouterCnt=@EncouterCount OUTPUT;</p>
<p>SELECT @EncouterCount; </p>
<p>In Execute option you will not get option of parameter you must define the variable in the dynamic sql to achieve the functionality.<br />
In  Sp_executesql you will get input and output parameter and that’s very useful in Development.<br />
Another thing is Execute is not reuse Execution query plan else you are not define same value in condition of where.<br />
Sp_executesql is  reuse the Execution Plan and give fast output then Execute clause.<br />
For the Optimization Sp_executesql is very handy in procedure.<br />
You will get syntax from below link.</p>
<p>http://msdn.microsoft.com/en-us/library/ms188001(SQL.90).aspx</p>
<p>I hope this article is useful to you and enhance your knowledge of sqlserver.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=45&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2010/11/10/use-of-dynamic-sqlquery-and-its-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
		<item>
		<title>Use of CASE &#8230; WHEN in Where condition and optional parameter in SQL Procedure</title>
		<link>http://sqlmca.wordpress.com/2010/05/10/use-of-case-when-in-where-condition-and-optional-parameter-in-sql-procedure/</link>
		<comments>http://sqlmca.wordpress.com/2010/05/10/use-of-case-when-in-where-condition-and-optional-parameter-in-sql-procedure/#comments</comments>
		<pubDate>Mon, 10 May 2010 12:57:54 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[optimization]]></category>
		<category><![CDATA[Procedure]]></category>
		<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Tsql]]></category>
		<category><![CDATA[Case in Where Condition]]></category>
		<category><![CDATA[CASE WHEN]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=38</guid>
		<description><![CDATA[I think every body know how to use the CASE in the select query. Today i will explain use of CASE in the where condition with some specific logic triks. Example : DECLARE @Recordstatus AS BIT SET @Recordstatus = 1 SELECT * FROM dbo.ADT_Columns WHERE RecordStatus = (CASE @Recordstatus WHEN @Recordstatus THEN 0 ELSE 1 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=38&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I think every body know how to use the CASE in the select query.<br />
Today i will explain use of CASE in the where condition with some specific logic triks.<br />
Example :<br />
DECLARE @Recordstatus AS BIT<br />
SET @Recordstatus = 1<br />
SELECT * FROM dbo.ADT_Columns<br />
WHERE RecordStatus = (CASE @Recordstatus WHEN @Recordstatus THEN 0 ELSE 1 END)</p>
<p>if CASE clause returns some value then you use CASE in Where condition.</p>
<p>Another thing i will explain is logical use of case which remove the use of If condition and also use the optional parameter in procedure.<br />
I think many developers know that feature but i think nobody uses this option  in sqlserver for the procedure level we use this at .net or clientside mostly but it is very useful feature. When you use one procedure at many places then you need not to pass this parameter at every place.<br />
In below example i will cover both the topics.<br />
&#8211; Run below code in AdventureWorks DB<br />
&#8211; GetAddress &#8216;All&#8217;<br />
&#8211; Procedure with IF condition<br />
CREATE PROCEDURE GetAddress @city VARCHAR(50)<br />
AS<br />
BEGIN<br />
IF @city = &#8216;All&#8217;<br />
BEGIN<br />
&#8211; Returns all Records whithout any condition<br />
SELECT * FROM Person.Address<br />
END<br />
ELSE<br />
BEGIN<br />
&#8211; Returns all Records which match the condition<br />
SELECT * FROM Person.Address<br />
WHERE City LIKE @City<br />
END<br />
END<br />
&#8211; GetAddressWihtCase &#8216;London&#8217;<br />
&#8211; GetAddressWihtCase  &#8216;All&#8217; or GetAddressWihtCase  &#8216;All&#8217;<br />
&#8211; Procedure without IF condition and Optional Parameters<br />
CREATE PROCEDURE GetAddressWihtCase @city VARCHAR(50) = &#8216;All&#8217;<br />
&#8211; OPtional parameter<br />
AS<br />
BEGIN<br />
SELECT * FROM Person.Address<br />
WHERE City LIKE (CASE @city WHEN &#8216;All&#8217; THEN City ELSE @city END)<br />
&#8211; Use it&#8217;s own value in the condition this is very useful thing and independent to<br />
&#8211; Technology you can use in Oracle or in MySQl too.</p>
<p>END</p>
<p>I hope this article is useful to enhance your knowledge about Sqlserver.If you like this article<br />
then please give comment on this article.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=38&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2010/05/10/use-of-case-when-in-where-condition-and-optional-parameter-in-sql-procedure/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
		<item>
		<title>Table value parameter new feature in SQLSERVER 2008.</title>
		<link>http://sqlmca.wordpress.com/2009/09/01/table-value-parameter-new-feature-in-sqlserver-2008/</link>
		<comments>http://sqlmca.wordpress.com/2009/09/01/table-value-parameter-new-feature-in-sqlserver-2008/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 13:22:24 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[Procedure]]></category>
		<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Tsql]]></category>
		<category><![CDATA[Bulk Delete]]></category>
		<category><![CDATA[Bulk Insert]]></category>
		<category><![CDATA[New feature of Sqlserver 2008]]></category>
		<category><![CDATA[parameter]]></category>
		<category><![CDATA[Sql 2008]]></category>
		<category><![CDATA[Table as parameter]]></category>
		<category><![CDATA[table as parameter in Procedure]]></category>
		<category><![CDATA[Table Value Parameter]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=32</guid>
		<description><![CDATA[Today i will explain very  nice and useful feature of the Sqlserver 2008.In the past i was facing many problem in reading and writing data in the Database from .Net environment for bulk insert and delete.I personally use XML to read the data from vb.net for implement bulk insert and delete. After understanding this feature [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=32&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today i will explain very  nice and  useful  feature of the Sqlserver 2008.In the past i was facing many problem in reading and writing data in the Database from .Net environment for bulk insert and delete.I personally use XML to read the data from vb.net for implement bulk insert and delete.<br />
After understanding this feature this thing is very easly done in future.<br />
Below is the example of,<br />
how we can pass Table as parameter to Procedure?</p>
<p>&#8211; Step 1<br />
&#8211; Create table Student</p>
<p><strong>CREATE TABLE [dbo].[Student](<br />
[Student_ID] [int] IDENTITY(1,1) NOT NULL,<br />
[Name] [varchar](80) NOT NULL,<br />
[City] [varchar](100) NOT NULL<br />
) ON [PRIMARY]</strong></p>
<p>GO</p>
<p>Step 2</p>
<p>&#8211; Insert the Rows in the Student Table<br />
<strong>Insert into Student<br />
values(&#8216;Jhon&#8217;,'NewYork&#8217;)<br />
Insert into Student<br />
values(&#8216;Mac&#8217;,'Paris&#8217;)<br />
Insert into Student<br />
values(&#8216;Rahul&#8217;,'Delhi&#8217;)<br />
Insert into Student<br />
values(&#8216;Jay&#8217;,'Mumbai&#8217;)<br />
Insert into Student<br />
values(&#8216;Jack&#8217;,'London&#8217;)</strong></p>
<p><strong>select * from Student</strong></p>
<p>Step 3<br />
&#8211; very important<br />
&#8211; Create the StudentTabelType as userdefine datatype of Table<br />
<strong>CREATE TYPE [StudentTableType] AS TABLE<br />
(<br />
[Name] [varchar](80) NOT NULL,<br />
[City] [varchar](100) NOT NULL<br />
)<br />
GO</strong></p>
<p>&#8211; Create the Procedure which get the Table as input parameter<br />
<strong>Create PROCEDURE StudentDetail(@StudentTable StudentTableType READONLY)<br />
AS<br />
BEGIN<br />
INSERT INTO Student<br />
SELECT * FROM @StudentTable</strong></p>
<p><strong>&#8211; You can create Tabel variable like this<br />
&#8211;DECLARE @StudentTab StudentTableType<br />
&#8211;insert into @StudentTab<br />
&#8211;values (&#8216;Rajesh&#8217;,'Bhopal&#8217;)<br />
&#8211;insert into Student<br />
&#8211;select * from @Studenttab<br />
END</strong><br />
<strong>GO</strong><br />
You must pass Table variable as read only you can not prform<br />
any DML command on this pass Table.<br />
Like that you can pass table in the procedure and use for the further manipulation.</p>
<p>Now, one of the important thing is how to pass from .net environment.</p>
<p>Declar the datatable with two columns name and city.<br />
<strong>Declare DTStudent as new datatable<br />
DTstudent.Columns.add(&#8220;Name&#8221;)<br />
DTstudent.Columns.add(&#8220;City&#8221;)</strong></p>
<p>Insert some Records in this DTStudent Table.<br />
Create Connection and command object and set all it&#8217;s property to establish connection to sqlserver.</p>
<p>Add parameter to command<br />
<strong>Dim sqlcommad as new SqlClient.SqlCommand</strong><br />
Use the Structured datatype for the parameter and set the datatable to the<br />
value of the parameter.</p>
<p><strong>sqlcmd.Parameters.Add(&#8220;StudentTable&#8221;, SqlDbType.Structured).value = StudentTable</strong></p>
<p>This feature is very useful in bulk insert and delete of data.</p>
<p>I hope this article is useful to you,give me feed back on this article.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=32&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2009/09/01/table-value-parameter-new-feature-in-sqlserver-2008/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
		<item>
		<title>How to get Start Date and End Date of Week from Given Date?</title>
		<link>http://sqlmca.wordpress.com/2009/08/25/how-to-get-start-date-and-end-date-of-week-from-given-date/</link>
		<comments>http://sqlmca.wordpress.com/2009/08/25/how-to-get-start-date-and-end-date-of-week-from-given-date/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 04:56:41 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Tsql]]></category>
		<category><![CDATA[DATEFIRST]]></category>
		<category><![CDATA[Week]]></category>
		<category><![CDATA[week Enddate]]></category>
		<category><![CDATA[week start Date and end date]]></category>
		<category><![CDATA[Week Startdate]]></category>
		<category><![CDATA[WeekDay]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=27</guid>
		<description><![CDATA[Today I will explain how you get the Start Date and End Date of week from any date.One of my colleague has requirement something like that he has week no and he want the start date and End date of that week. There are many ways to find that solution but I got one solution [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=27&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I will explain how you get the Start Date and End Date of week from any date.One of my colleague has requirement something like that he has week no and he want the start date and End date of that week.</p>
<p>There are many ways to find that solution but I got one solution that is useful to you if you want something like that in future.</p>
<p>Below is the Query from that you can get the start date and end date of the week.In this Query Replace order date with any date and you get the results from this query.</p>
<p>SELECT</p>
<p>CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST &#8211; DATEPART(dw, <strong>orderdate</strong>) &#8211; 6, <strong>orderdate</strong>)), 101) startdate</p>
<p>CONVERT(varchar(50), (DATEADD(dd, @@DATEFIRST &#8211; DATEPART(dw, <strong>orderdate</strong>), <strong>OrderDate</strong>)), 101) enddate,</p>
<p>from tbl_dietorder_trn (Any Table )</p>
<p>You can use Getdate() function and check this.</p>
<p>In this I use the<strong> @@DATEFIRST</strong> Function. By using this function you can set the first day of week. Sunday is default first day of week so it’s default value is 7.</p>
<p>This Query to find start date and End date id different from other because of <strong>@@DATEFIRST</strong> function.</p>
<p>You can set that function value between 1 to 7.Base on that thing this query is give you the result.</p>
<p>1 -  Monday<br />
2 -  Tuesday<br />
3 -  Wednesday<br />
4 -  Thursday<br />
5 -  Friday<br />
6 -  Saturday<br />
7 -  Sunday</p>
<p>You can set the value of @@DATEFIRST.</p>
<p>Set Datefirst 1</p>
<p>You can get this information from below link about @@DATEFIRST Function.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms187766.aspx">http://msdn.microsoft.com/en-us/library/ms187766.aspx</a></p>
<p>my question to you,<br />
can any one know other than this Query that give First and Last Date of week?<br />
I hope this is useful to you in future.Please give your feedback.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=27&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2009/08/25/how-to-get-start-date-and-end-date-of-week-from-given-date/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
		<item>
		<title>Top with Ties option</title>
		<link>http://sqlmca.wordpress.com/2009/08/06/top-with-ties-option/</link>
		<comments>http://sqlmca.wordpress.com/2009/08/06/top-with-ties-option/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 10:34:37 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Tsql]]></category>
		<category><![CDATA[Ties option]]></category>
		<category><![CDATA[Top Clause]]></category>
		<category><![CDATA[Top with Ties]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=18</guid>
		<description><![CDATA[Today, I will explain how we can use Ties option with Top.Basically Sqlserver reaturn all the rows from the select Query but with Top Option We can specify criteria on that basis Select Query Return the Records. &#8211;Select Query Select SaleitemQty from Sales This Query will return the alll the SaleitemQty from Sales table. &#8211;Select [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=18&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today, I will explain how we can use Ties option with Top.Basically Sqlserver reaturn all the rows from the select Query but with Top Option We can specify criteria on that basis Select Query Return the Records.</p>
<p>&#8211;Select Query</p>
<p><strong>Select SaleitemQty from Sales</strong></p>
<p>This Query will return the alll the SaleitemQty from Sales table.</p>
<p>&#8211;Select Query with Top Clause</p>
<p><strong>Select Top 10 SaleitemQty from Sales</strong></p>
<p>This Query will return the top 10 SaleitemQty from the Sales table.</p>
<p>Now, we come to Top clause with Ties option.</p>
<p>Create table like this</p>
<p><strong>Create Table Sales (CityName varchar(100),SaleQty int)</strong></p>
<p>Insert Rows in this table</p>
<p><strong>Insert into Sales values(&#8216;Delhi&#8217;,15)</strong></p>
<p><strong>Insert into Sales values(&#8216;Bombay&#8217;,17)</strong></p>
<p><strong>Insert into Sales values(&#8216;London&#8217;,20)</strong></p>
<p><strong>Insert into Sales values(&#8216;Newyork&#8217;,22)</strong></p>
<p><strong>Insert into Sales values(&#8216;Tokyo&#8217;,15)</strong></p>
<p>Now, we check first for the Only Top option.</p>
<p>&#8211; Query with Top clause only</p>
<p><strong>select Top 4 SaleQty,cityname from sales order by SaleQty desc</strong></p>
<p>&#8211; Result Set of Top clause</p>
<p>SaleQty     cityname</p>
<p>&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>22          Newyork</p>
<p>20          London</p>
<p>17          Bombay</p>
<p>15          Tokyo</p>
<p>Like that you can see in the result set that, there are Two tables that have saleQty 15 but only one City comes in the result set.</p>
<p>&#8211; Query with Top clause and Ties Option</p>
<p><strong>select Top 4 with Ties SaleQty,cityname from sales order by SaleQty desc</strong></p>
<p>&#8211; Result Set of Ties option</p>
<p>SaleQty     cityname</p>
<p>&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>22          Newyork</p>
<p>20          London</p>
<p>17          Bombay</p>
<p>15          Delhi</p>
<p>15          Tokyo</p>
<p>Ties option alllows the last place to include multiple rows if those rows have eaual values in the columns used in the order by clause.</p>
<p>In this example Two city has same SaleQty 15, with ties option we can get both the citys In the result set with same SaleQty.</p>
<p>I hope this example will clear the Ties option with Top clause.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=18&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2009/08/06/top-with-ties-option/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
		<item>
		<title>How to get Data from One procedure to another Procedure or Dynamic Sql?</title>
		<link>http://sqlmca.wordpress.com/2009/07/30/how-to-get-data-from-one-procedure-to-another-procedure-or-dynamic-sql/</link>
		<comments>http://sqlmca.wordpress.com/2009/07/30/how-to-get-data-from-one-procedure-to-another-procedure-or-dynamic-sql/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 12:33:46 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[Dynamic Sql]]></category>
		<category><![CDATA[Procedure]]></category>
		<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Tsql]]></category>
		<category><![CDATA[Sql Tricks]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=12</guid>
		<description><![CDATA[Today I will explain the very small but very useful features which I use in the past for the procedures. When I am new to sqlserver I am facing many problems this is one of this that I will explain to you today. That you all know that, User defines Function has limitation that we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=12&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I will explain the very small but very useful features which I use in the past for the procedures. When I am new to sqlserver I am facing many problems this is one of this that I will explain to you today.</p>
<p>That you all know that, User defines Function has limitation that we can not use #temp table or Table variable in UDF. Because of that we can not get the data from another function in the UDF. At that time I find the solution that we are now create the procedure and we get data from that procedure for further manipulation. Question is how to get data from another procedure?. You can get the answer of this question in this post..</p>
<p>e.g., suppose procedure <strong>ReturnStudentDetail</strong> return the table with two fields Name and City so how can you get that table in another procedure for further manipulation.</p>
<p>&#8211;Code</p>
<p>&#8211; Create Table</p>
<p><strong>Create table #StudentDetail</strong></p>
<p><strong>(Name varchar (50), City varchar (100))</strong></p>
<p>&#8211; Below code give you data in the procedure</p>
<p><strong>Insert into #StudentDetail</strong></p>
<p><strong>Exec ReturnStudentDetail</strong></p>
<p><strong> </strong></p>
<p>This syntax works only if you create table like this. You also use Dynamic Sql output for inserting data in #temp table.</p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p>&#8211; Create Table</p>
<p><strong>Create table #StudentDetail</strong></p>
<p><strong>(Name varchar (50), City varchar (100))</strong></p>
<p><strong> </strong></p>
<p><strong>Declare @sql as varchar (500)</strong></p>
<p><strong> </strong></p>
<p><strong>Set @sql = ‘Select Name, City from tbl_StudentDetail where ……..’</strong></p>
<p><strong> </strong></p>
<p>&#8211; Below code give you data in table</p>
<p><strong>Insert into #StudentDetail</strong></p>
<p><strong>Exec (@sql)</strong></p>
<p>So, this is how you can get the data from another procedure or dynamic sql in Table.</p>
<p>Basic thing is whatever the structure return on the basis of that you have to create the table and use that table to get data from procedure or dynamic sql.</p>
<p>I hope this feature is useful for in future. If any query feel free to ask me.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=12&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2009/07/30/how-to-get-data-from-one-procedure-to-another-procedure-or-dynamic-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
		<item>
		<title>How to get Data from Dataset into Sqlserver Table by using OPENXML Method?</title>
		<link>http://sqlmca.wordpress.com/2009/07/29/how-to-get-data-from-dataset-into-sqlserver-table-by-using-openxml-method/</link>
		<comments>http://sqlmca.wordpress.com/2009/07/29/how-to-get-data-from-dataset-into-sqlserver-table-by-using-openxml-method/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 14:26:50 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Tsql]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Data from dataset to Sql]]></category>
		<category><![CDATA[Openxml]]></category>
		<category><![CDATA[read XML in sql]]></category>
		<category><![CDATA[XML to SQL]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=8</guid>
		<description><![CDATA[Some time we need data from Dataset into Sqlserver for insertion or updation in the Sqlserver Tables. For example if you need to insert an image then you can use this method to insert the data in Sqlserver tables. Today, I will explain how to get data from Dataset into Sqlserver Table. &#8211; Code for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=8&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Some time we need data from Dataset into Sqlserver for insertion or updation in the Sqlserver Tables.</p>
<p>For example if you need to insert an image then you can use this method to insert the data in Sqlserver tables.</p>
<p>Today, I will explain how to get data from Dataset into Sqlserver Table.</p>
<p><strong>&#8211; Code for .Net</strong></p>
<p><em>Dim ds as new Dataset</em></p>
<p><em>Dim dt as new Table</em></p>
<p><em>dt.TableName = &#8220;MyTable&#8221;</em></p>
<p><em>ds.Tables.add(dt)</em></p>
<p><em>Dim lStrWriter As New StringWriter</em></p>
<p><em>Dim lStr As String</em></p>
<p><em>ds.WriteXML(lStrWriter)</em></p>
<p><em>lstr = lStrWriter.Tostring</em></p>
<p>Pass this lstr variable to the procedure as parameter &#8211; @xmldoc.</p>
<p>Now write the code procedure to get the data from Dataset.</p>
<p><strong> &#8212; Code in Sqlserver Store Procedure</strong></p>
<p><em>CREATE PROCEDURE [dbo].[TestXML]</em></p>
<p><em>@xmldoc text &#8212; Give Text datatype for getting xml from dataset</em></p>
<p><em>as</em></p>
<p><em>&#8212;&#8212;&#8212;Read the Datset  From XML &#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</em></p>
<p><em>&#8211; call sp_xml_preparedocument</em></p>
<p><em>DECLARE @idoc int</em></p>
<p><em>EXEC sp_xml_preparedocument @idoc OUTPUT, @xmldoc</em></p>
<p><em>SELECT * into #TestTable</em></p>
<p><em>FROM OPENXML (@idoc, &#8216;/NewDataSet/MyTable&#8217;,2)</em></p>
<p><em>WITH (pk_Userid  BigInt,</em></p>
<p><em>UserName  Varchar(10))</em></p>
<p><em>EXEC sp_xml_removedocument @idoc</em></p>
<p><em>&#8212;&#8212;&#8212;- End Read &#8212;&#8212;&#8212;-</em></p>
<p><em>END</em></p>
<p>BY the use of this procedure you get all the data from dataset in to the &#8221;#Testtable&#8221; and we can use this as Sqlserver table in procedure.</p>
<p>I hope this article will be useful to you, Please give feedback on this article.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=8&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2009/07/29/how-to-get-data-from-dataset-into-sqlserver-table-by-using-openxml-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
		<item>
		<title>How to Convert Cursor into While Loop In Sqlserver ?</title>
		<link>http://sqlmca.wordpress.com/2009/07/29/how-to-convert-cursor-into-while-loop-in-sqlserver/</link>
		<comments>http://sqlmca.wordpress.com/2009/07/29/how-to-convert-cursor-into-while-loop-in-sqlserver/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 14:18:56 +0000</pubDate>
		<dc:creator>Kuldip Bhatt</dc:creator>
				<category><![CDATA[optimization]]></category>
		<category><![CDATA[Sqlserver]]></category>
		<category><![CDATA[Tsql]]></category>
		<category><![CDATA[convert cursor]]></category>
		<category><![CDATA[cursor]]></category>
		<category><![CDATA[remove cursor]]></category>

		<guid isPermaLink="false">http://sqlmca.wordpress.com/?p=3</guid>
		<description><![CDATA[I personally face this issues in many Store Procedures i.e. Cursor taking too much time to execute. I am sure many of you will be having similar problem. I have worked on that issue and found following solution. It may not be perfect for cursor optimization but it does work in my case. &#8211; Original Cursor [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=3&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I personally face this issues in many Store Procedures i.e. Cursor taking too much time to execute. I am sure many of you will be having similar problem.</p>
<p>I have worked on that issue and found following solution. It may not be perfect for cursor optimization but it does work in my case.</p>
<p><strong>&#8211; Original Cursor Code &#8212; </strong></p>
<p style="padding-left:30px;">Declare @fk_MedicationOrderId as Bigint<br />
Declare @AdminTime as Bigint</p>
<p style="padding-left:30px;">declare CUR_MEDICATIONORDERID cursor FOR<br />
select<br />
fk_MedicationOrderId<br />
from<br />
tbl_medicationorderDetail_trn<br />
where<br />
Actiondate &gt; Getdate()</p>
<p style="padding-left:30px;">OPEN CUR_MEDICATIONORDERID</p>
<p style="padding-left:30px;">FETCH NEXT FROM CUR_MEDICATIONORDERID<br />
INTO @fk_MedicationOrderId</p>
<p style="padding-left:30px;">WHILE @@FETCH_STATUS =0<br />
BEGIN</p>
<p style="padding-left:60px;">Select @AdminTime=AdminTime from tbl_medicationorderDetail_trn<br />
where fk_medicationorderid = @fk_MedicationOrderId</p>
<p style="padding-left:60px;">Update tbl_TempMedicationOrderDetail_trn<br />
set ActionTime = @AdminTime<br />
set @AdminTime = &#8221;</p>
<p style="padding-left:30px;">FETCH NEXT FROM CUR_MEDICATIONORDERID<br />
INTO @fk_MedicationOrderId</p>
<p style="padding-left:30px;">END</p>
<p style="padding-left:30px;">CLOSE CUR_MEDICATIONORDERID<br />
DEALLOCATE CUR_MEDICATIONORDERID</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p><strong>&#8211; Modified Cursor Code (faster) &#8211;</strong></p>
<p style="padding-left:30px;">Declare @fk_MedicationOrderId as Bigint<br />
Declare @AdminTime as Bigint</p>
<p style="padding-left:30px;">Declare Table for the fields you need in the cursor.</p>
<p style="padding-left:30px;">declare @IDList table (ID Bigint)</p>
<p style="padding-left:30px;">Insert into @IDList<br />
select<br />
fk_MedicationOrderId<br />
from<br />
tbl_medicationorderDetail_trn<br />
where<br />
Actiondate &gt; Getdate()</p>
<p style="padding-left:30px;">while (select count(ID) from @IDList) &gt; 0<br />
begin<br />
select top 1 @fk_MedicationOrderId = ID from @IDList</p>
<p style="padding-left:60px;">Select @AdminTime=AdminTime from tbl_medicationorderDetail_trn<br />
where fk_medicationorderid = @fk_MedicationOrderId</p>
<p style="padding-left:60px;">Update tbl_TempMedicationOrderDetail_trn<br />
set ActionTime = @AdminTime<br />
set @AdminTime = &#8221;</p>
<p style="padding-left:30px;">delete from @IDList where ID = @fk_MedicationOrderId</p>
<p style="padding-left:30px;">end</p>
<p><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</strong></p>
<p>What I have done here is to convert <strong>&#8220;cursor&#8221;</strong> into <strong>&#8220;while loop&#8221;</strong>.<strong> </strong>It is just to get your data in any temporary table or table variable after that just delete the rows from that at end of loop.</p>
<p>It helps in improving performance, I have personally tried it in many stored procedures.</p>
<p>Let me know if you find any other useful way of improving performance.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlmca.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlmca.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlmca.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlmca.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlmca.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlmca.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlmca.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlmca.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlmca.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlmca.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlmca.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlmca.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlmca.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlmca.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlmca.wordpress.com&amp;blog=8779768&amp;post=3&amp;subd=sqlmca&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlmca.wordpress.com/2009/07/29/how-to-convert-cursor-into-while-loop-in-sqlserver/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b17ac59beeb61513209292269ec22401?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Kuldip Bhatt</media:title>
		</media:content>
	</item>
	</channel>
</rss>
