1. 程式人生 > 其它 >SQL server 語句

SQL server 語句

用指令碼還原bak檔案:

SQLServer通過SQL指令碼進行BAK備份檔案資料庫還原 | 放肆雷特 - 鋒哥的部落格 (fangsi.net)

RESTORE DATABASE [DBName_XXX]
FROM DISK = N'C:\DBbak\TestDBbackup.bak' WITH
MOVE N'TestDB' TO N'C:\DB\DBName_XXX.mdf',
MOVE N'TestDB_log' TO N'C:\DB\DBName_XXX_log.ldf',
FILE = 1, NOUNLOAD, STATS = 10

USE [master]
GO
CREATE LOGIN user_XXX WITH
PASSWORD = N'pwd_XXX' , DEFAULT_DATABASE = DBName_XXX, CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF GO PRINT 'CREATE LOGIN OK.' USE DBName_XXX GO CREATE USER user_XXX FOR LOGIN user_XXX WITH DEFAULT_SCHEMA = [dbo] GO EXEC sp_addrolemember N'db_owner', N'user_XXX' GO PRINT 'CREATE USER OK.'

將“C:\DBbak\TestDBbackup.bak”替換為你資料庫備份的檔案路徑

DBName_XXX:資料庫名

user_XXX:資料庫登入使用者名稱

pwd_XXX:資料庫登入使用者密碼

查詢該例項下所有資料庫的名:

select name from sysdatabases where dbid>=5 order by dbid desc

查詢指定資料庫內所有表名:

select name from KPIBoard..sysobjects where type='U'

SELECT Name FROM SysObjects Where XType='U' ORDER BY Name

注:-------type中  u為使用者定義表,P為儲存過程,TR為觸發器,S為系統表