Você está na página 1de 5

SAP Note

Header Data
Symptom
Wi t h t he r est r uct ur i ng of har dwar e or wi t h a l ack of space, you may need t o change or cr eat e
i ndi vi dual dat abase f i l es or al l dat abase f i l es f or t he SQL Ser ver .
Thi s not e descr i bes t he f i l e management of dat abase f i l es f or t he user dat abases and t he t empdb
dat abase.

Other Terms
sp_det ach_db, sp_at t ach_db, move, al t er dat abase, t empdb, i ncr ease, decr ease, copy

Reason and Prerequisites
Thi s not e descr i bes t he pr ocedur e f or SQL Ser ver 7. 0 and hi gher . The commands execut ed f or an R/ 3
dat abase wi t h t he PRD ar e pr ovi ded as exampl es onl y. Al l commands ar e execut ed wi t h t he Quer y
Anal yzer s or t he SQL Ser ver Devel opment St udi o f r omt he SQL Ser ver I nst al l at i on.
The exact synt ax wi t h t he possi bl e opt i ons f or i ndi vi dual commands can be t aken f r omt he SQL Ser ver
Books Onl i ne.

Solution
A. Movi ng user dat abases:

I f you want t o move your R/ 3 dat abase, make sur e your R/ 3 i s st opped wi t h al l appl i cat i on ser ver s.
a) Change t o t he mast er dat abase and t hen di sconnect t he r equi r ed dat abase f r omt he SQL
Ser ver .

For exampl e:

use mast er
go
execut e sp_det ach_db ' PRD' , ' t r ue'
The database is now no longer visible and no longer available for the SQL
Ser ver .
b) Now use t he NT Expl or er t o move t he dat abase f i l es t o your new st or age l ocat i on.
c) Af t er you move t he f i l es, at t ach t he dat abase t o t he SQL Ser ver usi ng t he sp_at t ach_db
command. You must speci f y t he f ul l pat h f or al l t he dat abase f i l es her e.

For exampl e:

use mast er
execut e sp_at t ach_db ' PRD' ,
' d: \ PRDDATA1\ PRDDATA1. mdf ' ,
' e: \ PRDDATA2\ PRDDATA2. ndf ' ,
' f : \ PRDDATA3\ PRDDATA3. ndf ' ,
' l : \ PRDLOG\ PRDLOG. l df '
Now the database is visible again and available for the SQL Server.
d) Af t er t he change, you shoul d make a f ul l backup of your R/ 3 dat abase and mast er dat abase.
B. Movi ng t he t empdb dat abase

As t he t empdb dat abase i s const ant l y bei ng used by t he SQL Ser ver , i t cannot be moved usi ng t he same
pr ocess descr i bed above ( sp_det ach_db, sp_at t ach_db) . I nst ead, you must use t he ' al t er dat abase
modi f y f i l e' command.
a) Fi r st di spl ay al l t empdb dat abase f i l es usi ng t he f ol l owi ng command:

363018 - File management for SQL Server
Version 13 Validity: 26.08.2008 - active Language English
Released On 26.08.2008 13:53:36
Release Status Released for Customer
Component BC-DB-MSS Microsoft SQL Server
Priority Recommendations / Additional Info
Category Consulting
sel ect conver t ( char ( 20) , name) as name, f i l ename f r omt empdb. . sysf i l es

namefilename
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
tempdevd:\TEMPDB\TEMPDB.MDF
templogd:\TEMPDB\TEMPLOG.LDF
( 2 r ow( s) af f ect ed)
b) Set a new st or age l ocat i on f or t he f i l es by execut i ng an ALTER DATABASE f or each f i l e:

Exampl e:

al t er dat abase t empdb
modify file (name=tempdev,filename='E:\NEW\tempdb.mdf')
al t er dat abase t empdb
modify file (name=templog,filename='E:\NEW\templog.ldf')
c) Rest ar t your SQL Ser ver . The t empdb dat abase i s cr eat ed agai n dur i ng each r est ar t of t he
SQL Ser ver . I n t hi s case, t he f i l es ar e now cr eat ed i n t he new di r ect or y.
d) Use t he NT Expl or er t o del et e t he ol d dat abase f i l es f or t he t empdb.
e) We r ecommend t hat you make a f ul l backup of t he mast er dat abase af t er t he change.
C. Addi ng and r emovi ng dat abase f i l es

