<feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
    <title>Nagendra Prasad</title>
    <link rel="self" type="application/xml" href="http://geekswithblogs.net/nagendraprasad/Atom.aspx" />
    <subtitle type="html"> </subtitle>
    <id>http://geekswithblogs.net/nagendraprasad/Default.aspx</id>
    <author>
        <name>nagendraprasad</name>
        <uri>http://geekswithblogs.net/nagendraprasad/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 0.0.0.0">Subtext</generator>
    <updated>2009-08-21T13:21:15Z</updated>
    <entry>
        <title>Reduce size/Shrink Transaction Log of the DB - SQL Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/08/21/reduce-sizeshrink-transaction-log-of-the-db---sql-server.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/08/21/reduce-sizeshrink-transaction-log-of-the-db---sql-server.aspx</id>
        <published>2009-08-21T13:21:15-12:00:00</published>
        <updated>2009-08-21T13:21:15Z</updated>
        <content type="html">&lt;p&gt;In most of the DBs in the server will have more space, when the backup is used to store it in the development environment or testing environment, it might not have much data, but it will occupy more space physically.&lt;/p&gt;
&lt;p&gt;To reduce the database size, then there is a way to reduce it. Shrink the Transactional Log. There are two ways of shrinking the Transaction Log. One is through Front-End and another is manual script.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Front-End:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Databases -&amp;gt; DbName. Right click on the DbName, then go to Tasks-&amp;gt;Shrink-&amp;gt;Files.&lt;br /&gt;
Here both the Data and Log files can be shrinked. you can see the amount of unused space in this window.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Script:&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;-- Shrink the Transaction Log &lt;br /&gt;
&lt;font color="#0000ff"&gt;USE&lt;/font&gt; DatabaseName &lt;br /&gt;
GO &lt;br /&gt;
&lt;font color="#0000ff"&gt;DBCC&lt;/font&gt; SHRINKFILE(&amp;lt;TransactionLogName&amp;gt;, 1) &lt;br /&gt;
&lt;font color="#0000ff"&gt;BACKUP&lt;/font&gt; &lt;font color="#ff00ff"&gt;LOG&lt;/font&gt; &amp;lt;DatabaseName&amp;gt; &lt;font color="#0000ff"&gt;WITH&lt;/font&gt; TRUNCATE_ONLY &lt;br /&gt;
&lt;font color="#0000ff"&gt;DBCC&lt;/font&gt; SHRINKFILE(&amp;lt;TransactionLogName&amp;gt;, 1) &lt;br /&gt;
GO &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;If you have any problems/confusions with the TransactionLogName, it will be stored in the table and it can be retrieved using:&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff"&gt;select * from&lt;/font&gt; sys.database_files &lt;/p&gt;
&lt;p&gt;I came across this issue, I had the database backup of 3GB .bak file. Once it is restored in the development environment, it occupies 80GB of space in which Transactional Log is nearly 70GB. I resolved it by doing the above. &lt;/p&gt;
&lt;p&gt;I just want to share this, so that anyone else facing the same problem may get benefit out of this.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/134241.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/134241.aspx</wfw:comment>
        <slash:comments>2</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/134241.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/134241.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Restore DB with mdf file alone - SQL Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/08/21/restore-db-with-mdf-file-alone---sql-server.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/08/21/restore-db-with-mdf-file-alone---sql-server.aspx</id>
        <published>2009-08-21T13:06:07-12:00:00</published>
        <updated>2009-08-21T13:06:07Z</updated>
        <content type="html">&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: -webkit-monospace; WHITE-SPACE: pre-wrap; FONT-SIZE: 13px" class="Apple-style-span"&gt;Basically, we need two files to restore the database. They are .mdf and .ldf files. There is another way to restore the database, if we have the database backup file(.bak). But we can restore using one file(.mdf file) as well. Here's how it will be restored using .mdf file.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;-- Restore Database using MDF File alone &lt;br /&gt;
