Posts mit dem Label SQL Server 2008 werden angezeigt. Alle Posts anzeigen
Posts mit dem Label SQL Server 2008 werden angezeigt. Alle Posts anzeigen

Freitag, 17. Mai 2013

UPDATE table with values from another table using inner join

update t1
set baseid = tv.ID
output deleted.baseid del, inserted.baseid i
from
tmp.Norm t1 inner join dbo.v_Norm tv
on t1.baseid = tv.tmpid

Montag, 25. März 2013

SQL: Compare 2 tables with same structure

Sample SQL statements to compare data in two tables with identical structure.
(Statements work for MS SQL Server, as well as for many other databases.)

To find records which exist in source table but not in target table:
SELECT * FROM t1 WHERE NOT EXISTS (SELECT * FROM t2 WHERE t2.Id = t1.Id)
or
SELECT * FROM t1 LEFT OUTER JOIN T2 on t1.Id = t2.Id WHERE t2.Id IS NULL
If the primary key consists of more than one column, you can modify SQL statement:

SELECT Id, Col1 FROM t1 WHERE NOT EXISTS
(SELECT 1 FROM t2 WHERE t1.Id = t2.Id AND Col1.t1 = Col2.t2)


On SQL Server 2005 or newer you can use the EXCEPT operator:
SELECT Id, Col1 FROM t1 EXCEPT SELECT Id, Col1 FROM t2
To find records which exist in source table but not in target table, as well as records which exists in target table but not in source table:
SELECT * FROM (SELECT Id, Col1 FROM t1, 'old'
UNION ALL
SELECT Id, Col1 FROM t2, 'new') t
ORDER BY Id

Note: For tables with large amounts of data UNION statement might be very slow.

Freitag, 8. März 2013

SQL: Convert a date value to int

select YEAR(getdate())*10000+MONTH(getdate())*100+DAY(getdate())
Result:
DateAsInt
-----------
20130308

Freitag, 27. April 2012

MS SQL Server 2008 R2: change default backup path




To change the default backup path, you need to edit the registry:


1. execute regedit.exe

2. go to the following Registry path:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQLServer]

3. Modify the "BackupDirectory" Registry key: