use
master
godeclare
@dbname
sysname,
@destpath
varchar
(256
)declare
@dbtable
(name sysname,
physical_name sysname)
begin
tryselect
@dbname='
ff',
--input database name
@destpath='
e:/'
--input destination path
--kill database processes
declare
@spid
varchar(20
)declare
curprocess
cursor
forselect
spid
from
sys.sysprocesses
where
db_name
(dbid)
=@dbname
open
curprocess
fetch
next
from
curprocess
into
@spid
while
@@fetch_status=0
begin
exec('
kill '+
@spid
) fetch
next
from
curprocess
endclose
curprocess
deallocate
curprocess
--query physical name
insert
@db(
name,
physical_name)
select
a.name,
a.physical_name
from
sys.master_files a
inner
join
sys.databases b
ona.database_id
=b.database_id
andb.name
=@dbname
where
a.type
<=1--
set offline
exec('
alter database '+
@dbname+'
set offline')
--move to dest path
declare
@login_name
sysname,
@physical_name
sysname,
@temp_name
varchar
(256
)declare
curmove
cursor
forselect
name,
physical_name
from
@dbopen
curmove
fetch
next
from
curmove
into
@login_name
,@physical_name
while
@@fetch_status=0
begin
set@temp_name
=right
(@physical_name
,charindex('
/',reverse
(@physical_name
)) -1)
exec('
exec xp_cmdshell
''move "'+
@physical_name+'
" "'
+@destpath+'
"'''
)exec('
alter database '+
@dbname+'
modify file ( name = '+
@login_name+'
, filename =
'''+
@destpath
+@temp_name
+''')'
)fetch
next
from
curmove
into
@login_name
,@physical_name
endclose
curmove
deallocate
curmove
--set online
exec('
alter database '+
@dbname+'
set online')
--show result
select
a.name,
a.physical_name
from
sys.master_files a
inner
join
sys.databases b
ona.database_id
=b.database_id
andb.name
=@dbname
endtry
begin
catch
select
error_message()
aserrormessage
endcatch
SQL小工具 移動資料庫物理檔案
use master godeclare dbname sysname,destpath varchar 256 declare db table name sysname,physical name sysname begin try select dbname ff input database...
資料庫 儲存過程
儲存過程,stored procedure,是在大型資料庫系統中,一組為了完成特定功能的sql語句集,經編譯後儲存在資料庫中,使用者通過指定儲存過程的名字並給出引數 如果該儲存過程帶有引數 來執行它。模擬於c中的函式。mysql與sqlserver是不同的。建立儲存過程 conn getconnec...
資料庫 儲存過程
在資料庫中,儲存過程屬於一種物件,是一種高效的安全的訪問資料庫的方法。下邊我們就資料庫中的儲存過程總結它的相關知識點。我們分為概述,實現和管理三個方面來總結。一,儲存過程的概述 1,概念 儲存過程 storedprocedure 是在資料庫伺服器端執行的一組t sql語句的集合,經編譯後存放在資料庫...