&lt;font color="#0000ff"&gt;EXEC&lt;/font&gt; &lt;font color="#993300"&gt;sp_attach_single_file_db&lt;/font&gt; @dbname = &lt;font color="#ff0000"&gt;'testerDB'&lt;/font&gt;, &lt;br /&gt;
@physname = &lt;font color="#ff0000"&gt;'c:\testDB.mdf'&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: -webkit-monospace; WHITE-SPACE: pre-wrap; FONT-SIZE: 13px" class="Apple-style-span"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: -webkit-monospace; WHITE-SPACE: pre-wrap; FONT-SIZE: 13px" class="Apple-style-span"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 16px &amp;quot;Times New Roman&amp;quot;; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="FONT-FAMILY: -webkit-monospace; WHITE-SPACE: pre-wrap; FONT-SIZE: 13px" class="Apple-style-span"&gt;Hope this might help.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/134240.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/134240.aspx</wfw:comment>
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/134240.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/134240.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Find Position of a particular string in SQL Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/03/31/find-position-of-a-particular-string-in-sql-server.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/03/31/find-position-of-a-particular-string-in-sql-server.aspx</id>
        <published>2009-03-31T16:24:52-12:00:00</published>
        <updated>2009-03-31T16:24:52Z</updated>
        <content type="html">To find a position of particular string/Char in the field of table, it is done using patindex. It might be well known for most of them. But what i found in that may be known to only few people. I want to share with everyone.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Table: tblNames&lt;/span&gt; &lt;br /&gt;
SELECT Name FROM tblNames&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;
Name&lt;/span&gt;&lt;br /&gt;
Nagendra&lt;br /&gt;
John&lt;br /&gt;
Edward&lt;br /&gt;
&lt;br /&gt;
3 row(s) affected.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Example 1:&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt; To find Char or String, here is the way. ( Many of them know this )&lt;br /&gt;
SELECT PATINDEX('John', name) as Pos, Name FROM tblNames&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Pos  Name&lt;/span&gt;&lt;br /&gt;
0       Nagendra&lt;br /&gt;
1       John&lt;br /&gt;
0       Edward&lt;br /&gt;
&lt;br /&gt;
3 row(s) affected.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Example 2:&lt;/span&gt; To find particular Char or String in between the word. for eg., you need to look 'ohn' exists in any of the row. Then the above example will not work. it will throw the position as 0. It can be done using the wild card symbol '%'&lt;br /&gt;
&lt;br /&gt;
SELECT PATINDEX('ohn', name) as Pos,PATINDEX('%ohn%', name) as PosNew, Name FROM tblNames&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Pos  &lt;/span&gt; &lt;span style="font-weight: bold;"&gt;PosNew  &lt;/span&gt;&lt;span style="font-weight: bold;"&gt;Name&lt;/span&gt;&lt;br /&gt;
0        0               Nagendra&lt;br /&gt;
0       2               John&lt;br /&gt;
0        0               Edward&lt;br /&gt;
&lt;br /&gt;
3 row(s) affected.&lt;br /&gt;
From the above example, it returned the position as 2(&lt;span style="font-weight: bold;"&gt;PosNew&lt;/span&gt;) with the wild card symbol example. But in the first one, it returned 0(&lt;span style="font-weight: bold;"&gt;Pos&lt;/span&gt;).&lt;br /&gt;
In simple, Behaviour of &lt;span style="font-weight: bold;"&gt;PATINDEX &lt;/span&gt;will be same as &lt;span style="font-weight: bold;"&gt;LIKE&lt;/span&gt; , where &lt;span style="font-weight: bold;"&gt;LIKE&lt;/span&gt; is used in Where clause and &lt;span style="font-weight: bold;"&gt;PATINDEX &lt;/span&gt;is used for search of a string/Char.&lt;br /&gt;
&lt;br /&gt;
This can be used for search of a character or specific word according to your requirement.&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/130557.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/130557.aspx</wfw:comment>
        <slash:comments>5</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/130557.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/130557.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Export SQL Server Data into Excel</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/03/26/export-sql-server-data-into-excel.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/03/26/export-sql-server-data-into-excel.aspx</id>
        <published>2009-03-26T11:32:28-12:00:00</published>
        <updated>2009-03-26T11:33:46Z</updated>
        <content type="html">Lets see how to export SQL Server data into Excel. &lt;br /&gt;
