Thursday, March 22, 2012

example of finding percent restored

select percent_complete
from sys.dm_exec_requests
where session_id = 58

Monday, March 19, 2012

Continue

DECLARE @int varchar(10)  
DECLARE curs_int  CURSOR FOR
SELECT '1'
union SELECT'2'
union SELECT'3'
union SELECT'4'
union SELECT'5'
order by 1
OPEN curs_int

FETCH NEXT FROM curs_int
INTO @int
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @int
FETCH NEXT FROM curs_int
      INTO @int
    if @int = '3' continue
print @int      
END
CLOSE curs_int
DEALLOCATE curs_int

returns
1
2
2
3
4
4
5
5
5

a unsuccessful insert will not change @@identity

create table #dsfdsf(a varchar(1000),b int identity)

insert into #dsfdsf(a)
select 'gfdfg' df
select  @@identity

insert into #dsfdsf(a)
select 'gfdfg' df
where 1=5  

select  @@identity


drop table #dsfdsf

the select returns 1 in both cases

Default inserts

when the only column has a default

create table x (i int default 1)
insert into x values (default)
 

isolation level cheatsheet

DBCC USEROPTIONS will show default settings

Set Option        Value
isolation level    read committed

to avoid locks where the data is does not have to be perfect
SET TRANSACTION ISOLATION LEVEL
    { READ UNCOMMITTED
    | READ COMMITTED
    | REPEATABLE READ
    | SNAPSHOT
    | SERIALIZABLE
    }
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED


Select etc …

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

Or 

Select * from trans with (nolock)

Nulls in avg and count

select     
    sum(a) / count(a)  [homemade average - count doesnt count nulls],
      avg(isnull(a,0))  [average with isnull],
      avg(a)  [regular average]
from
(
select null a
union select 0
union select 5
union select 15
) main

Turning of the firewall in XP

Here's how