<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6972743593291649811</id><updated>2024-12-18T19:28:50.473-08:00</updated><title type='text'>SQL Web Mentor</title><subtitle type='html'>SQL Trick And Tips, Interview questions,Queries, examples to make developer&#39;s life easier than ever before..&#xa;&#xa;My aim to create this blog is to write all such tricks and tips in SQL queries which will be helpful to my friends all over the world.&#xa;&#xa;I want to share my experience about the SQL and hence i am too excited to start with....&#xa;&#xa;Hope you all take me further over the web with my genuine intention to help you out with enjoyment of the technology......</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sqlwebmentor.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>26</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-4807577145013120448</id><published>2011-06-21T00:15:00.000-07:00</published><updated>2011-06-21T00:16:12.443-07:00</updated><title type='text'>FOR XML with SQL queries</title><content type='html'>id          test&lt;br /&gt;----------- -----------&lt;br /&gt;1           prk&lt;br /&gt;2           prk1&lt;br /&gt;3           prk2&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;select * from dbo.aaaa1 for    xml raw&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;br /&gt;&lt;row id=&quot;1&quot; test=&quot;prk&quot;/&gt;&lt;row id=&quot;2&quot; test=&quot;prk1&quot;/&gt;&lt;row id=&quot;3&quot; test=&quot;prk2&quot;/&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;select * from dbo.aaaa1 for   XML AUTO&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;aaaa1 id=&quot;1&quot; test=&quot;prk&quot;/&gt;&lt;aaaa1 id=&quot;2&quot; test=&quot;prk1&quot;/&gt;&lt;aaaa1 id=&quot;3&quot; test=&quot;prk2&quot;/&gt;&lt;br /&gt;&lt;br /&gt;select * from dbo.aaaa1 for xml auto,elements&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;br /&gt;&lt;aaaa1&gt;&lt;id&gt;1&lt;/id&gt;&lt;test&gt;prk&lt;/test&gt;&lt;/aaaa1&gt;&lt;aaaa1&gt;&lt;id&gt;2&lt;/id&gt;&lt;test&gt;prk1&lt;/test&gt;&lt;br /&gt;&lt;/aaaa1&gt;&lt;aaaa1&gt;&lt;id&gt;3&lt;/id&gt;&lt;test&gt;prk2&lt;/test&gt;&lt;/aaaa1&gt;&lt;br /&gt;&lt;br /&gt;select * from dbo.aaaa1 for   XML AUTO ,Elements XSINIL&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;Give same o/p as above but maps null value also. The ELEMENTS directive provides an XSINIL option to map NULL values to an element with an attribute xsi:nil=&quot;true&quot;.&lt;br /&gt;&lt;br /&gt;&lt;aaaa1&gt;&lt;id&gt;1&lt;/id&gt;&lt;test&gt;prk&lt;/test&gt;&lt;/aaaa1&gt;&lt;aaaa1&gt;&lt;id&gt;2&lt;/id&gt;&lt;test&gt;prk1&lt;/test&gt;&lt;br /&gt;&lt;/aaaa1&gt;&lt;aaaa1&gt;&lt;id&gt;3&lt;/id&gt;&lt;test&gt;prk2&lt;/test&gt;&lt;/aaaa1&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/4807577145013120448'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/4807577145013120448'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2011/06/for-xml-with-sql-queries.html' title='FOR XML with SQL queries'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-6422409806715241575</id><published>2011-03-15T12:49:00.000-07:00</published><updated>2011-03-15T12:56:31.424-07:00</updated><title type='text'></title><content type='html'>&lt;b&gt;Overview:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;When deploying applications to a production server(s) or to a shared SQL Server, there is often a concern that other people might peek at your business logic. Most of the code / business logic resides in Stored procedures.&lt;br /&gt;Below is a mechanism to secure the source code of Stored procedure:&lt;br /&gt;--------------------------&lt;br /&gt;CREATE PROCEDURE [test]&lt;br /&gt;WITH ENCRYPTION&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;   SELECT &#39;test&#39;&lt;br /&gt;END&lt;br /&gt;--------------------------  &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;1) Any one will not be able to&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt; open the procedure in Enterprise Manager&#39;s GUI. &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;He will receive the following error: &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; &gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px;&quot;&gt;-----------------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;Microsoft SQL-DMO&lt;br /&gt;Error 20585: [SQL-DMO]&lt;br /&gt;/******&lt;br /&gt;    Encrypted object is not transferable,&lt;br /&gt;    and script can not be generated.&lt;br /&gt;******/&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;-----------------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;2) You can not get source code with sp_helptext [test]&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;----------------------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;2 ways I can find the code.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;1) Peep in SQL profiler&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;2) Create new stored procedure.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 18px; font-family: &#39;Times New Roman&#39;; &quot;&gt;&lt;span style=&quot;font-size:12.0pt;line-height:115%; font-family:&amp;quot;Times New Roman&amp;quot;;mso-fareast-font-family:&amp;quot;Times New Roman&amp;quot;; mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6422409806715241575'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6422409806715241575'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2011/03/overview-when-deploying-applications-to.html' title=''/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-8812276889034908031</id><published>2010-11-12T10:11:00.000-08:00</published><updated>2010-11-12T10:12:27.734-08:00</updated><title type='text'>How to Stop SQL Injection</title><content type='html'>1) Create User WebUser&lt;br /&gt;2) Make it db_Owner for the application database&lt;br /&gt;3) Execute below SQL statements&lt;br /&gt;&lt;br /&gt;use [master]&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [INFORMATION_SCHEMA].[TABLES] TO [Public]&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [INFORMATION_SCHEMA].[COLUMNS] TO [Public]&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [INFORMATION_SCHEMA].[VIEW_COLUMN_USAGE] TO [Public]&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [INFORMATION_SCHEMA].[CONSTRAINT_COLUMN_USAGE] TO [Public]&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [INFORMATION_SCHEMA].[COLUMN_PRIVILEGES] TO [Public]&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;Use [SQLInjection] &lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [sys].[columns] TO [test]&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [sys].[tables] TO [test]&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [sys].[syscolumns] TO [test]&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [sys].[sysobjects] TO [test]&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [sys].[objects] TO [test]&lt;br /&gt;&lt;br /&gt;DENY SELECT ON [sys].[syscomments] TO [test]&lt;br /&gt;GO</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/8812276889034908031'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/8812276889034908031'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/11/how-to-stop-sql-injection.html' title='How to Stop SQL Injection'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-5831991601021135181</id><published>2010-11-12T10:09:00.000-08:00</published><updated>2010-11-12T10:11:14.762-08:00</updated><title type='text'>Script to find out the SQL infection</title><content type='html'>DECLARE @T varchar(255),@C varchar(4000)&lt;br /&gt;DECLARE Table_Cursor CURSOR FOR&lt;br /&gt; select a.name,b.name from sysobjects a,syscolumns b &lt;br /&gt; where a.id=b.id and a.xtype=&#39;u&#39; and (b.xtype=99 or b.xtype=35 or b.xtype=231 &lt;br /&gt; or b.xtype=167)&lt;br /&gt; OPEN Table_Cursor FETCH NEXT FROM Table_Cursor INTO @T,@C &lt;br /&gt; WHILE(@@FETCH_STATUS=0) &lt;br /&gt; BEGIN&lt;br /&gt;&lt;br /&gt; exec(&#39;Select * from [&#39;+@T+&#39;] where [&#39;+@C+&#39;] like &#39;&#39;%title%&#39;&#39;&#39;) &lt;br /&gt; &lt;br /&gt; FETCH NEXT FROM &lt;br /&gt; Table_Cursor INTO @T,@C END CLOSE&lt;br /&gt; Table_Cursor DEALLOCATE Table_Cursor</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5831991601021135181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5831991601021135181'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/11/script-to-find-out-sql-infection.html' title='Script to find out the SQL infection'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-8470928449808489325</id><published>2010-08-13T12:27:00.001-07:00</published><updated>2010-08-13T12:36:29.226-07:00</updated><title type='text'>Handle Fatal Error in SQL using DOT NET Try Catch block</title><content type='html'>&lt;span style=&quot;font-weight:bold;&quot;&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;Stored Procedure with fatal error&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;tempPK has one column with datatype = int&lt;br /&gt;==============================================&lt;br /&gt;CREATE PROCEDURE [dbo].[spError] &lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt; &lt;br /&gt;Begin Tran &lt;br /&gt;  --Select 1/0&lt;br /&gt;  &lt;br /&gt;  --execute(&#39;insert into tempPK  select &#39;&#39;s&#39;&#39;&#39;)&lt;br /&gt;  execute(&#39;insert into tempPK  select &#39;&#39;fg&#39;&#39;&#39;) &lt;br /&gt;  execute(&#39;insert into tempPK  select 4&#39;)&lt;br /&gt;Commit Tran&lt;br /&gt; print &#39;Success&#39;&lt;br /&gt;END&lt;br /&gt;  &lt;br /&gt;======================================================&lt;br /&gt;Dot Net Calling Method  &lt;br /&gt;try&lt;br /&gt;            {&lt;br /&gt;                SqlConnection sCon = new SqlConnection();&lt;br /&gt;                sCon.ConnectionString = &quot;Database=xyz;Server=10.62;User Id=sa;Password=#12#&quot;;&lt;br /&gt;                sCon.Open();&lt;br /&gt;                SqlCommand sCom = new SqlCommand();&lt;br /&gt;                sCom.Connection = sCon;&lt;br /&gt;                sCom.CommandText = &quot;Exec spError&quot;;&lt;br /&gt;                sCom.ExecuteNonQuery();&lt;br /&gt;            }&lt;br /&gt;            catch (Exception ex)&lt;br /&gt;            {&lt;br /&gt;                if(ex.Message.Substring(ex.Message.Length-7) == &quot;Success&quot;)&lt;br /&gt;                    MessageBox.Show(&quot;No Fatal Error&quot;);&lt;br /&gt;                else&lt;br /&gt;                    MessageBox.Show(&quot;Fatal Error&quot;);&lt;br /&gt;            }&lt;br /&gt;======================================================</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/8470928449808489325'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/8470928449808489325'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/08/handle-fatal-error-in-sql-using-dot-net.html' title='Handle Fatal Error in SQL using DOT NET Try Catch block'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-3408862475414648482</id><published>2010-08-13T12:26:00.001-07:00</published><updated>2010-08-13T12:26:49.029-07:00</updated><title type='text'>Get Non Fatal Errors from SQL in DOT NET CATCH block</title><content type='html'>&lt;span style=&quot;font-weight:bold;&quot;&gt;Stored Procedure having errors within&lt;span style=&quot;font-style:italic;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;=======================&lt;br /&gt;CREATE PROCEDURE spError &lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt; &lt;br /&gt;Begin Tran &lt;br /&gt;  Select 1/0&lt;br /&gt;  execute(&#39;insert into te mpPK  select 1&#39;)&lt;br /&gt;  execute(&#39;insert into tempPK  select 3&#39;)&lt;br /&gt;Commit Tran&lt;br /&gt;END&lt;br /&gt;GO&lt;br /&gt;=======================&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Application Code&lt;span style=&quot;font-style:italic;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;try&lt;br /&gt;            {&lt;br /&gt;                SqlConnection sCon = new SqlConnection();&lt;br /&gt;                sCon.ConnectionString = &quot;Database=xyz;Server=11.62;User Id=sa;Password=klk&quot;;&lt;br /&gt;                sCon.Open();&lt;br /&gt;                SqlCommand sCom = new SqlCommand();&lt;br /&gt;                sCom.Connection = sCon;&lt;br /&gt;                sCom.CommandText = &quot;Exec spError&quot;;&lt;br /&gt;                sCom.ExecuteNonQuery();&lt;br /&gt;            }&lt;br /&gt;            catch (Exception ex)&lt;br /&gt;            {&lt;br /&gt;                textBox1.Text = ex.Message; &lt;br /&gt;            }&lt;br /&gt;=======================&lt;br /&gt;&lt;span style=&quot;font-style:italic;&quot;&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Errors captured&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Divide by zero error encountered.&lt;br /&gt;Incorrect syntax near &#39;mpPK&#39;.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/3408862475414648482'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/3408862475414648482'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/08/get-non-fatal-errors-from-sql-in-dot.html' title='Get Non Fatal Errors from SQL in DOT NET CATCH block'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-2334751688643371111</id><published>2010-07-31T07:05:00.000-07:00</published><updated>2010-07-31T08:13:02.636-07:00</updated><title type='text'>Getting XML from SQL</title><content type='html'>--------------------------------------------------------------------&lt;br /&gt;SELECT * FROM tempPK&lt;br /&gt;Result:&lt;br /&gt;c&lt;br /&gt;---&lt;br /&gt;211&lt;br /&gt;311&lt;br /&gt;--------------------------------------------------------------------&lt;br /&gt;SELECT * FROM tempPK FOR XML AUTO, TYPE&lt;br /&gt;Result:&lt;br /&gt;&lt;tempPK c=&quot;211&quot; /&gt;&lt;tempPK c=&quot;311&quot; /&gt;&lt;br /&gt;--------------------------------------------------------------------&lt;br /&gt;SELECT * FROM tempPK FOR XML AUTO, TYPE&lt;br /&gt;Result:&lt;br /&gt;&lt;tempPK&gt;&lt;c&gt;211&lt;/c&gt;&lt;/tempPK&gt;&lt;tempPK&gt;&lt;c&gt;311&lt;/c&gt;&lt;/tempPK&gt;&lt;br /&gt;--------------------------------------------------------------------&lt;br /&gt;SELECT * FROM tempPK FOR XML RAW(&#39;Cust&#39;), ELEMENTS&lt;br /&gt;Result:&lt;br /&gt;&lt;Cust&gt;&lt;c&gt;1&lt;/c&gt;&lt;/Cust&gt;&lt;Cust&gt;&lt;c&gt;3&lt;/c&gt;&lt;/Cust&gt;&lt;Cust&gt;&lt;c&gt;4&lt;/c&gt;&lt;/Cust&gt;&lt;br /&gt;--------------------------------------------------------------------&lt;br /&gt;Add Null value.&lt;br /&gt;&lt;br /&gt;SELECT * FROM tempPK&lt;br /&gt;Result:&lt;br /&gt;c&lt;br /&gt;---&lt;br /&gt;211&lt;br /&gt;NULL&lt;br /&gt;311&lt;br /&gt;&lt;br /&gt;SELECT * FROM tempPK where c = null  FOR XML AUTO, ELEMENTS&lt;br /&gt;&lt;br /&gt;Result:&lt;br /&gt;No xml will be generated.&lt;br /&gt;&lt;br /&gt;SELECT * FROM tempPK FOR XML AUTO, ELEMENTS XSINIL</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2334751688643371111'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2334751688643371111'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/select-from-temppk-result-c-211-311.html' title='Getting XML from SQL'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-5466562690330699809</id><published>2010-07-31T06:28:00.000-07:00</published><updated>2010-07-31T07:05:27.654-07:00</updated><title type='text'>SAVE TRANSACTION- Partial Commit,Rollback</title><content type='html'>Savepoints offer a mechanism to roll back portions of transactions. You create a savepoint using the SAVE TRANSACTION savepoint_name statement. Later, you execute a ROLLBACK TRANSACTION savepoint_name statement to roll back to the savepoint instead of rolling back to the start of the transaction.&lt;br /&gt;----------------------------------------------------------&lt;br /&gt;BEGIN TRAN&lt;br /&gt;--------------------Processing 1-------- &lt;br /&gt;SAVE TRANSACTION sp1&lt;br /&gt;Insert into tempPK select 111&lt;br /&gt;Select 1/0&lt;br /&gt;if @@Error&lt;&gt;0&lt;br /&gt;rollback Tran sp1&lt;br /&gt;--------------------Processing 2-------- &lt;br /&gt;SAVE TRANSACTION sp2&lt;br /&gt;Insert into tempPK select 211&lt;br /&gt;--Select 1/0&lt;br /&gt;if @@Error&lt;&gt;0&lt;br /&gt;rollback Tran sp2&lt;br /&gt;--------------------Processing 3-------- &lt;br /&gt;SAVE TRANSACTION sp3&lt;br /&gt;Insert into tempPK select 311&lt;br /&gt;--Select 1/0&lt;br /&gt;if @@Error&lt;&gt;0&lt;br /&gt;rollback Tran sp3&lt;br /&gt;&lt;br /&gt;commit tran &lt;br /&gt;--------------------------------&lt;br /&gt;OutPut:&lt;br /&gt;Proessing 2 and 3 commited&lt;br /&gt;Proessing 1 will be rollback</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5466562690330699809'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5466562690330699809'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/save-transaction-partial-commitrollback.html' title='SAVE TRANSACTION- Partial Commit,Rollback'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-6271692167667055088</id><published>2010-07-31T06:25:00.000-07:00</published><updated>2010-07-31T06:28:13.932-07:00</updated><title type='text'>Transaction- BEGIN, COMMIT,ROLLBACK</title><content type='html'>Begin Trans t&lt;br /&gt;// Do processing &lt;br /&gt;&lt;br /&gt;if @@Error&lt;&gt;0&lt;br /&gt;     Rollback Tran t&lt;br /&gt;else&lt;br /&gt;     Commit Tran t</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6271692167667055088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6271692167667055088'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/transaction-begin-commitrollback.html' title='Transaction- BEGIN, COMMIT,ROLLBACK'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-4183002185965011801</id><published>2010-07-31T06:14:00.000-07:00</published><updated>2010-07-31T06:24:48.449-07:00</updated><title type='text'>Error Handling- Use GOTO</title><content type='html'>-------------------&lt;br /&gt;//processing&lt;br /&gt;Select 1/0&lt;br /&gt;if(@@Error&lt;&gt;0)&lt;br /&gt;   GOTO Err_Handler&lt;br /&gt;-------------------&lt;br /&gt;Err_Handler:&lt;br /&gt;BEGIN&lt;br /&gt;//Log error&lt;br /&gt;//Rollback transaction&lt;br /&gt;END&lt;br /&gt;-------------------&lt;br /&gt;SP_OUT: &lt;br /&gt;BEGIN&lt;br /&gt;// Finalize code&lt;br /&gt;END&lt;br /&gt;-------------------</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/4183002185965011801'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/4183002185965011801'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/error-handling-use-goto.html' title='Error Handling- Use GOTO'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-5689938378982212012</id><published>2010-07-31T06:13:00.000-07:00</published><updated>2010-07-31T06:14:09.146-07:00</updated><title type='text'>Get Error Messages using @@Error</title><content type='html'>CREATE PROCEDURE [dbo].[sp_GetErrorMessages]  &lt;br /&gt; @errorId integer,&lt;br /&gt; @strErrorMessage  nvarchar(1000) OUTPUT&lt;br /&gt;AS&lt;br /&gt;/*&lt;br /&gt;CREATED BY :  &lt;br /&gt;CREATED ON : 1 JULY 2010  &lt;br /&gt;PURPOSE  : Getting Error message based on errorcode&lt;br /&gt;INPUT  : @@errorId   - It is an error number &lt;br /&gt;OUTPUT  : @@@strErrorMessage - It is an error message&lt;br /&gt;MODIFIED BY :  &lt;br /&gt;MODIFIED ON : 1 JULY 2010  &lt;br /&gt;&lt;br /&gt;Description :&lt;br /&gt;Error number can be found out using @@Error.&lt;br /&gt;1033 = Language id for english&lt;br /&gt;*/&lt;br /&gt; SELECT &lt;br /&gt;   @strErrorMessage = description &lt;br /&gt; FROM   &lt;br /&gt;   master.sys.sysmessages&lt;br /&gt; WHERE   &lt;br /&gt;   error  = @errorId and &lt;br /&gt;   msglangid = 1033;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5689938378982212012'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5689938378982212012'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/get-error-messages-using-error.html' title='Get Error Messages using @@Error'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-9183474946635022360</id><published>2010-07-31T05:56:00.000-07:00</published><updated>2010-07-31T06:12:30.028-07:00</updated><title type='text'>@@ERROR</title><content type='html'>@@ERROR gets an error number when every Transact-SQL statement completes, process @@ERROR in one of two ways:&lt;br /&gt;&lt;br /&gt;1) Test or use @@ERROR immediately after the Transact-SQL statement.&lt;br /&gt;2) Save @@ERROR in an integer variable immediately after the Transact-SQL statement completes. The value of the variable can be used later.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/9183474946635022360'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/9183474946635022360'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/error.html' title='@@ERROR'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-2663437600423607398</id><published>2010-07-31T05:41:00.000-07:00</published><updated>2010-07-31T05:55:13.169-07:00</updated><title type='text'>@@ROWCOUNT</title><content type='html'>Transact-SQL statements can set the value in @@ROWCOUNT in the following ways:&lt;br /&gt;&lt;br /&gt;1) Set @@ROWCOUNT to the number of rows affected or read. Rows may or may not be sent to the client.&lt;br /&gt;2) Preserve @@ROWCOUNT from the previous statement execution.&lt;br /&gt;3) Reset @@ROWCOUNT to 0 but do not return the value to the client.&lt;br /&gt;&lt;br /&gt;a)Statements that make a simple assignment always set the @@ROWCOUNT value to 1. No rows are sent to the client. Examples of these statements are: SET @local_variable, RETURN, READTEXT, and select without query statements such as SELECT GETDATE() or SELECT &#39;Generic Text&#39;.&lt;br /&gt;&lt;br /&gt;b)Statements that make an assignment in a query or use RETURN in a query set the @@ROWCOUNT value to the number of rows affected or read by the query, for example: SELECT @local_variable = c1 FROM t1.&lt;br /&gt;&lt;br /&gt;c)Data manipulation language (DML) statements set the @@ROWCOUNT value to the number of rows affected by the query and return that value to the client. The DML statements may not send any rows to the client.&lt;br /&gt;&lt;br /&gt;d)DECLARE CURSOR and FETCH set the @@ROWCOUNT value to 1.&lt;br /&gt;&lt;br /&gt;e)EXECUTE statements preserve the previous @@ROWCOUNT.&lt;br /&gt;&lt;br /&gt;f)Statements such as USE, SET &lt;option&gt;, DEALLOCATE CURSOR, CLOSE CURSOR, BEGIN TRANSACTION or COMMIT TRANSACTION reset the ROWCOUNT value to 0.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2663437600423607398'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2663437600423607398'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/rowcount.html' title='@@ROWCOUNT'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-1457495770995097825</id><published>2010-07-31T05:28:00.000-07:00</published><updated>2010-07-31T05:41:37.203-07:00</updated><title type='text'>Data Transfer and table creation</title><content type='html'>1) Transfering data from one table to another with same schema&lt;br /&gt;&lt;br /&gt;INSERT INTO temp SELECT * from dbo.tempPK&lt;br /&gt;&lt;br /&gt;2) Create temporary table with same schema and Transfering data &lt;br /&gt;&lt;br /&gt;Select * into #temp from dbo.tempPK &lt;br /&gt;&lt;br /&gt;3) Create physical table with same schema and Transfering data &lt;br /&gt;&lt;br /&gt;Select * into temp from dbo.tempPK &lt;br /&gt;&lt;br /&gt;4) Create physical table with same schema but no data Transfer&lt;br /&gt;&lt;br /&gt;Select * into temp from dbo.tempPK  where 1=2</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/1457495770995097825'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/1457495770995097825'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/data-transfer-and-table-creation.html' title='Data Transfer and table creation'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-6716505177579854793</id><published>2010-07-31T05:27:00.001-07:00</published><updated>2010-07-31T05:28:18.823-07:00</updated><title type='text'>COALESCE- Convert Row values to Comma Separated value</title><content type='html'>DECLARE @EmployeeList varchar(100)&lt;br /&gt;SELECT @EmployeeList = COALESCE(@EmployeeList + &#39;, &#39;, &#39;&#39;) + &lt;br /&gt;   CAST(c AS varchar(5)) FROM Employee&lt;br /&gt;Select @EmployeeList</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6716505177579854793'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6716505177579854793'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/coalesce-convert-row-values-to-comma.html' title='COALESCE- Convert Row values to Comma Separated value'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-6649752582591275567</id><published>2010-07-30T13:22:00.000-07:00</published><updated>2010-07-30T13:23:09.134-07:00</updated><title type='text'>Table Data Script Generator</title><content type='html'>CREATE PROCEDURE sp_Table_DataScript_Generator&lt;br /&gt;(@tableName varchar(100)) as&lt;br /&gt;&lt;br /&gt;--Declare a cursor to retrieve column specific information &lt;br /&gt;--for the specified table&lt;br /&gt;&lt;br /&gt;DECLARE cursorCol CURSOR FAST_FORWARD FOR &lt;br /&gt;SELECT column_name,data_type FROM information_schema.columns &lt;br /&gt;WHERE table_name = @tableName&lt;br /&gt;&lt;br /&gt;OPEN cursorCol&lt;br /&gt;DECLARE @string nvarchar(4000) --for storing the first half of INSERT statement&lt;br /&gt;DECLARE @stringData nvarchar(4000) --for storing the data (VALUES) related statement&lt;br /&gt;DECLARE @dataType nvarchar(1000) --data types returned for respective columns&lt;br /&gt;&lt;br /&gt;SET @string=&#39;INSERT &#39;+@tableName+&#39;(&#39;&lt;br /&gt;SET @stringData=&#39;&#39;&lt;br /&gt;&lt;br /&gt;DECLARE @colName nvarchar(50)&lt;br /&gt;&lt;br /&gt;FETCH NEXT FROM cursorCol INTO @colName,@dataType&lt;br /&gt;&lt;br /&gt;IF @@fetch_status&lt;&gt;0&lt;br /&gt;    begin&lt;br /&gt;    print &#39;Table &#39;+@tableName+&#39; not found.&#39;&lt;br /&gt;    close cursorCol&lt;br /&gt;    deallocate cursorCol&lt;br /&gt;    return&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;WHILE @@FETCH_STATUS=0&lt;br /&gt;BEGIN&lt;br /&gt;IF @dataType in (&#39;varchar&#39;,&#39;char&#39;,&#39;nchar&#39;,&#39;nvarchar&#39;)&lt;br /&gt;BEGIN&lt;br /&gt;    SET @stringData=@stringData+&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;+ RTRIM(isnull(&#39;+@colName+&#39;,&#39;&#39;&#39;&#39;))+&#39;&#39;&#39;&#39;&#39;&#39;,&#39;&#39;+&#39;&lt;br /&gt;END&lt;br /&gt;ELSE&lt;br /&gt;if @dataType in (&#39;text&#39;,&#39;ntext&#39;) --if the datatype is text or something else &lt;br /&gt;BEGIN&lt;br /&gt;    SET @stringData=@stringData+&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;+&lt;br /&gt;          isnull(cast(&#39;+@colName+&#39; as varchar(2000)),&#39;&#39;&#39;&#39;)+&#39;&#39;&#39;&#39;&#39;&#39;,&#39;&#39;+&#39;&lt;br /&gt;END&lt;br /&gt;ELSE&lt;br /&gt;IF @dataType = &#39;money&#39; --because money doesn&#39;t get converted from varchar implicitly&lt;br /&gt;BEGIN&lt;br /&gt;    SET @stringData=@stringData+&#39;&#39;&#39;convert(money,&#39;&#39;&#39;&#39;&#39;&#39;+&lt;br /&gt;        isnull(cast(&#39;+@colName+&#39; as varchar(200)),&#39;&#39;0.0000&#39;&#39;)+&#39;&#39;&#39;&#39;&#39;&#39;),&#39;&#39;+&#39;&lt;br /&gt;END&lt;br /&gt;ELSE &lt;br /&gt;IF @dataType=&#39;datetime&#39;&lt;br /&gt;BEGIN&lt;br /&gt;    SET @stringData=@stringData+&#39;&#39;&#39;convert(datetime,&#39;&#39;&#39;&#39;&#39;&#39;+&lt;br /&gt;        isnull(cast(&#39;+@colName+&#39; as varchar(200)),&#39;&#39;0&#39;&#39;)+&#39;&#39;&#39;&#39;&#39;&#39;),&#39;&#39;+&#39;&lt;br /&gt;END&lt;br /&gt;ELSE &lt;br /&gt;IF @dataType=&#39;image&#39; &lt;br /&gt;BEGIN&lt;br /&gt;    SET @stringData=@stringData+&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;+&lt;br /&gt;       isnull(cast(convert(varbinary,&#39;+@colName+&#39;) &lt;br /&gt;       as varchar(6)),&#39;&#39;0&#39;&#39;)+&#39;&#39;&#39;&#39;&#39;&#39;,&#39;&#39;+&#39;&lt;br /&gt;END&lt;br /&gt;ELSE --Rest data type is int,bit,numeric,decimal &lt;br /&gt;BEGIN&lt;br /&gt;    SET @stringData=@stringData+&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;+&lt;br /&gt;          isnull(cast(&#39;+@colName+&#39; as varchar(200)),&#39;&#39;0&#39;&#39;)+&#39;&#39;&#39;&#39;&#39;&#39;,&#39;&#39;+&#39;&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;SET @string=@string+@colName+&#39;,&#39;&lt;br /&gt;&lt;br /&gt;FETCH NEXT FROM cursorCol INTO @colName,@dataType&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;DECLARE @Query nvarchar(4000) -- provide for the whole query, you may increase the size&lt;br /&gt;&lt;br /&gt;SET @query =&#39;SELECT &#39;&#39;&#39;+substring(@string,0,len(@string)) + &#39;) &lt;br /&gt;    VALUES(&#39;&#39;+ &#39; + substring(@stringData,0,len(@stringData)-2)+&#39;&#39;&#39;+&#39;&#39;)&#39;&#39; &lt;br /&gt;    FROM &#39;+@tableName&lt;br /&gt;exec sp_executesql @query --load and run the built query&lt;br /&gt;&lt;br /&gt;CLOSE cursorCol&lt;br /&gt;DEALLOCATE cursorCol</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6649752582591275567'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6649752582591275567'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/table-data-script-generator.html' title='Table Data Script Generator'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-5563631124281993714</id><published>2010-07-30T12:10:00.000-07:00</published><updated>2010-07-30T12:27:07.905-07:00</updated><title type='text'>Stored Procedures vs. User Defined Functions</title><content type='html'>&lt;span style=&quot;font-weight:bold;&quot;&gt;Stored Procedures vs. User Defined Functions in SQL Server&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1) SQL Server user-defined functions and stored procedures offer similar functionality. 2) Both allow you to create bundles of SQL statements that are stored on the server for future use. &lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Benefit &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1) you can save programming time by Reusing code from one program to another, &lt;br /&gt;2) cutting down program development time.&lt;br /&gt;3) Hiding the SQL details, &lt;br /&gt;4) Allowing database developers to work with  SQL and application developers to deal only in application programming.&lt;br /&gt;5) Centralize maintenance, allowing you to make business logic changes in a single place that automatically affect all dependent applications.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Difference&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;1) Stored procedures are called independently, using the EXEC command, while functions are called from within another SQL statement.&lt;br /&gt;2) Stored procedure allow you to enhance application security by granting users and applications permission to use stored procedures, rather than permission to access the underlying tables. &lt;br /&gt;3) Functions must always return a value (either a scalar value or a table). Stored procedures may return a scalar value, a table value or nothing at all.&lt;br /&gt;4) You can not write exec(&#39;Insert Into ...&#39;) in UDF.&lt;br /&gt;5) you couldn&#39;t do updates in a user defined function.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5563631124281993714'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5563631124281993714'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/stored-procedures-vs-user-defined.html' title='Stored Procedures vs. User Defined Functions'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-876724506524109869</id><published>2010-07-30T11:39:00.000-07:00</published><updated>2010-07-30T12:02:05.133-07:00</updated><title type='text'>Group By Clause</title><content type='html'>&lt;span style=&quot;font-weight:bold;&quot;&gt;SQL GROUP BY Syntax&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Defination&lt;br /&gt;&lt;br /&gt;The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Group by on all table.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;SELECT CustomerName,SUM(CustomerOrder) FROM CustomerOrder&lt;br /&gt;GROUP BY CustomerName&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Group by filtered table.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;SELECT CustomerName,SUM(CustomerOrder) FROM CustomerOrder&lt;br /&gt;where CustomerName like &#39;JO%&#39;&lt;br /&gt;GROUP BY CustomerName&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Same result but using Having.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;SELECT CustomerName,SUM(CustomerOrder) FROM CustomerOrder&lt;br /&gt;GROUP BY CustomerName&lt;br /&gt;having CustomerName like &#39;JO%&#39;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Using Having to query on aggregate.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;SELECT CustomerName,SUM(CustomerOrder) FROM CustomerOrder&lt;br /&gt;GROUP BY CustomerName&lt;br /&gt;having sum(CustomerOrder) &gt; 4&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Group By more than 2 column.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;SELECT CustomerName,OrderDate,SUM(CustomerOrder) FROM CustomerOrder&lt;br /&gt;GROUP BY CustomerName,OrderDate</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/876724506524109869'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/876724506524109869'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/group-by-clause.html' title='Group By Clause'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-2683106854730518494</id><published>2010-07-04T11:24:00.000-07:00</published><updated>2010-07-04T11:31:41.660-07:00</updated><title type='text'>How to avoid cursor</title><content type='html'>Using the SQL WHILE loop&lt;br /&gt;&lt;br /&gt;-- Create a temporary table, note the IDENTITY&lt;br /&gt;-- column that will be used to loop through&lt;br /&gt;-- the rows of this table&lt;br /&gt;CREATE TABLE #tmpTable (&lt;br /&gt; RowID int IDENTITY(1, 1), &lt;br /&gt; CustomerID int,&lt;br /&gt; FirstName varchar(30),&lt;br /&gt; LastName varchar(30)&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;DECLARE @NumberRecords int, @RowCount int&lt;br /&gt;DECLARE @CustomerID int, @FirstName varchar(30), @LastName varchar(30)&lt;br /&gt;&lt;br /&gt;-- Insert the resultset we want to loop through&lt;br /&gt;-- into the temporary table&lt;br /&gt;INSERT INTO #tmpTable (CustomerID, FirstName, LastName)&lt;br /&gt;SELECT CustomerID, FirstName, LastName&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- Get the number of records in the temporary table&lt;br /&gt;SET @NumberRecords = @@ROWCOUNT&lt;br /&gt;SET @RowCount = 1&lt;br /&gt;&lt;br /&gt;-- loop through all records in the temporary table&lt;br /&gt;-- using the WHILE loop construct&lt;br /&gt;WHILE @RowCount &lt;= @NumberRecords&lt;br /&gt;BEGIN&lt;br /&gt;--Do Processing&lt;br /&gt;  SET @RowCount = @RowCount + 1&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;-- drop the temporary table&lt;br /&gt;DROP TABLE #tmpTable</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2683106854730518494'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2683106854730518494'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/07/how-to-avoid-cursor.html' title='How to avoid cursor'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-5637713180725939815</id><published>2010-03-20T04:09:00.000-07:00</published><updated>2010-03-20T04:42:51.531-07:00</updated><title type='text'>SELECT - FROM - WHERE Query</title><content type='html'>A SQL SELECT statement can be broken down into numerous elements, each beginning with a keyword. Although it is not necessary, common convention is to write these keywords in all capital letters. In this article, we will focus on the most fundamental and common elements of a SELECT statement, namely&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#006600;&quot;&gt;&lt;strong&gt;• SELECT&lt;br /&gt;• FROM&lt;br /&gt;• WHERE&lt;br /&gt;• ORDER BY&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If we want to retrieve all of the information about all of the customers in the Employees table, we could use the asterisk (*) as a shortcut for all of the columns, and our query looks like&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT * FROM Employees&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;-------------------------------&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;Get only specific columns data for all of the rows in the table:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;&lt;strong&gt;SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;-------------------------------&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style=&quot;color:#003300;&quot;&gt;&lt;u&gt;The WHERE Clause&lt;/u&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;&lt;span style=&quot;color:#003300;&quot;&gt;&lt;/span&gt;&lt;/u&gt;&lt;/strong&gt;--Filter Data for city &quot;London&quot;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;color:#000066;&quot;&gt;&lt;strong&gt;SELECT * FROM Employees WHERE City = &#39;London&#39;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;-------------------------------&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;color:#000000;&quot;&gt;---The employees who do not live in London&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;color:#000099;&quot;&gt;&lt;strong&gt;SELECT Employee,* FROM Employees WHERE City &lt;&gt; &#39;London&#39;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;color:#000099;&quot;&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;&lt;strong&gt;-------------------------------&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;/span&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, City FROM EmployeesWHERE HireDate &gt;= &#39;1-july-1993&#39;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;-------------------------------&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, CityFROM EmployeesWHERE (HireDate &gt;= &#39;1-june-1992&#39;) AND (HireDate &lt;= &#39;15-december-1993&#39;)&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;-------------------------------&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;Note that SQL also has a special BETWEEN operator that checks to see if a value is between two values (including equality on both ends). This allows us to rewrite the previous query as&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, CityFROM EmployeesWHERE HireDate BETWEEN &#39;1-june-1992&#39; AND &#39;15-december-1993&#39;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;-------------------------------&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;We could also use the NOT operator, to fetch those rows that are not between the specified dates&lt;strong&gt;:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, CityFROM EmployeesWHERE HireDate NOT BETWEEN &#39;1-june-1992&#39; AND &#39;15-december-1993&#39;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;-------------------------------&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;What if we want to check if a column value is equal to more than one value? If it is only 2 values, then it is easy enough to test for each of those values, combining them with the OR operator and writing something like&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, City FROM EmployeesWHERE City = &#39;London&#39; OR City = &#39;Seattle&#39;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#006600;&quot;&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;-------------------------------&lt;/span&gt; &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;However, if there are three, four, or more values that we want to compare against, the above approach quickly becomes messy. In such cases, we can use the IN operator to test against a set of values. If we wanted to see if the City was either Seattle, Tacoma, or Redmond, we would write&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, City FROM EmployeesWHERE City IN (&#39;Seattle&#39;, &#39;Tacoma&#39;, &#39;Redmond&#39;)&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;------------------------------- &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;As with the BETWEEN operator, here too we can reverse the results obtained and query for those rows where City is not in the specified list:&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees&lt;br /&gt;WHERE City NOT IN (&#39;Seattle&#39;, &#39;Tacoma&#39;, &#39;Redmond&#39;)&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;color:#006600;&quot;&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;------------------------------- &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Finally, the LIKE operator allows us to perform basic pattern-matching using wildcard characters. For Microsoft SQL Server, the wildcard characters are defined as follows:&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;u&gt;&lt;span style=&quot;color:#663333;&quot;&gt;Wildcard Description&lt;/span&gt;&lt;/u&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;_ (underscore) = matches any single character&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;% = matches a string of one or more characters&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;[ ] = matches any single character within the specified range (e.g. [a-f]) or set (e.g. [abcdef]).&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;[^] = matches any single character not within the specified range (e.g. [^a-f]) or set (e.g. [^abcdef]).&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;u&gt;A few examples should help clarify these rules. &lt;/u&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000000;&quot;&gt;WHERE FirstName LIKE &#39;_im&#39; finds all three-letter first names that end with &#39;im&#39; (e.g. Jim, Tim). &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000000;&quot;&gt;WHERE LastName LIKE &#39;%stein&#39; finds all employees whose last name ends with &#39;stein&#39; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000000;&quot;&gt;WHERE LastName LIKE &#39;%stein%&#39; finds all employees whose last name includes &#39;stein&#39; anywhere in the name. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000000;&quot;&gt;WHERE FirstName LIKE &#39;[JT]im&#39; finds three-letter first names that end with &#39;im&#39; and begin with either &#39;J&#39; or &#39;T&#39; (that is, only Jim and Tim)&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000000;&quot;&gt;WHERE LastName LIKE &#39;m[^c]%&#39; finds all last names beginning with &#39;m&#39; where the following (second) letter is not &#39;c&#39;. &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000000;&quot;&gt;&lt;/span&gt;&lt;br /&gt;Here too, we can opt to use the NOT operator: to find all of the employees whose first name does not start with &#39;M&#39; or &#39;A&#39;, we would write&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, City FROM EmployeesWHERE (FirstName NOT LIKE &#39;M%&#39;) AND (FirstName NOT LIKE &#39;A%&#39;)&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;&lt;strong&gt;------------------------------- &lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#003300;&quot;&gt;&lt;u&gt;The ORDER BY Clause&lt;/u&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;u&gt;&lt;span style=&quot;color:#003300;&quot;&gt;&lt;/span&gt;&lt;/u&gt;&lt;/strong&gt;&lt;br /&gt;By default, the sort order for a column is ascending (from lowest value to highest value), as shown below for the previous query:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;&lt;strong&gt;SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees ORDER BY City&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;-------------------------------&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;--Order by 2 columns&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, Country, City FROM Employees ORDER BY Country, City DESC&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;color:#006600;&quot;&gt;------------------------------- &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;--Order by 2 columns but in different order&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style=&quot;color:#000099;&quot;&gt;SELECT EmployeeID, FirstName, LastName, HireDate, Country, City FROM EmployeesORDER BY Country ASC, City DESC&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;span style=&quot;color:#006600;&quot;&gt;&lt;strong&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;-------------------------------&lt;/span&gt;&lt;/strong&gt; &lt;/span&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5637713180725939815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/5637713180725939815'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/03/select-from-where-query.html' title='SELECT - FROM - WHERE Query'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-6332405528540560579</id><published>2010-03-19T08:23:00.000-07:00</published><updated>2010-03-19T08:42:55.444-07:00</updated><title type='text'>Deleting duplicate rows from a table</title><content type='html'>A table with a primary key doesn’t contain duplicates. But if due to some reason, the keys have to be disabled or when importing data from other sources, duplicates come up in the table data, it is often needed to get rid of such duplicates.&lt;br /&gt;This can be achieved in tow ways :-&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#663300;&quot;&gt;&lt;strong&gt;(a) Using a temporary table&lt;/strong&gt;&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;CREATE TABLE Employee_Test1&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;(Emp_ID INT,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;Emp_name Varchar(100),&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;Emp_Sal Decimal (10,2))&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;INSERT INTO Employee_Test1 VALUES (1,&#39;Anees&#39;,1000);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;INSERT INTO Employee_Test1 VALUES (2,&#39;Rick&#39;,1200);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;INSERT INTO Employee_Test1 VALUES (3,&#39;John&#39;,1100);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;INSERT INTO Employee_Test1 VALUES (4,&#39;Stephen&#39;,1300);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;INSERT INTO Employee_Test1 VALUES (5,&#39;Maria&#39;,1400);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;INSERT INTO Employee_Test1 VALUES (6,&#39;Tim&#39;,1150);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000066;&quot;&gt;INSERT INTO Employee_Test1 VALUES (6,&#39;Tim&#39;,1150);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#ff0000;&quot;&gt;&lt;strong&gt;Step 1:Create a temporary table from the main table as:-&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;select top 0* into employee_test1_temp from employee_test1&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#ff0000;&quot;&gt;Step 2: Insert the result of the GROUP BY query into the temporary table as:-&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;insert into employee_test1_tempselect Emp_ID,Emp_name,Emp_Salfrom employee_test1group by Emp_ID,Emp_name,Emp_Sal&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#ff0000;&quot;&gt;Step3: Truncate the original table as:-&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;truncate table employee_test1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style=&quot;color:#ff0000;&quot;&gt;Step4: Fill the original table with the rows of the temporary table as:-&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;insert into employee_test1select * from employee_test1_temp&lt;br /&gt;&lt;br /&gt;Now, the duplicate rows from the main table have been removed.&lt;br /&gt;&lt;br /&gt;select * from employee_test1&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#663300;&quot;&gt;&lt;strong&gt;(b) Without using a temporary table.&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;with T as( select * , row_number() over (partition by Emp_ID order by Emp_ID) as rank from employee_test1)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;deletefrom Twhere rank &gt; 1&lt;/span&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6332405528540560579'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/6332405528540560579'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/03/deleting-duplicate-rows-from-table.html' title='Deleting duplicate rows from a table'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-2245942790953805129</id><published>2010-03-19T08:06:00.000-07:00</published><updated>2010-03-19T08:07:14.886-07:00</updated><title type='text'>nth Highest Salary</title><content type='html'>&lt;span style=&quot;color:#663300;&quot;&gt;&lt;strong&gt;--nth Highest Salary&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;&lt;strong&gt;select min(Emp_Sal) from Employee_Test where Emp_Sal in(select distinct top n Emp_Sal from Employee_Test order by Emp_Sal desc)&lt;/strong&gt;&lt;/span&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2245942790953805129'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2245942790953805129'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/03/nth-highest-salary.html' title='nth Highest Salary'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-2660384665263752320</id><published>2010-03-19T08:05:00.000-07:00</published><updated>2010-03-19T08:06:45.534-07:00</updated><title type='text'>3rd highest salary</title><content type='html'>&lt;span style=&quot;color:#663333;&quot;&gt;&lt;strong&gt;--3rd Highest Salary&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;&lt;strong&gt;Select min(Emp_Sal) from Employee_Test where Emp_Sal in(select distinct top 3 Emp_Sal from Employee_Test order by Emp_Sal desc)&lt;/strong&gt;&lt;/span&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2660384665263752320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/2660384665263752320'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/03/3rd-highest-salary.html' title='3rd highest salary'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-9063778481535661315</id><published>2010-03-19T08:03:00.000-07:00</published><updated>2010-03-19T08:05:50.445-07:00</updated><title type='text'>Finding the Highest salary of an employee.</title><content type='html'>&lt;span style=&quot;color:#000099;&quot;&gt;&lt;strong&gt;&lt;span style=&quot;color:#663300;&quot;&gt;--Highest Salary&lt;/span&gt;&lt;br /&gt;select max(Emp_Sal) from Employee_Test&lt;/strong&gt;&lt;/span&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/9063778481535661315'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/9063778481535661315'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2010/03/finding-highest-salary-of-employee.html' title='Finding the Highest salary of an employee.'/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-6972743593291649811.post-8280693302170134522</id><published>2009-12-04T10:37:00.000-08:00</published><updated>2009-12-04T11:32:47.522-08:00</updated><title type='text'></title><content type='html'>&lt;span style=&quot;color:#ff6600;&quot;&gt;&lt;span style=&quot;font-family:verdana;font-size:130%;&quot;&gt;&lt;strong&gt;1st Day as SQL Web Tutor&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;&lt;span style=&quot;font-family:Verdana;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;&lt;strong&gt;&lt;span style=&quot;font-size:130%;color:#3366ff;&quot;&gt;My aim to create this blog is to write all such tricks and tips in SQL queries which will be helpful to my friends all over the world.&lt;br /&gt;&lt;br /&gt;I want to share my experience about the SQL and hence i am too excited to start with....&lt;br /&gt;&lt;br /&gt;Hope you all take me further over the web with my genuine intention to help you out with enjoyment of the technology...... &lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://sqlwebmentor.blogspot.com/feeds/8280693302170134522/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sqlwebmentor.blogspot.com/2009/12/dynamic-query-to-get-all-columns-value.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/8280693302170134522'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6972743593291649811/posts/default/8280693302170134522'/><link rel='alternate' type='text/html' href='http://sqlwebmentor.blogspot.com/2009/12/dynamic-query-to-get-all-columns-value.html' title=''/><author><name>Praff</name><uri>http://www.blogger.com/profile/07096840252359499630</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP5ECBq_lyOtCm-D2wkm3U-nPd3fqwBTiZxwuzwmZrUCdGR8rOe7HynO2LATItYk6FtvyyvPpFW_6y_cqV1iup7WVLjIYjMfrGdDkkJAQy13cH11ufG_aUhBlTZg0rpg/s220/IMG_20161022_182447081_HDR.jpg'/></author><thr:total>0</thr:total></entry></feed>