First we have to enable the Ad Hoc Distributed Queries option. &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Enable Ad Hoc Distributed Queries&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
EXEC sp_configure 'show advanced options', 1;&lt;br /&gt;
GO&lt;br /&gt;
RECONFIGURE;&lt;br /&gt;
GO&lt;br /&gt;
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;&lt;br /&gt;
GO&lt;br /&gt;
RECONFIGURE;&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
After enabling it, we can proceed further to write the query for exporting the SQL Serer Data.&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Export into Excel Query:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 8.0;Database=C:\testing.xls;', 'SELECT Name, Email FROM [Sheet1$]') &lt;br /&gt;
SELECT Name, Email FROM tblnames&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
Before executing the query, we need to do few steps:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Provide a Excel sheet path and Create a excel sheet as mentioned in the path. For eg., i have mentioned "&lt;span style="font-weight: bold;"&gt;C:\testing.xls&lt;/span&gt;" created testing.xls in the C:\&lt;/li&gt;
    &lt;li&gt;Fill the header row with the columns we provided in the query. For eg., i have Name and Email as columns. I have entered the column names in the excel as show below&lt;/li&gt;
&lt;/ul&gt;
Excel File -&lt;span style="font-weight: bold;"&gt; testing.xls   &lt;/span&gt;                        
&lt;table cellspacing="1" cellpadding="1" border="1" align="" style="width: 292px; height: 69px;" summary=""&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td style="font-weight: bold; background-color: rgb(192, 192, 192);"&gt;&lt;br /&gt;
            &lt;/td&gt;
            &lt;td style="font-weight: bold; background-color: rgb(192, 192, 192);"&gt;A&lt;/td&gt;
            &lt;td style="font-weight: bold; background-color: rgb(192, 192, 192);"&gt;B&lt;/td&gt;
            &lt;td style="background-color: rgb(192, 192, 192);"&gt;&lt;span style="font-weight: bold;"&gt;C&lt;/span&gt;     &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="font-weight: bold; background-color: rgb(192, 192, 192);"&gt;1&lt;/td&gt;
            &lt;td style="font-weight: bold;"&gt;Name&lt;/td&gt;
            &lt;td style="font-weight: bold;"&gt;Email&lt;/td&gt;
            &lt;td&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="font-weight: bold; background-color: rgb(192, 192, 192);"&gt;2&lt;/td&gt;
            &lt;td&gt; &lt;/td&gt;
            &lt;td&gt; &lt;/td&gt;
            &lt;td&gt; &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;br /&gt;
&lt;img src="file:///C:/DOCUME~1/C_PRAS~1/LOCALS~1/Temp/moz-screenshot-1.jpg" alt="" /&gt;&lt;img src="file:///C:/DOCUME~1/C_PRAS~1/LOCALS~1/Temp/moz-screenshot.jpg" alt="" /&gt;
&lt;ul&gt;
    &lt;li&gt;Enter the provider in the query according to the Microsoft Excel version you installed in your version. For eg., i provided &lt;span style="font-weight: bold;"&gt;Microsoft.ACE.OLEDB.12.0&lt;/span&gt; since i have installed Microsoft Office 2007. This provider works for MS Office 2003 and above. For MS Office 2002 and earlier versions, provider will be &lt;span style="font-weight: bold;"&gt;Microsoft.Jet.OLEDB.4.0&lt;/span&gt;. (Please check it).&lt;/li&gt;
    &lt;li&gt;If the sheet name of the Excel sheet varies from the default, then you need to change that in the query. For eg., i have used [Sheet1$] since i did not change the sheet name in my excel file.&lt;/li&gt;
&lt;/ul&gt;
Once everything is done, you can execute the above query and you will have the SQL Server data exported into your excel file.&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Note: &lt;/span&gt;If you fail to create the excel file as mentioned in the query or fail to create the column names as mentioned in the query, you may get the error below:&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Error:&lt;/span&gt;&lt;br /&gt;
OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.".&lt;br /&gt;
Msg 7303, Level 16, State 1, Line 2&lt;br /&gt;
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/130451.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/130451.aspx</wfw:comment>
        <slash:comments>10</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/130451.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/130451.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Wait/Delay in SQL Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/03/25/waitdelay-in-sql-server.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/03/25/waitdelay-in-sql-server.aspx</id>
        <published>2009-03-25T14:50:05-12:00:00</published>
        <updated>2009-03-25T14:50:05Z</updated>
        <content type="html">If you have two queries and you have to execute one after another with some amount of delay, then that is possible in SQL Server.&lt;br /&gt;
&lt;br /&gt;
Lets say, your first query fills up the table with some records. Lets assume that, you will get some responses based on this records which will fill other table. Now your second query should run based on the responses table. Then you can make a delay for quite amount of time.&lt;br /&gt;
&lt;br /&gt;
Lets see a simple example of using waitfor and delay in SQL Server:&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;
Syntax:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
WAITFOR DELAY 'HH:MM:SS'&lt;br /&gt;
&lt;br /&gt;
where HH:MM:SS - Hours, minutes and seconds respectively.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Example:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
SELECT GETDATE() CurrentTime&lt;br /&gt;
WAITFOR DELAY '00:00:05'&lt;br /&gt;
SELECT GETDATE() CurrentTime&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Output:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;CurrentTime&lt;/span&gt;&lt;br /&gt;
2009-03-25 14:47:&lt;span style="font-weight: bold;"&gt;13&lt;/span&gt;.797&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;CurrentTime&lt;/span&gt;&lt;br /&gt;
2009-03-25 14:47:&lt;span style="font-weight: bold;"&gt;18&lt;/span&gt;.800&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/130423.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/130423.aspx</wfw:comment>
        <slash:comments>3</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/130423.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/130423.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Get dynamic number of rows using TOP in a SQL query - SQL Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/03/24/get-dynamic-number-of-rows-using-top-in-a-sql.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/03/24/get-dynamic-number-of-rows-using-top-in-a-sql.aspx</id>
        <published>2009-03-24T16:15:04-12:00:00</published>
        <updated>2009-03-24T16:15:04Z</updated>
        <content type="html">If you want to get the n number of rows dynamically, then we need use variable. It can be achieved by two different methods.&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Method 1:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
DECLARE @MyCount VARCHAR(5)&lt;br /&gt;
DECLARE @sql NVARCHAR(200) -- note nvarchar datatype should be used, since it is for dynamic query&lt;br /&gt;
&lt;br /&gt;
SELECT @MyCount =2&lt;br /&gt;
SELECT @sql = 'SELECT TOP ' + @MyCount + ' * FROM TBLNAMES'&lt;br /&gt;
&lt;br /&gt;
EXEC sp_executesql @sql&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Method 2:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
DECLARE @MyCount int&lt;br /&gt;
&lt;br /&gt;
SET @MyCount = 2&lt;br /&gt;
SELECT TOP (@MyCount) * FROM TBLNAMES;&lt;br /&gt;
&lt;br /&gt;
First method would be the standard method, many people would use it.But i would recommend second method, since it has not many variables used and the @MyCount  variable is used as int datatype over here.&lt;br /&gt;
In first method also we can use int datatype, but we need to convert it to string when it comes to a query since it is dynamic query.&lt;br /&gt;
&lt;br /&gt;
Any comments are welcome.&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/130378.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/130378.aspx</wfw:comment>
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/130378.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/130378.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Copy a table into new table with/without data - SQL Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/03/20/copy-a-table-into-new-table-with-and-without-data.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/03/20/copy-a-table-into-new-table-with-and-without-data.aspx</id>
        <published>2009-03-20T14:01:23-12:00:00</published>
        <updated>2009-03-20T14:04:19Z</updated>
        <content type="html">Lets see how to copy an existing table to new table in SQL Server. There are two options. They are&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Copy only the structure of an existing table into new table&lt;br /&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
    &lt;li&gt;Copy only the structure with data of an existing table into new table&lt;/li&gt;
