Voici quelques scripts fréquemment utilisés pour la gestion des bases de données SQL Server.
BACKUP DATABASE [NomDeLaBase]
TO DISK = 'C:\Backups\NomDeLaBase.bak'
WITH FORMAT;
RESTORE DATABASE [NomDeLaBase]
FROM DISK = 'C:\Backups\NomDeLaBase.bak'
WITH FILE = 1, NORECOVERY;
SELECT dbschemas.name AS 'Schema',
dbtables.name AS 'Table',
indexes.name AS 'Index',
indexstats.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.tables dbtables ON dbtables.object_id = indexstats.object_id
INNER JOIN sys.schemas dbschemas ON dbtables.schema_id = dbschemas.schema_id
INNER JOIN sys.indexes indexes ON dbtables.object_id = indexes.object_id
AND indexstats.index_id = indexes.index_id
WHERE indexstats.avg_fragmentation_in_percent > 10;
CREATE PROCEDURE AjouterEmploye
@Nom NVARCHAR(50),
@Prenom NVARCHAR(50),
@Salaire DECIMAL(10, 2)
AS
BEGIN
INSERT INTO Employes (Nom, Prenom, Salaire)
VALUES (@Nom, @Prenom, @Salaire);
END;
SELECT e.Nom, e.Prenom, d.Departement
FROM Employes e
INNER JOIN Departements d ON e.DepartementID = d.DepartementID;