25.5.12

Option to access the Column Names in Data table


Here is the option to access the column (header) name of a datatable.

DataSet ds = new DataSet();
ds.Tables[0].Columns[0].ColumnName

Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx

Open multiple windows in browser startup. (Multiple homepages option)

If you want to open two sites simultaneously, there is an option available in IE. Just go to Options page of your IE browser and provide multiple URL’s. Click “Apply” and “Ok”. Close the browser and start the browser once again. Eureka, hope you are able to see two tabs opening simultaneously.





Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx

20.5.12

Upgrading the version from Evaluation to Standard/Enterprise/Developer version

For the people who are using evaluation version of Microsoft SQL Server, you will face the below problem after a certain period. While trying to access SSMS. You will get this error.
Evaluation period has expired. For information on how to upgrade your evaluation software please go to http://www.microsoft.com/sql/howtobuy (http://www.microsoft.com/sql/howtobuy)

There are two ways to fix the above error message:
1. Install Service Pack 1 before upgrading SQL Server 2008 evaluation edition to any licensed edition.
2. Modify Registry. Go to Start->Run->Regedit. In the Registry Editor, select the following keyHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100\ConfigurationState and change the value of the Common Files REG_DWORD to 3 as shown in the figure below.
http://msdn.microsoft.com/en-us/library/ms143393.aspx




After completing the registry changes, run the edition upgrade and you are done. For a list of supported version and Edition upgrades, check out the following:




You need to provide appropriate keys and install the software or upgrade the software successfully.

Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx

15.5.12

Query to Create comma separated Output.





Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx

How can we force SQL Server to use a specific index when executing a query?





Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx

Query in one of the forum:



using Below sample query(2 times same query with one condition as extra) to get the 2 type of result set.

IF @Status = 'A'
Select * From TableName where ID = @ID and Value > 200
Else
Select * From TableName where ID = @ID

How can i use Case statement to avoid to create the same select statement 2 times.


Here is my answer to achieve the same.





Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx 


REBUILDING VS REORGANIZE THE INDEX


When an index is rebuilt, it is dropped and a new one is created.
In the process fragmentation is removed and disk space is reclaimed.

ALTER INDEX PK_CustId ON Sales.Customer REBUILD

REORGANIZE THE INDEX
Minor Level modification, so that rearranging of the tree node is done.
 Small Fragmentation is removed, the index remains.

ALTER INDEX PK_CustId ON Sales.Customer REORGANIZE

Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx 

Variable in Top Clause (Dynamic value for the top clause):


Suppose, If we want to pass the arguments for getting top number of records we can achieve it by declaring the variable and using the variable as below.




Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx 


Get the Top Data with Ties from SQL Server (If there are same data….Ties in data will happen. If we need all the data.)


In this scenario you need to use With Ties option





In the second query, I’ve given Top 3 data. But the third and fourth data is having a tie up which inturn enables the query engine to fetch both the data which are in Ties.

·         WITH TIES OPTION MAY CAUSE MORE THAN THE SPECIFIED NUMBER OF RECORDS TO BE DISPLAYED

·         BECAUSE IT DISPLAYS ALL OF THE RECORDS MATCHING THE LAST RECORD'S ORDER BY CLAUSE

Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx 










Get the Top Data from SQL Server



Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx 


How to use dynamic sql for a nvarchar of more than 4000 characters?


Usually, it's a typical problem we will face while executing the Dynamic SQL the length of the string is really huge when compared to the max length of the string. To overcome this major issue. Here is a small alternative. We can have part of the execution string in one variable and remaining part in the other string.
  A small example

find columns having index?
 sp_helpindex  tblUserDetails


sp_help tblUserDetails





EXEC sp_msforeachtable @command1= "PRINT '?'  
EXEC sp_helpindex @objname = '?'"


Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx 

SET or SELECT -- is there any performance difference?


It doesn't have major performance difference between the Set and Select statement. The only thing is the usability nature of the commands.

            SET @variable = (SELECT datacolumn FROM tablename)
The above statement should return a single value which will be assigned to the variable. Here, if the results of the subquery expression is empty then @variable has a value of NULL but if the results of the subquery are more than one row then you get a error.
And thus it makes sense while,

SELECT @variable = columnvalue FROM dbo.tablename

returns a value from a number of rows and we are not sure what’s the criteria in selecting that row.

 One major difference in assigning using SET and SELECT is:

