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 Database..sysobjects where type='U'

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

-------type中  U为用户定义表,P为存储过程,TR为触发器,S为系统表

------Database为你指定的数据库名

 

获取所有字段名:

SELECT * FROM syscolumns WHERE id=Object_Id('Table_Name')

 

 

要是要弄清这些语句的含义 可以试下:

SELECT * FROM SysObjects

SELECT * FROM syscolumns

select Object_Id('Table_Name') 

稍微对比一下 就知道原因了~这也是我踩坑的时候无意中发现的。感觉以后很多问题都能用这个方法!

 

 

数据表会关联许多主外键,我现在DELETE也用不了了,最后用了这个方法:

USE [DBName]ALTER TABLE [dbo].[DataFormName] DISABLE TRIGGER ALL 
ALTER TABLE [dbo].[DataFormName] DISABLE TRIGGER ALL 
DELETE FROM [dbo].[DataFormName]
ALTER TABLE [dbo].[DataFormName] CHECK CONSTRAINT ALL
ALTER TABLE [dbo].[DataFormName] ENABLE TRIGGER ALL
GO

 

posted on 2022-05-10 14:55  张不胖  阅读(73)  评论(0编辑  收藏  举报

导航