Search

Monday, November 14, 2011

Difference between TRUNCATE command and DELETE Command


There are lots of differences in Delete and Truncate Command. Some are listed below:

  1. TRUNCATE is much faster than DELETE. Reason for thsi is : When you type DELETE.all the data get copied into the Rollback Tablespace first.then delete operation get performed. For this reason when you type ROLLBACK after deleting a table ,you can get back the data(The system get it for you from the Rollback Tablespace).All this process take time.But when you type TRUNCATE,it removes data directly without copying it into the Rollback Tablespace. For this reason TRUNCATE is faster. Once you Truncate you cann't get back the data.
  2. You cann't rollback in TRUNCATE but in DELETE you can rollback.
  3. TRUNCATE is a DDL command whereas DELETE is a DML command.
  4. TRUNCATE removes the record permanently. but in DELETE you can get back the data from ROLLBACK
  5. In case of TRUNCATE ,Trigger doesn't get fired.But in DML commands like DELETE .Trigger get fired.
  6. You cann't use conditions(WHERE clause) in TRUNCATE.But in DELETE you can write conditions using WHERE clause.
  7. Truncate resets identity(seed). delete will not.

Saturday, November 12, 2011

The query has exceeded the maximum number of result sets that can be displayed in the results grid. Only the first 100 result sets are displayed

If you get this error message in your SQL Server then read on:
In "Results to Grid" format one cannot display more than 100 resultset.  So just change the result format to either "Result to Text" or "Result to File" option
Example to reproduce this error:
Step 1: Ctrl + D (setting to Grid format for demo purpose)
Step 2: Try to print the current datetime for 101 times for example
Select GetDate()
Go 101
You would see the error as we are trying to display more than 100 resultset in a "Result to Grid" format.
Just in case you thought 100 is the limitation of GO command in SQL Server try the below script
Create table TestTable
(
SLno int identity,
Firstname varchar(50)
)
Go
Insert into TestTable values ('Testing')
Go 101


For easy reference:
1. Ctrl + T :: Result will be displayed in Text mode
2. Ctrl + D :: Result will be displayed in Grid mode
3. Ctrl + Shift + F :: Result will be saved to a File

Friday, November 11, 2011

Know space distribution by table

We can use sys.dm_db_partition_stats DMV to find out this information
select  OBJECT_NAME(object_id) as objname
        , SUM (reserved_page_count) * 8192/ 1024 asreserved_kb
        , SUM(used_page_count) * 8192 / 1024 as used_kb
from sys.dm_db_partition_stats
group by OBJECT_NAME(object_id)
order by reserved_kb desc
Before SQL Server 2005 We can use the below query to find space used by tables: 
if object_id ('tempdb..#table') is not null
  drop table #table

create table #table (
  name varchar(8000)
, rows int, reserved varchar(50)
, data varchar(50)
, index_size varchar(50)
, unused varchar(50))

insert into #table
exec sp_msforeachtable 'sp_spaceused ''?'''

select name, rows, reserved = REPLACE (reserved, 'KB','')
, data = REPLACE (data, 'KB',''),index_size = REPLACE(index_size, 'KB','')
,unused = REPLACE (unused, 'KB','')
from #table
order by convert ( int , REPLACE (reserved, 'KB','')) desc

drop table #table

Search for Wild Card Characters in a Column

First create a table


CREATE TABLE Table_1 (CustID int, CustName varchar(35))
go
--Now Populat the table with some data
DECLARE @iCtr INT
SET @iCtr =0
WHILE @iCtr <= 100
BEGIN
INSERT INTO Table_1 VALUES (@iCtr, REPLICATE('X',30))
SET @iCtr = @iCtr + 1
END
--Add a Wild Card to one records
UPDATE Table_1 SET CustName = 'Arun % Ladha' WHERE CustID = 5


Now the table is created and I had updated one record with [%] wild card character. Now I want to search all the record where this Wild Card Character is there. There are many ways to do this. I am showing two of them.

--In this first query we have defined a '\' as they escape character which means the next character will be treated as literal
SELECT * FROM Table_1 WHERE CustName LIKE '%\%%' ESCAPE '\'
--In this second query the [] brackets tell the database engine to treat % as a normal character and not a wildcard
SELECT * FROM Table_1 WHERE CustName LIKE '%[%]%'
--both queries return the same results


Both Queries return the same results. In the first query we have defined ‘/’ as the escape character which the next character following the escape character will be treated as literal and not a wild card. In the second query we used the [] brackets to tell the database engine to treat the % as a normal character and not a wild card.

Thursday, November 10, 2011

Delete all Data from all tables in a database


Run below query to delete all records from all tables:
EXEC sp_MSforeachtable @command1 = “DELETE FROM ?”
OR
EXEC sp_MSforeachtable @command1 = “TRUNCATE TABLE ?”
We cannot delete data from all tables if there are foreign key references. To resolve this we have to disable all constraint than run the above query. To disable all constraint run below query:

EXEC sp_MSForEachTable ‘ALTER TABLE ? NOCHECK CONSTRAINT ALL’
Now run the query to delete data.