SET @variable1 = 'somevalue', @variable2 = 'someothervalue'

This is NOT possible but the same is possible with SELECT as in:

SELECT @variable1 = 'somevalue', @variable2 = 'someothervalue' 

Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx 

Difference between Stored Procedure (SP) and User Defined Function


Ø   User Defined Function can be executed using the “SELECT” clause whereas Stored Procedure cannot be Executed..
Ø    User Defined Function does not return any output parameters while Stored Procedure will return output parameters.
Ø   User Defined Function cannot make permanent changes to server environments while SP’s can change some of the server environment.
Ø   If there is an error in UDF its stops executing. But in SP’s it just ignores the error and moves to the next statement.

Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx 

14.5.12

Chief Guest for Jaya Engineering College, Faculty Development program



Chief Guest for Jaya Engineering College, Faculty Development program on Microsoft Technologies

Program Coverage : 

      Session 1:  Talk on Latest  Microsoft Technologies (WCF, WPF, Cloud Computing, Dotnet Framework, Denali, Web Matrix, Windows Server 2008)

      Session 2:  Talk on basic C# and Dotnet Framework Concepts.



My talk about Cloud Computing (SQL Azure and WinAzure):


Talk about Denali and it's new Features:





It was a fantastic session and I really enjoyed the arrangements and facilities provided for this program. Had a great lunch :-) and came back to my office.

Thanks to Anna University and Jaya Engineering College for arranging this session.

Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
For free Ebook : http://kaashivinfotech.com/Ebooks.aspx

10.5.12

Join tables from different databases

This can be achieved by joining the tables or indicating the tables using Fully Qualified names.




SELECT t1.id, t1.name


FROM AdventureWorks.dbo.Venkat_Table1 t1


INNER JOIN


AdventureWorksLT2008R2.dbo.Venkat_Table2 t2


ON t1.id = t2.id



Programmatically find out when SQL Server service is started

1. First Option is to check when the tempdb database is created. This is the exact time at which the services were restarted.





SELECT crdate AS 'SQL Server service started Time'


FROM master.dbo.sysdatabases


WHERE name = 'tempdb'




2. Second Option is the information about the database startup will be available while executing the command,


exec xp_readerrorlog

 
 
                       

Find the execution time of a stored procedure

The best way is to use SQL profiler. Just choose the required events.Else if you dont have the permission to access SQL Profiler or want to determine that programatically, then try:


DECLARE @StartDate datetime, @EndDate datetime
SET @StartDate = getdate()

/*

--Exec your stored procedure code here--

*/

SET @EndDate = getdate()
SELECT datediff(ms,@StartDate, @EndDate) AS 'Milliseconds'

Avoiding prefix 'sp_' in the name of user stored procedure

By nature, if the stored procedure is created with the prefix sp_. SQL Server will start search for the stored procedure in the master database. After wards, it will search for the local database which in turn a major problem. To avoid this,



We need to access the stored procedure using Fully Qualified Name,

DatabaseName.SchemaName.ProcedureName

Deleting backup and restore history

Usually, backup and restore history will be available in the MSDB database(Microsoft Database). It’s advisable to cleanup the database after archiving the databases.




Scenario: Considering, am having daily backups and after two years the database will be archived. In that case, the backup and restore information which is happened before two years need not be maintained and we can go for cleanup. In that case, we are using the below command to cleanup the database. You need to specify the exact dates. So that, data before that dates will be cleaned up.



USE msdb


EXEC sp_delete_backuphistory '1/31/2011'

4.5.12

Get Recent records from the table

Here is a small code snippet for getting the recent records from the table.

drop table VENKAT_TABLE
go
create table Venkat_Table(code varchar(100),rate decimal(10,8),date1 date)
go
insert into VENKAT_TABLE values ('sam', 1.240000, '01/08/2004')
insert into VENKAT_TABLE values ('sam', 1.840000, '02/08/2004')
insert into VENKAT_TABLE values ('pal', 1.240000, '01/08/2005')
insert into VENKAT_TABLE values ('pal', 0.840000, '10/08/2003')

select code,rate,date1 from
(
select ROW_NUMBER() over (partition by code order by date1) as rank1,*
  from VENKAT_TABLE ) t
  where rank1=1
 
Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech