21 September 2012

Access - evitare l'errore di modifica contemporanea di record

Una form di Access legata ad una tabella SQL via ODBC visualizza questo errore durante il salvataggio:

"Modifica contemporanea di record - Durante la corrente sessione di modifica il record è stato modificato da un altro utente. Salvando le proprie modifiche si sovrascriveranno i cambiamenti dell'altro utente"

Con questo trucco si dovrebbe* evitare l'errore:
ALTER TABLE Table1
ADD Timestamp

La sintassi per la ALTER TABLE (Transact-SQL) dice:

column_name
For new columns, column_name can be omitted for columns created with a timestamp data type. The name timestamp is used if no column_name is specified for a timestamp data type column.

Sostanzialmente aggiunge una colonna di tipo timestamp che si chiama [timestamp] che si aggiorna in automatico.

____

* si dovrebbe = l'ho usato una volta e ha funzionato. =)

06 September 2012

Three Methods to Insert Multiple Rows into Single Table

-- Insert Multiple Values into SQL Server
CREATE TABLE #SQLAuthority (ID INT, Value VARCHAR(100));
Method 1: Traditional Method of INSERT... VALUE
-- Method 1 - Traditional Insert
INSERT INTO #SQLAuthority (ID, Value)
VALUES (1, 'First');
INSERT INTO #SQLAuthority (ID, Value)
VALUES (2, 'Second');
INSERT INTO #SQLAuthority (ID, Value)
VALUES (3, 'Third');

-- Clean up
TRUNCATE TABLE #SQLAuthority;

Method 2: INSERT... SELECT
-- Method 2 - Select Union Insert
INSERT INTO #SQLAuthority (ID, Value)
SELECT 1, 'First'
UNION ALL
SELECT 2, 'Second'
UNION ALL
SELECT 3, 'Third';

-- Clean up
TRUNCATE TABLE #SQLAuthority;
Method 3: SQL Server 2008+ Row Construction
-- Method 3 - SQL Server 2008+ Row Construction
INSERT INTO #SQLAuthority (ID, Value)
VALUES (1, 'First'), (2, 'Second'), (3, 'Third');

-- Clean up
DROP TABLE #SQLAuthority;

Trovato qui.