novembre 2007 - Posts

[SQL2008] Operatore MERGE: il codice del webcast
12 novembre 07 12.51 | abenedetti | with no comments

Questa mattina è stato erogato il primo webcast relativo a SQL Server 2008.

Agenda: operatore MERGE.

Di seguito il codice utilizzato per la piccola demo:

USE master
GO

/* Creazione db per tests */
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'demoMerge')
DROP DATABASE demoMerge
GO

CREATE DATABASE demoMerge
GO

USE demoMerge

GO

/* Creazione tabelle sorgente / destinazione per test */
IF OBJECT_ID('dbo.books', 'U') IS NOT NULL
  DROP TABLE dbo.books
GO

IF OBJECT_ID('dbo.booksInfo', 'U') IS NOT NULL
  DROP TABLE dbo.booksInfo
GO

create table books
(
idRecord int primary key identity(1,1),
description varchar(70),
code char(5) unique,
qty smallint
)
GO

create table booksInfo
(
idRecord int primary key identity(1,1),
description varchar(70),
code char(5) unique,
qty smallint
)
GO

/*
Dati per test
[books]: tabella destinazione (tabella di produzione)
[booksInfo]: tabella sorgente (tabella di alimentazione)
*/
insert books values
('Inside Microsoft SQL Server 6.5 :-)','sq650',1),
('Inside Microsoft SQL Server 2005: T-SQL Querying','sq051',21),
('Inside Microsoft SQL Server 2005: T-SQL Programming','sq052',5),
('Beginning SQL Server 2005 Programming','sq05p',12)
GO

insert booksInfo values
('Inside Microsoft SQL Server 2005: T-SQL Querying','sq051',0),
('Inside Microsoft SQL Server 2005: T-SQL Programming','sq052',1),
('Beginning SQL Server 2005 Programming','sq05p',1),
('SQL Server 2008 for developers','sq081',10),
('SQL Server 2008 for DBA','sq082', 10)
GO

/* Operatore MERGE */
MERGE
    dbo.books AS [existing] -- destinazione
USING
    dbo.booksInfo AS [new] -- sorgente
ON
    ([existing].code = [new].code)
WHEN
    MATCHED AND ([existing].qty <> [new].qty)
    THEN
        UPDATE SET [existing].qty = [new].qty
WHEN
    NOT MATCHED -- righe non presenti in destinazione
    THEN
           INSERT (description, code, qty)
            VALUES ([new].description, [new].code, [new].qty)
WHEN
    SOURCE NOT MATCHED -- righe che non esistono in sorgente
    THEN
        DELETE;
GO

/* Vedo i risultati */
select * from books
GO

/* Operazioni di UPSERT in Stored Procedure */
CREATE PROCEDURE up_upsertMerge
(
    @code char(5),
    @description VARCHAR(70), @qty smallint
)
AS
set nocount on

MERGE books t
    USING (SELECT @code AS code, @description AS description,
            @qty AS qty) s
    ON t.code = s.code
    WHEN MATCHED
    AND (t.description <> s.description OR t.qty<> s.qty) THEN
        UPDATE SET description = s.description, qty = s.qty
    WHEN NOT MATCHED THEN
        INSERT VALUES (code, description, qty);

set nocount off
go

exec dbo.up_upsertMerge @code ='sq081',
        @description = 'SQL Server 2008 for developers 2nd edt',
        @qty = 500
go

/* Vedo i risultati */
select * from books
GO

A breve sul sito UGISS il download di slide e demo (solution) completa!

Filed under:
[SQL 2008] Webcast: Merge statement
11 novembre 07 11.48 | abenedetti | with no comments

Domani, lunedì 12 novembre, avranno inizio, dopo la presentazione fatta da Davide, i webcast relativi alla prossima versione di SQL Server.

Abstract: "Sessione dettaglia dedicata al nuovo operatore "MERGE" ideale in tutte quelle situazioni in cui è necessario sincronizzare il contenuto di due tabelle, solitamente tipica necessità di un DataWareHouse."

Qui il link relativo per registrarsi!

Filed under:
DBCC CHECKDB è tuo amico!
05 novembre 07 11.41 | abenedetti | 4 comment(s)

La verifica di consistenza del database è un'operazione fondamentale per una corretta analisi dei nostri archivi.

Oggi mi è capitata la necessità di mostrare come poter memorizzare le informazioni risultanti da questa analisi.

La necessità era quindi quella di:

  1. eseguire il controllo di consistenza
  2. memorizzarne il risultato all'interno di una tabella così da poterne tenere traccia nel tempo
  3. memorizzare anche altre informazioni (ad esempio: la data di esecuzione)

Ho risolto così:

  1. creo la tabella dove memorizzare i dati del DBCC con
    1. struttura identica ai metadati uscenti dal comando
    2. aggiunto colonna dataEsecuzione con valore di default a getdate()
  2. creo una vista costituita *solo* dai metadati uscenti dal DBCC
  3. eseguo il comando facendo un insert nella vista (e non nella tabella)

Di seguito lo script:

/* CONTROLLO CONSISTENZA DATABASE */
/* 1. creazione tabella per storage risultati DBCC */
CREATE table myDBCC
(
idRecord INT PRIMARY KEY IDENTITY(1,1),
dataEsecuzione DATETIME DEFAULT(GETDATE()),
[error] int , [level] int ,
[state] int , [messagetext] varchar(1000) ,
[repairlevel] int , [statut] int ,
[dbid] int , [objectid] int ,
[indexid] int , [partitionid] int, [allocunitid] int,
[file] int , [page] int , [slot] int ,
[reffile] int , [refpage] int ,
[refslot] int , [allocation] int
)
GO

/* 2. creazione vista per insert in tabella di storage */
CREATE view myDBCCTemp
AS
SELECT [error] , [level] ,
[state] , [messagetext] ,
[repairlevel] , [statut] ,
[dbid] , [objectid] ,
[indexid] , [partitionid] , [allocunitid] ,
[file] , [page] , [slot] ,
[reffile] , [refpage] ,
[refslot] , [allocation]
FROM myDBCC
GO

/* 3. esecuzione DBCC con insert in vista */
INSERT myDBCCTemp
EXEC ('DBCC CHECKDB(''UGISS'') WITH TABLERESULTS')

/* 4. vedo la tabella di storage */
SELECT * FROM myDBCC

/* 5. drop oggetti */
DROP VIEW myDBCCTemp
DROP TABLE myDBCC
GO

Filed under:
MSDN Italia ha un suo blog
05 novembre 07 11.34 | abenedetti | with no comments

Finalmente!

Anche la nostra squadra di MSDN Italia ha il suo blog. Qui.

Filed under:

This Blog

Syndication