28.3.10

Rename the table in SQL Server

sp_rename is used to rename the objects.

The syntax is,

sp_rename "Old Object Name","New Object Name"

Code Sample:


drop table Venkat_Table
create table Venkat_Table (id int)
-- Rename the existing table.
exec sp_rename 'Venkat_Table','Venkat_Table_New'
-- To check whether the table is available or not.
-- OR to check the properties of the table.
sp_help 'Venkat_Table_New'

Cheers,

Venkatesan Prabu .J

27.3.10

Delete duplicate records in SQL Server

==========================================================================================
/* TABLE WITH MULTIPLE COLUMNS*/
==========================================================================================
Considering this table is a stand alone table with multiple columns and there is no identity.
Select the distict values into temp table, drop the existing table and rename the temp table into the original table.
==========================================================================================
drop table venkat
create table venkat(id int ,nam varchar(100))
insert into venkat values (1,'Arun')
insert into venkat values (1,'Arun')
insert into venkat values (1,'Arun')
insert into venkat values (2,'Arun1')
insert into venkat values (2,'Arun1')
select * from venkat
-- GET DISTINCT RECORDS INTO TEMP TABLE.
SELECT DISTINCT * INTO temp_Venkattable FROM venkat
-- DROP EXISTING TABLE.
DROP TABLE venkat
-- RENAME THE NEW TABLE TO EXISTING TABLE.
EXEC sp_rename 'temp_Venkattable','venkat'
select * from venkat
-- IF THE TABLE IS NOT A STAND ALONE TABLE, YOU CAN'T DO THE ABOVE PROCESS. BEFORE ACHIEVING IT,
--YOU NEED TO CONSIDER ALL THE DEPENDENCIES ON THE TABLE
==========================================================================================
/* TABLE WITH IDENTITY COLUMN*/
==========================================================================================

Considering this table having an identity column. In that case, our work is too simple.
==========================================================================================

drop table venkat
create table venkat(id int IDENTITY ,nam varchar(100))
insert into venkat values ('Arun')
insert into venkat values ('Arun')
insert into venkat values ('Arun')
insert into venkat values ('Arun1')
insert into venkat values ('Arun1')
select * from venkat
DELETE FROM VENKAT WHERE ID NOT IN
(SELECT MIN(ID) FROM VENKAT GROUP BY NAM)
select * from venkat
==========================================================================================
/* TABLE WITH SINGLE COLUMNS*/
==========================================================================================
drop table venkat
create table venkat(nam varchar(100))
insert into venkat values ('Arun')
insert into venkat values ('Arun')
insert into venkat values ('Arun')
insert into venkat values ('Arun1')
insert into venkat values ('Arun1')
select * from venkat
-- CTE IS THE VERY BEST OPTION TO DELETE THE DUPLICATE RECORDS IN SQL SERVER 2005
with cte as (
select row_number() over ( partition by nam order by nam ) as id ,nam from venkat
)
delete from cte where id !=1
==========================================================================================

14.3.10

SSIS Error

I got a chance to connect Oracle database from SQL Server. Below are the steps done,


1. Created an Oracle DB connection.

2. Created an Execute SQL Task.

3. Select the DB connection and typed the query needed.

"DELETE FROM TABLE WHERE ID=10;"



OOPS!!! I got the below error message,



Error: ORA-00911: invalid character



On analysing this issue, You should not have the ";" character at the end. Just remove it and try it. That's nice to hear the word... working fine.

Cheers,
Venkatesan Prabu .J

9.3.10

SQL Server 2000 DTS Designer components

I got a chance to work with SQL Server 2000 package migration to SQL Server 2005,
While trying to open SQL 2000 DTS package in SQL Server 2005, I got the below error message,
"SQL Server 2000 DTS Designer components are required to edit DTS packages. Install the special Web download, "SQL Server 2000 DTS Designer Components" to use this feature."

I think, am missing something in my installation. Googled a little bit, found some interesting stuffs.

Microsoft is advising us to install SQLServer2005_DTS.msi located in the below path,
http://www.microsoft.com/downloads/details.aspx?familyid=d09c1d60-a13c-4479-9b91-9e8b9d835cdc&displaylang=en&displaylang=en

or else, you can try some other alternative by hacking your sqlwb manifest file,

http://support.microsoft.com/default.aspx/kb/917406

Happy Learning!!!
Cheers,
Venkatesan Prabu .J

Interview question on real time data restoring

Sent: Thursday, 11 February 2010 11:34 AMTo: Venkatesan Prabu JSubject: SQL server 2005 help
Hi Venkatesan,

