Tuesday, July 15, 2008

SQL Restore Database with Move

/*
This is my standard restore to a different location. This is basically the script from the help file so I don't have to find it any more.

The script assumes that the data base has been created and will be overwritten.

The first script gets the file lay out and names which needs to be inserted into the second call. It demonstrates more than one logical location in a mdf file. It can use UNC file navigation.
*/

Restore FileListonly from disk = '[File location][file name].BAK';

/*
Run the first query to get the logical name prior running the second

The file location must be relative the machine that the restore is being done so it must be a local drive as demonstrated in the sample. I have not found a way to restore from a remotely located drive.
*/

Restore Database [database name] FROM DISK = '[File location][file name].BAK'
WITH MOVE '[LogicalName]' TO 'C:\restored_databases\[filename].mdf'
, MOVE '[LogicalName_1]' To 'C:\restored_databases\[filename_1].mdf'
, MOVE '[LogicalName_log]' To 'C:\restored_databases\[filename_log].ldf'
, stats = 5
, REPLACE



----------------
Now playing: NPR - 0807195: NPR: 07-13-2008 Wait Wait... Don't Tell Me!
via FoxyTunes

Tuesday, July 1, 2008

SQL get the column definitions from a table

--This script is used to get the column definitions from the same table in 2 different databases
--it is primarily to demonstrate how to get column definitions from a table

DECLARE
@table_name nvarchar(128)

SET @table_name = '[table_name]'

SELECT *
FROM [db_name].sys.all_objects
INNER JOIN [db_name].sys.all_columns
on all_objects.object_id = all_columns.object_id
INNER JOIN [db_name].sys.systypes
ON all_columns.system_type_id = systypes.xtype
WHERE all_objects.type = 'U'
AND all_objects.name in ( @table_name)


SELECT *
FROM [db_name_2].sys.all_objects
INNER JOIN [db_name_2].sys.all_columns
on all_objects.object_id = all_columns.object_id
INNER JOIN [db_name_2].sys.systypes
ON all_columns.system_type_id = systypes.xtype
WHERE all_objects.type = 'U'
AND all_objects.name in ( @table_name)

SQL Script to get definitions for procedures, functions, and views

-- This procedure will allow you to get the definition for the raw SQL
-- that is used to create the stored procedures, functions, and views

USE
[db_name];

SELECT TOP 200
all_objects.object_id
, name
, definition
, type_desc
, type --P=Stored Procedure, V= View, FN = Function
FROM sys.all_objects
INNER JOIN sys.sql_modules
on all_objects.object_id = sql_modules.object_id
--Use the to find specific definitions
WHERE
definition like '%nolock%'