By addi ng or r emovi ng dat abase f i l es t o t he SAP dat abase t he space avai l abl e wi t hi n t he dat abase can
be var i ed or t he di st r i but i on of t he dat abase t o t he har d di sks i n quest i on can be changed.
When you have added a f i l e t o you dat abase or r emoved one f r omi t , you have t o per f or ma compl et e
dat abase backup, as t he chai n of t he dat abase and l og backups br eaks when you change t he st r uct ur e
of t he dat abase. Thi s means t hat you cannot r est or e a l og backup f r oma dat abase wi t h, f or exampl e,
5 f i l es ( 3 dat abase and 2 l og f i l es) on a dat abase backup wi t h, f or exampl e, 4 f i l es ( 3 dat abase
f i l es and 1 l og f i l e) .
a) Addi ng dat a f i l es:
You can add a file to the database using the ALTER DATABASE command.

For exampl e:

ALTER DATABASE PRD ADD FI LE
( NAME = ' PRD_DATA4' ,
FI LENAME = ' I : \ PRDDATA4\ PRDDATA4. ndf ' ,
SI ZE = 1024,
MAXSI ZE = UNLI MI TED ,
FI LEGROWTH = 100MB)
b) Addi ng l og f i l es:
You can add additional log files to the database to increase the size of the
t r ansact i on l og. You must use t he ALTER DATABASE ADD LOG FI LE command f or t hi s.

Exampl e:

ALTER DATABASE PRD ADD LOG FI LE
( NAME = PRDLOG2,
FI LENAME = ' l : \ PRDLOG\ PRDLOG2. l df ' ,
SI ZE = 512,
MAXSI ZE = UNLI MI TED ,
FI LEGROWTH = 100MB)
c) Removi ng dat a f i l es:
To remove a data file from the database, you must fist ensure that it does
not cont ai n any dat a. Use t he DBCC SHRI NKFI LE command t o empt y a f i l e. Bef or e you empt y t he f i l e,
make sur e t her e i s enough space l ef t i n t he r emai ni ng f i l es t o i ncl ude t he dat a. The pr i mar y dat a
f i l e ( . mdf ) cannot be r emoved.
WARNING: You can use the command DBCC SHRINKFILE while the SAP system is
r unni ng. However , t he command gener at es an addi t i onal I O l oad f or t he syst em. As a r esul t , t he
syst emper f or mance may det er i or at e si gni f i cant l y. We t her ef or e r ecommend t hat you do not execut e t he
command DBCC SHRI NKFI LE whi l e t he SAP syst emi s r unni ng.

For exampl e:

USE PRD
DBCC SHRI NKFI LE ( ' PRD_DATA4' , EMPTYFI LE)
This command divides the data from this file evenly among the remaining
f i l es. You can t hen use t he ALTER DATABASE command t o r emove t he f i l e f r omt he dat abase.

For exampl e:

ALTER DATABASE PRD REMOVE FI LE PRD_DATA4
If error 5042 or 3140 occurs when you try to remove the file, even though it
i s empt y, t hi s i s a known pr obl emwi t h SQL Ser ver 7. The pr obl emi s descr i bed i n t he Mi cr osof t
Knowl edgebase ar t i cl e Q254253, whi ch you can f i nd at
ht t p: / / suppor t . mi cr osof t . com/ suppor t / kb/ ar t i cl es/ q254/ 2/ 53. asp. To avoi d t hi s pr obl em, i mpor t
Ser vi ce Pack 3 f or SQL Ser ver 7 and set t he 8901 t r acef l ag bef or e t he SHRI NKFI LE command.

For exampl e:

DBCC TRACEON( 8901)
DBCC SHRI NKFI LE ( ' PRD_DATA4' , EMPTYFI LE)
DBCC TRACEOFF( 8901)
Then you can remove the file using the ALTER DATABASE command.
d) Removi ng l og f i l es:
To remove a log file from the database, you must first ensure that it does
not cont ai n any dat a. The ent r i es, whi ch ar e no l onger act i ve, ar e r emoved f r omt he l og by a backup
of t he t r ansact i on l og.
If you are using SQL Server 7, the file to be removed must not contain the
act i ve par t of t he l og. Thi s act i ve par t must be moved t o enabl e you r emove t he f i l e. I f you ar e
usi ng SQL Ser ver 2000, you do not have t o car r y out t hi s manual move.
Use the command

