How to shrink MS SQL database log file

USE [$(databasename)]
GO

-- Drop old transactions log data (just mark for deletion)
BACKUP LOG [$(databasename)] WITH NO_LOG -- TO DISK='D:BackupSQLServer$(databasename)_log.trn'
GO

-- Physically shrinks the log file
DBCC SHRINKFILE($(databasename)_log, NOTRUNCATE)
Advertisements

MS SQL – Table Variables & Temporary Tables (Transact-SQL)

Temporary Tables

  • visible only in current session of SQL Server;
  • automatically droped on session close;
  • can’t have foreign key constraints;
  • are created in temporary database of SQL Server tempdb;
  • should manually drop to keep cached only in memory.
CREATE TABLE #temp (id int);

INSERT INTO #temp
   SELECT id FROM Table1 WHERE ;

-- Use #temp data here.

DROP TABLE #temp;

Table Variables

  • if you are using SQL Server 2000 or higher;
  • always stay in memory;
  • don’t need to be dropped;
  • more flexibility.
DECLARE @temp table (id int);

INSERT INTO @temp
   SELECT id FROM Table1 WHERE ;

-- Use #temp data here.

Conclusion

If you have SQL Server 2000 or higher always should use table variables.