&lt;/ul&gt;
&lt;span style="font-weight: bold;"&gt;Copy only the structure of an existing table into new table:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
SELECT * INTO tblNew FROM tblOld WHERE 1=2&lt;br /&gt;
&lt;br /&gt;
The above query will copy the structure of  an existing table(tblOld) into the new table(tblNew).&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Copy only the structure with data of an existing table into new table:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
SELECT * INTO tblNew FROM tblOld&lt;br /&gt;
&lt;br /&gt;
This is also same like the previous query, but it copies the structure of existing table(tblOld) with data as well into the new table(tblNew).&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/130269.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/130269.aspx</wfw:comment>
        <slash:comments>16</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/130269.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/130269.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Date Custom Format - SQL Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/03/19/date-custom-format---sql-server.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/03/19/date-custom-format---sql-server.aspx</id>
        <published>2009-03-19T16:29:43-12:00:00</published>
        <updated>2009-03-19T16:29:43Z</updated>
        <content type="html">As everyone knows, DATE can be formatted in different ways and the usual way will be using CONVERT&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;
Syntax:&lt;/span&gt;&lt;br /&gt;
SELECT CONVERT(VARCHAR(30), GETDATE(), 106)&lt;br /&gt;
&lt;br /&gt;
Where as 106 can be replaced by 101, 110, etc according to the requirements.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Custom Format:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If we need custom date format(19-March-2009), which is not a pre-defined format in CONVERT, then consider the below:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Syntax:&lt;/span&gt;&lt;br /&gt;
SELECT DATENAME(DAY,GETDATE()) + '-' + DATENAME(MONTH,GETDATE()) + '-' + DATENAME(YEAR, GETDATE()) &lt;br /&gt;
&lt;br /&gt;
I know, this is just a small thing. But still it might help someone who is in search of requirement like this !&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/130243.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/130243.aspx</wfw:comment>
        <slash:comments>4</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/130243.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/130243.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Formatting number to add leading zeros - SQL Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/03/19/formatting-number-to-add-leading-zeros---sql-server.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/03/19/formatting-number-to-add-leading-zeros---sql-server.aspx</id>
        <published>2009-03-19T12:15:35-12:00:00</published>
        <updated>2009-03-19T12:15:35Z</updated>
        <content type="html">Formatting numbers to add leading zeros can be done in SQL Server. It is just simple. Lets create a new table and see how it works:&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE Numbers(Num INT);&lt;br /&gt;
&lt;br /&gt;
Table Created.&lt;br /&gt;
&lt;br /&gt;
Lets insert few values and see:&lt;br /&gt;
&lt;br /&gt;
    INSERT Numbers VALUES('12');&lt;br /&gt;
    INSERT Numbers VALUES('112');&lt;br /&gt;
    INSERT Numbers VALUES('12');&lt;br /&gt;
    INSERT Numbers VALUES('122');&lt;br /&gt;
    INSERT Numbers VALUES('122');&lt;br /&gt;
