今天在創建數據庫的時候突然發現,xp_cmdshell的存儲過程不能用了,網上一搜,發現大部分都是只關閉安全配置,然后就有了下文

代碼:具體的看注釋,值得一提的是==》reconfigure with override,上面一句語句如果不加這句,則只是臨時可用,不會影響系統原有配置(可以理解為==》不加就是new和加了就是override)

代碼貼上:
--創建目錄(如果指定的路徑不存在就會報錯) exec sp_configure 'show advanced options',1 --顯示高級選項 reconfigure with override--重新配置 exec sp_configure 'xp_cmdshell',1 --1代表允許,0代表阻止 reconfigure with override exec xp_cmdshell 'mkdir F:\Work\SQL mkdir E:\SQL' exec sp_configure 'xp_cmdshell',0 reconfigure with override exec sp_configure 'show advanced options',0 reconfigure with override

SQL也貼上吧,比較這玩意總得有個語境吧:
--如果數據庫存在就刪除 use master if exists(select * from sysdatabases where Name=N'LawyerBlog') begin drop database LawyerBlog end --創建目錄(如果指定的路徑不存在就會報錯) exec sp_configure 'show advanced options',1 --顯示高級選項 reconfigure with override--重新配置 exec sp_configure 'xp_cmdshell',1 --1代表允許,0代表阻止 reconfigure with override exec xp_cmdshell 'mkdir F:\Work\SQL mkdir E:\SQL' exec sp_configure 'xp_cmdshell',0 reconfigure with override exec sp_configure 'show advanced options',0 reconfigure with override --創建數據庫 create database LawyerBlog on primary --數據庫文件,主文件組 ( name='LawyerBlog_Data', --邏輯名 size=10mb, --初始大小 filegrowth=10%, --文件增長 maxsize=1024mb, --最大值 filename=N'F:\Work\SQL\LawyerBlog_Data.mdf'--存放路徑(包含文件后綴名) ), filegroup ArticleData --Article文件組(表創建到不同的文件組里面可以分擔壓力) ( name='LawyerBlog_Data_Article', size=10mb, filegrowth=10%, maxsize=1024mb, filename=N'E:\SQL\LawyerBlog_Data_Article.ndf' ) log on --日記 ( name='LawyerBlog_Log1', size=5mb, filegrowth=5%, filename=N'F:\Work\SQL\LawyerBlog_log1.ldf' ), ( name='LawyerBlog_Log2', size=5mb, filegrowth=5%, filename=N'E:\SQL\LawyerBlog_log2.ldf' ) go
擴展:
如果是普通用戶要有ALTER SETTINGS權限才能運行sp_configure(一般管理員才有這個權限)
向數據庫添加數據文件或日志文件
-
連接到數據庫引擎。
-
在標准菜單欄上,單擊“新建查詢”。
-
將以下示例復制並粘貼到查詢窗口中,然后單擊“執行”。此實例向數據庫添加由兩個文件組成的文件組。此示例在 AdventureWorks2012 數據庫中創建文件組 Test1FG1,然后將兩個 5MB 的文件添加到該文件組。
View CodeUSE master GO ALTER DATABASE AdventureWorks2012 ADD FILEGROUP Test1FG1; GO ALTER DATABASE AdventureWorks2012 ADD FILE ( NAME = test1dat3, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\t1dat3.ndf', SIZE = 5MB, MAXSIZE = 100MB, FILEGROWTH = 5MB ), ( NAME = test1dat4, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\t1dat4.ndf', SIZE = 5MB, MAXSIZE = 100MB, FILEGROWTH = 5MB ) TO FILEGROUP Test1FG1; GO
