Showing posts with label deleted. Show all posts
Showing posts with label deleted. Show all posts

Thursday, March 29, 2012

getting a deleted ids froma field

hi i need to get deleted dis froma table say customers id where in we have 1000 records and we have randomly deleted 100 records i want the ids of those randomly deleted recodsTry http://www.lumigent.com Log explorer
or if it was numeric column, you can guess IDs by this query
select
FreeRange=
case when (h.ID+1)=(min(l.Id)-1)
then convert(varchar(20),h.ID+1)
else isnull(convert(varchar(20),h.ID+1),'?')+'..'+isnul l(convert(varchar(20),min(l.Id)-1),'?')
end
,"Count"=isnull(convert(varchar(20),min(l.Id)-1-(h.ID+1)+1),'?')
from
(
select yt.ID
from YourTable yt
where not exists(select 'x' from YourTable ytH where yt.ID=ytH.ID-1)
) h
full join
(
select yt.ID
from YourTable yt
where not exists(select 'x' from YourTable ytL where yt.ID=ytL.ID+1)
) l on h.ID<l.Id
group by h.ID
order by h.ID|||see www.nigelrivett.com
Find gaps in sequence numbers|||see www.nigelrivett.com
Find gaps in sequence numbers|||nigelrivett's query

select convert(varchar(10),imin.ID) + ' - ' + convert(varchar(10)
,
(
select min(ID)
from
(
select ID
from YourTable
where not exists
(
select *
from YourTable a2
where YourTable.ID-1 = a2.ID
)
and YourTable.ID <> (select min(ID) from YourTable)) as imax where imax.ID > imin.ID)
)
from
(
select ID
from YourTable
where not exists
(
select *
from YourTable a2
where YourTable.ID+1 = a2.ID
)
and YourTable.ID <> (select max(ID) from YourTable)
) as imin

But it was a little slow (7s/4000 rows), I added index
create unique clustered index icx_YourTable on YourTable(ID ASC)
but it is still about 3s/4000 rows (By the way Index tuning wizard did not recomend any index !)
My query is about 1s/4000 rows with no index and I expect higher difference for larger dataset.

You said, you have 1000 rows, so time under 10 s is excelent for you.
I am speaking to terabyters :)

Sunday, February 19, 2012

Get Number of rows Deleted

How do we get the number of rows deleted from a table?
@.@.rowcount is returning 0@.@.rowcount (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_globals_20vo.asp) returns the number of rows affected by the last successful statement executed by your spid. It only has meaning for the duration of one statment, then it is reset.

See the code for the gory details of how you can go worng ;)DECLARE @.i1 INT
, @.i2 INT

CREATE TABLE #foo (
fooID INT NOT NULL
)

INSERT INTO #foo (fooID)
SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION
SELECT 3 UNION SELECT 4 UNION SELECT 5

SELECT * FROM #foo

DELETE FROM #foo WHERE 1 = fooID % 2

SELECT @.i1 = @.@.rowcount

DELETE FROM #foo

PRINT 'We''re working on it!'

SELECT @.i2 = @.@.rowcount

SELECT @.i1, @.i2

SELECT * FROM #foo

DROP TABLE #foo-PatP|||A true gentleman

Isn't that a fubar table?