I am member of dot net spider and I require a help from you.
Please provide your help in this regard.

Recently, I went for an interview where they asked me this question

Question: Assume that I have a flat file with more than a million records. I have to insert this in the production database without performance hit for the users? The application is a 24 X 7 application.

My answer: Since we want to do the insert in production database, we would check if there are any maintenance window available. If one is available we would do it during the window by using bulk copy or import data or some schedule job which would read the file and insert into the table.

But the interviewer asked me how do you do it when there are no maintenance window. my answere now was, it is risky to do in production database when users are using the application. But still if you want to continue, try inserting small chunks (say 1000 records). Lock the table, insert 1000 records and release the lock, so that the user would not feel the performance hit.

But still the interviewer was not convinced and expected something more.

Since i am new to sql server 2005, can u pls help me by giving a solution to this scenario.

Thanks
Saravanan


---------------------------------------------------------------------------------------------
Sent: Mon, 1 March, 2010 3:55:10 PMSubject: RE: SQL server 2005 help
Dear Saravanan,

Your answer is not correct.

1. You should not lock the table which is available for 24/7.
2. You should have another table(With same schema and everything) with all the data from the existing table. Insert all the records into the table.
3. Rename the existing table with temp in the prefix.
4. Rename the new table with old table using sp_rename option.

Probably, I will post a detailed explanation in my blog. Hope, you are happy with this explanation.

Thanks and Regards,
Venkatesan Prabu .J
Microsoft SQL Server MVP/HCL SQL Knowledge Champion/DNS MVM Award Winner
MCITP/MCTS/MCAD/CCNA/QAI
B.B Certified Asp.net/C#/RDBMS/Networking professional
E.R certified Networking candidate

http://venkattechnicalblog.blogspot.com/
----------------------------------------------------------------------------------------------

Sent: Thursday, 4 March 2010 12:15 PMTo: Venkatesan Prabu JSubject: Re: SQL server 2005 help
Dear Venkatesan,

Thanks a lot for your reply.
I really appreciate in taking your valuable time to respond to my query.

However, i have one doubt in your solution. If we do the manipulation in the temporary table, any transaction that happened to the original table would be lost right.

Thanks
Saravanan

----------------------------------------------------------------------------------------------
Sent: Thu, 4 March, 2010 2:47:17 PMSubject: RE: SQL server 2005 help

Dear Saravanan,

That's a very nice question.

For this kind of huge work, always there will be a little hickup which we can't control.

considering the table name is VenkatTable.

1. Put your table as VenkatTable_New
2. Rename the existing table as VenkatTable_Old
3. Rename the table VenkatTable_New as VenkatTable.
4. Migrate the new data from VenkatTable_Old to VenkatTable.

Problem arises, If you have constraints. In that case,

1. Disable the constraints before doing this and enable the constraints after your process.

Enjoy....

Thanks and Regards,
Venkatesan Prabu .J
Microsoft SQL Server MVP/HCL SQL Knowledge Champion/DNS MVM Award Winner
MCITP/MCTS/MCAD/CCNA/QAI
B.B Certified Asp.net/C#/RDBMS/Networking professional
E.R certified Networking candidate


http://venkattechnicalblog.blogspot.com/
-----------------------------------------------------------------------------------------------

Dear Venkat,

Thanks a lot for your response. That gave me a good idea.

Thanks
Saravanan

4.3.10

Add or subract month or year or day in SQL Server

Get Previous month:

Dateadd is a handy command to add or subtract the days/month/year in a specified date column.
The syntax is Dateadd(type,datecolumn)
type may be dd or mm or yy
Below is the query to get the current date:
select getdate()
I need the previous month (Same day). Then, the query should be modified as,
select dateadd(mm,-1,getdate()) -- dateadd(yy,1,getdate())
If I need to retrieve next year with same day and sae month. Then,
select dateadd(yy,1,getdate())



Cheers,
Venkatesan Prabu .J

Execution time of the stored procedure

How to find the execution time of a stored procedure?

There are two options to find the execution time of the stored procedure,

First Option:
Get the current time before execution. Execute the stored procedure. Get the current time after execution. Manipulate the difference in times which will provide you the exact execution time.

declare @startproc datetime
declare @endproc datetime
declare @time integer
select @startproc = getdate()
exec VenkatProc(Your procedure name)
select @endproc = getdate()
select @time = DATEDIFF(second, @startproc, @endproc)
print str(@time)

Second Option:
Get Use profiler to get the exact time taken + split up period like, time taken by each query + time taken to read or write.

Cheers,
Venkatesan Prabu .J