use PRD
sel ect * f r omsysf i l es

fileid.... namefilename
- - - - - - - . . . . - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 .... PRDDATA1d:\PRDDATA1\PRDDATA1.mdf
2.... PRDLOGl:\PRDLOG\PRDLOG.ldf
3.... PRDDATA2e:\PRDDATA2\PRDDATA2.ndf
4.... PRDDATA3f:\PRDDATA3\PRDDATA3.ndf
5.... PRDLOG2 m:\PRDLOG2\PRDLOG2.ldf

( 4 r ow( s) af f ect ed)
and

DBCC LOGI NFO( PRD)

FileIdFileSize... StatusParity CreateTime
- - - - - - - - - - - - - - - - . . . - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2253952... 0 02001- 05- 21 17: 53: 26. 090
2253952... 2 64 2001- 05- 23 14: 21: 12. 857
2270336... 0 1282001- 05- 21 17: 54: 39. 900
5253952... 0 02001- 05- 23 14: 59: 45. 117
5253952... 0 02001- 05- 23 14: 59: 45. 117
5253952... 0 02001- 05- 23 14: 59: 45. 117
5278528... 002001- 05- 23 14: 59: 45. 117

( 7 r ow( s) af f ect ed)
to localize the active part of the log and the associated physical file. In
t he LOGI NFO di spl ay, St at us 2 shows t he act i ve par t of t he l og as wel l as t he r el at ed Fi l eI D, i n our
case, Fi l eI D 2. Thr ough t he out put of f i l e names wi t h t he f i r st command, t he physi cal f i l e can now
be assi gned, i n our exampl e, t he ' l : \ PRDLOG\ PRDLOG. l df ' f i l e.
If the file that you want to remove does not contain any active part of the
l og, you can now r emove t he f i l e usi ng t he ALTER DATABASE command af t er a BACKUP LOG ( see ' Removi ng
dat a f i l es' ) .
If the file contains the active part of the log, this must be moved first.
You can move t he act i ve par t by ar t i f i ci al l y f i l l i ng t he l og t hr ough dummy t r ansact i ons unt i l t he
act i ve par t swi t ches t o anot her f i l e. You can use t he f ol l owi ng scr i pt f or t hi s.
A help table is created first and filled with a data record:

CREATE TABLE MoveVLFPoi nt er
(A INTNOT NULL,
BCHAR(2000)NOT NULL)
GO

I NSERT MoveVLFPoi nt er ( A, B)
VALUES ( 1 , r epl i cat e( ' X' , 2000) )
GO
Now execute the following script again to duplicate the entries in the log:

DECLARE @MAXI NT I NT
SELECT @MAXI NT=MAX( A) FROM MoveVLFPoi nt er
I NSERT MoveVLFPoi nt er ( A, B)
SELECT A+@MAXI NT, B FROM MoveVLFPoi nt er
If the active part has been removed from the required file, a BACKUP LOG must
be car r i ed out t o empt y t he f i l es agai n. You can t hen use t he ALTER DATABASE command t o r emove t he
f i l e f r omt he dat abase. The pr i mar y l og f i l e ( Fi l eI D = 2) cannot be r emoved.
D. I ncr easi ng and decr easi ng dat abase f i l es
a) I ncr easi ng dat abase f i l es
To increase database files, use the ALTER DATABASE command with a new size.

Exampl e ( i ncr easi ng t he f i l e t o 2GB) :


ALTER DATABASE PRD MODI FY FI LE ( NAME = ' PRD_DATA4' , SI ZE = 2048)
You cannot decrease the data file using this command. To do this, use the
pr ocess descr i bed bel ow i nst ead.
b) Decr easi ng dat abase f i l es
To decrease database files, use the DBCC SHRINKFILE command with a target
si ze f or t he f i l e. The pr i mar y dat a f i l e ( . mdf ) cannot be decr eased t o any t ar get si ze because i t
al ways cont ai ns t he syst emt abl es.

Exampl e ( decr easi ng t he f i l e t o 1GB) :

USE PRD
DBCC SHRI NKFI LE ( ' PRD_DATA4' , 1024)
The system tries to decrease the file to the target size. However, the file
can be decr eased onl y t o t he si ze of t he dat a i t cont ai ns. I f t he val ue f or t he t ar get si ze i s
smal l er t han t he dat aset cont ai ned i n t he f i l e, t he f i l e i s decr eased onl y t o t he si ze of t he
dat aset . Dat a i s not moved t o ot her f i l es.
E. I ncr easi ng and decr easi ng l og f i l es
a) I ncr easi ng l og f i l es
To increase log files, use the ALTER DATABASE command with a new size.

Exampl e ( i ncr easi ng t he f i l e t o 2GB) :

ALTER DATABASE PRD MODI FY FI LE ( NAME = ' PRDLOG' , SI ZE = 2048)
You cannot decrease the log file using this command. To do this, use the
pr ocess descr i bed bel ow i nst ead.
b) Decr easi ng l og f i l es
To decrease log files, use the DBCC SHRINKFILE command with a target size for
t he f i l e.

Exampl e ( decr easi ng t he f i l e t o 1GB) :

USE PRD
DBCC SHRI NKFI LE ( ' PRDLOG' , 1024)
In this point, the behavior of SQL Server differs between Versions 7.0 and
2000.
SQL Server 7:
Unlike the data files, the file is not immediately decreased in this case.
Fi r st , t he t ar get si ze al one i s ent er ed i n t he l og f i l e. Wi t h subsequent l og backups, t he pr ogr am
t r i es t o r educe t he l og f i l e t o t he speci f i ed t ar get si ze. Unt i l t he f i nal t ar get si ze i s r eached,
sever al l og saves may be r equi r ed. The number of saves r equi r ed depends on t he scope of t he
si mul t aneous usage of t he l og and t he par t of t he l og t hat i s cur r ent l y i n use. Si nce t he l og f i l e
can onl y be decr eased f r omt he end, t he act i ve par t of t he l og must not be l ocat ed at t he end of t he
l og because t he act i ve par t cannot be r emoved. Thr ough t he l og bei ng used, t he act i ve par t gr adual l y
moves t o t he st ar t of t he f i l e, and t he f i l e can t hus be decr eased.
In rare cases, the file may shrink and grow cyclically in connection with the
r educt i on of l og f i l es. The l og f i l e i ni t i al l y gr ows as nor mal gr ows due t o nor mal use, t aki ng i nt o
account t he Aut ogr ow opt i on. However , i t i s i mmedi at el y r educed agai n, whi ch t hen l eads di r ect l y t o
an i ncr ease once agai n ( due t o i t s bei ng used) . The cause i s a DBCC SHRI NKFI LE command wi t h a t ar get
si ze bel ow t he mi ni mumper mi t t ed si ze of a l og f i l e. Thi s means t hat t he t ar get si ze can never be
r eached and t he DBCC SHRI NKFI LE command can never be execut ed successf ul l y. To el i mi nat e t he
pr obl em, execut e t he DBCC SHRI NKFI LE command wi t h a l ar ger t ar get si ze, so t hat i t can be execut ed
successf ul l y.
SQL Server 2000 and higher:
With higher versions, the files are reduced immediately. The part that is not
occupi ed by t he l og wi t hi n t he f i l e i s t r uncat ed f i r st . Then, dummy r ecor ds ar e i nser t ed i nt o t he
l og, i f r equi r ed, so t hat t he act i ve par t moves t o t he st ar t of t he l og f i l e. At t hi s poi nt , t he
shr i nkf i l e command i s compl et ed wi t h a not e st at i ng t hat t he t r ansact i on l og shoul d be saved so t hat
t he i nact i ve par t can be r emoved. Af t er war ds, t he DBCC shr i nkf i l e command must be execut ed agai n
wi t h t he same t ar get si ze t o decr ease t he l og t o t he desi r ed si ze.


Validity
This document is not restricted to a software component or software component version
References
This document refers to:

SAP Notes
This document is referenced by:
SAP Notes (4)

1455399 Performance issues with DSO activation
534036 SAP LT: System settings for conversions
421644 SQL error 9002: The transaction log is full
151603 Copying an SQL Server database
421644 SQL error 9002: The transaction log is full
151603 Copying an SQL Server database
1455399 Performance issues with DSO activation
534036 SAP LT: System settings for conversions

Você também pode gostar