Tuesday 4 October 2011

Very fast Clearing of a Table That has an Indexed View

The new feature allows very fast clearing of a table that has an indexed view on it by creating a staging table (call it T1_Stage) with the same structure as our T1 table, switching T1 to T1_Stage, and then dropping T1_Stage. All this can be done without dropping the view or even the index on it. Here’s the code to achieve the task:

CREATE TABLE dbo.T1_STAGE

(

  col1 INT NOT NULL PRIMARY KEY,

  col2 INT NOT NULL,

  col3 NUMERIC(12, 2) NOT NULL

);



ALTER TABLE dbo.T1 SWITCH TO dbo.T1_STAGE;



DROP TABLE dbo.T1_STAGE;

Monday 3 October 2011

Check Temp Table

IF OBJECT_ID('tempdb..#temp') > 0          
     DROP TABLE #temp 
 
     SELECT * INTO  #temp FROM (SELECT * FROM TABLENAME)

     SELECT * FROM  #temp
     SELECT COUNT(*) FROM  #temp






Sunday 2 October 2011

MsSqlserver 2008 Cursor Example


DECLARE @RevFormatID INT
DECLARE @Centerid INT

DECLARE  @vin CURSOR
SET @vin= CURSOR  FOR  
SELECT DISTINCT ReportingCenterID
FROM vCenterSelect           
WHERE ISNULL(RevFormatID,1)=  1

OPEN @vin             
             
FETCH NEXT             
FROM @vin INTO @CenterId 
WHILE @@FETCH_STATUS = 0         
BEGIN   

SELECT UserId,CenterID from TblUserCenterMapping WHERE CenterID=@Centerid
            
FETCH NEXT             
FROM @vin INTO @CenterId   
END             
CLOSE @vin             
DEALLOCATE @vin