Tuesday, April 30, 2013
UPDATE in Batches
WHILE (2 > 1)
BEGIN
BEGIN TRANSACTION
UPDATE TOP(100000)
<<TABLE>>
SET <<COLUMN>> = <<VALUE>>
WHERE <<WHERE_CLAUSE>>
IF @@ROWCOUNT = 0
BEGIN
COMMIT TRANSACTION
BREAK
END
COMMIT TRANSACTION
-- 1 second delay
WAITFOR DELAY '00:00:01'
END -- WHILE
Till Next Time
Tuesday, April 23, 2013
CHECKSUM_AGG
Playing around with CHECKSUM_AGG:
select CHECKSUM_AGG ( QTY ) AS CheckSumAgg_QTY, CHECKSUM_AGG ( Distinct QTY ) AS CheckSumAgg_DistinctQTY, sum(QTY) as Sum_QTY
from (
select 4 as qty
union all
select 2 as qty
union all
select 2 as qty
union all
select 2 as qty) T Till Next Time
select CHECKSUM_AGG ( QTY ) AS CheckSumAgg_QTY, CHECKSUM_AGG ( Distinct QTY ) AS CheckSumAgg_DistinctQTY, sum(QTY) as Sum_QTY
from (
select 4 as qty
union all
select 2 as qty
union all
select 2 as qty
union all
select 2 as qty) T Till Next Time
Wednesday, April 17, 2013
Playing around with default Contraints
selectTill Next Time
schema_name(t.schema_id) as Schema_Name,
t.name as Table_Name,
d.name as Default_Constraint_Name,
c.name as Column_Name,
d.definition as Constraint_Definition
, 'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(T.SCHEMA_ID)) +'.'+ QUOTENAME(T.NAME) + ' DROP CONSTRAINT ' + D.NAME + ';' as Drop_Statement
, 'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(T.SCHEMA_ID)) +'.'+ QUOTENAME(T.NAME) + ' ADD CONSTRAINT DF_'+schema_name(t.schema_id)+'_'+t.name+'_'+ c.name + ' DEFAULT '+d.definition + ' FOR '+QUOTENAME( C.NAME) +';' as Create_Statement
, 'exec sp_rename ''' +schema_name(t.schema_id)+'.'+d.name +''', ''DF_'+schema_name(t.schema_id)+'_'+ t.name+'_'+ c.name +''';' as Rename_Statement
from sys.tables t
join sys.default_constraints d
on d.parent_object_id = t.object_id
join sys.columns c
on c.object_id = t.object_id
and c.column_id = d.parent_column_id
Tuesday, April 9, 2013
TSQL Difference between 2 rows
use AdventureWorks2012;
selectTill Next Time
CustomerID,
OrderDate,
TotalDue,
sum(Totaldue) over(partition by CustomerId order by OrderDate Desc ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING ) as LastOrderAmount,
TotalDue - sum(Totaldue) over(partition by CustomerId order by OrderDate Desc ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING ) as ChangeFromLastOrder from sales.SalesOrderHeader
order by CustomerID, OrderDate Desc
Labels:
AdventureWorksDW,
AGGREGATE,
OVER,
PARTITON BY,
ROWS BETWEEN,
SUM
Subscribe to:
Posts (Atom)