&lt;br /&gt;
1 row(s) affected.&lt;br /&gt;
1 row(s) affected.&lt;br /&gt;
1 row(s) affected.&lt;br /&gt;
1 row(s) affected.&lt;br /&gt;
1 row(s) affected.&lt;br /&gt;
&lt;br /&gt;
Now we can see how the numbers are formatted with 6 digits, if it has less than 6 digits it will add leading zeros.&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Data&lt;/span&gt;:&lt;br /&gt;
&lt;br /&gt;
SELECT * FROM Numbers;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Num&lt;/span&gt;&lt;br /&gt;
12&lt;br /&gt;
112&lt;br /&gt;
12&lt;br /&gt;
122&lt;br /&gt;
122&lt;br /&gt;
&lt;br /&gt;
5 row(s) affected.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Formatting:&lt;/span&gt;&lt;br /&gt;
SELECT RIGHT('00000'+ CONVERT(VARCHAR,Num),6) AS NUM FROM Numbers;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;NUM&lt;/span&gt;&lt;br /&gt;
000012&lt;br /&gt;
000112&lt;br /&gt;
000012&lt;br /&gt;
000122&lt;br /&gt;
000122&lt;br /&gt;
&lt;br /&gt;
5 row(s) affected.&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/130232.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/130232.aspx</wfw:comment>
        <slash:comments>58</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/130232.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/130232.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Getting NULL values instead of data while concatenation - SQL Server</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/nagendraprasad/archive/2009/03/17/getting-null-values-instead-of-data-while-concatenation---sql.aspx" />
        <id>http://geekswithblogs.net/nagendraprasad/archive/2009/03/17/getting-null-values-instead-of-data-while-concatenation---sql.aspx</id>
        <published>2009-03-17T15:38:11-12:00:00</published>
        <updated>2009-03-17T15:38:11Z</updated>
        <content type="html">I have to concatenate first name with last name. I got too many NULL values which it should not. Then I found the reason behind it.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Data in TblNames :&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;FIRST_NAME  LAST_NAME&lt;/span&gt;&lt;br /&gt;
Edward            Jones&lt;br /&gt;
Jason               NULL&lt;br /&gt;
John                 Smith&lt;br /&gt;
Robert              NULL       &lt;br /&gt;
&lt;br /&gt;
4 row(s) affected.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Expected Result:&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;NAME&lt;/span&gt;&lt;br /&gt;
Edward  Jones&lt;br /&gt;
Jason&lt;br /&gt;
John Smith&lt;br /&gt;
Robert&lt;br /&gt;
&lt;br /&gt;
4 row(s) affected.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Before finding the solution:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;&lt;/span&gt; SELECT FIRST_NAME + ' ' + LAST_NAME AS [NAME] FROM TBLNAMES;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;NAME&lt;/span&gt;&lt;br /&gt;
Edward  Jones&lt;br /&gt;
NULL&lt;br /&gt;
John Smith&lt;br /&gt;
NULL&lt;br /&gt;
&lt;br /&gt;
5 row(s) affected.&lt;br /&gt;
&lt;br /&gt;
I realized the if any of the columns(&lt;span style="font-weight: bold;"&gt;FIRST_NAME&lt;/span&gt; or&lt;span style="font-weight: bold;"&gt; LAST_NAME&lt;/span&gt;) is &lt;span style="font-weight: bold;"&gt;NULL&lt;/span&gt;, it returns the &lt;span style="font-weight: bold;"&gt;NULL &lt;/span&gt;value. To resolve this, set the concat_null_yields_null to OFF.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Solution:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
SET CONCAT_NULL_YIELDS_NULL OFF;&lt;br /&gt;
SELECT FIRST_NAME + ' ' + LAST_NAME AS [NAME] FROM TBLNAMES;&lt;br /&gt;
SET CONCAT_NULL_YIELDS_NULL ON;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;NAME&lt;/span&gt;&lt;br /&gt;
Edward  Jon&lt;br /&gt;
Jason&lt;br /&gt;
John Smithes&lt;br /&gt;
Robert&lt;br /&gt;
&lt;br /&gt;
4 row(s) affected.&lt;br /&gt;
&lt;br /&gt;
Hope it would help you.&lt;img src="http://geekswithblogs.net/nagendraprasad/aggbug/130162.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/nagendraprasad/comments/130162.aspx</wfw:comment>
        <slash:comments>2</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/nagendraprasad/comments/commentRss/130162.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/nagendraprasad/services/trackbacks/130162.aspx</trackback:ping>
    </entry>
</feed>
