Wednesday, March 28, 2012
Script which will shrink all databases
May somebody send script which will shrink all databases on server
(SQL 2005). I think about DBCC SHRINKFILE and DBCC SHRINKDATABASE.
I need do this at many servers, so I can't do this manually.
Thanks
RegardsOn Aug 9, 12:41 pm, anxc...@.gmail.com wrote:
> Hi,
> May somebody send script which will shrink all databases on server
> (SQL 2005). I think about DBCC SHRINKFILE and DBCC SHRINKDATABASE.
> I need do this at many servers, so I can't do this manually.
> Thanks
> --
> Regards
There are multiple ways of doing this.
Create a .CMD file with this statement:
----
----
sqlcmd -s <servername1> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername2> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername3> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername4> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername5> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
----
----
Replace the <servername> with your servername
For each server add a new line and with the changed servername
(to create a CMD file: open notepad and paste the final script and
save it as scriptfile.cmd)
Once you have the CMD file, open commandprompt and go to the drive
location and enter the name of the file
In my case it is:
----
----
c:\dbccscript.cmd
----
----
This will display all the output to the commandprompt. You can
redirect all the output to a log/txt file using -o switch in the cmd
script.
Note that sqlcmd is a new utility in MS SQL server 2005.
in SQL Server 2000 you can achieve the same using "OSQL" books online
has enuf info on this to help you rewrite it for osql.
Regards,
schal|||Thanks shal, unfortunately I don't have "shell - cmd" access to
server, I have to shrink all databases from SQL script only.|||What schal posted will shrink all databases for the server you are connected
to:
EXEC sp_MSForEachDB @.Command1 = N'DBCC SHRINKDATABASE (?, 10)', @.replacechar
= '?'"
But I strongly suggest you read http://www.karaszi.com/SQLServer/in...ink.
asp first.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<anxcomp@.gmail.com> wrote in message news:1186682763.588386.295740@.19g2000hsx.googlegroups.c
om...
> Thanks shal, unfortunately I don't have "shell - cmd" access to
> server, I have to shrink all databases from SQL script only.
>|||It really works, I forgot that is normal storage procedure which I can
run from Query, sorry.
But what if I'd like first use SHRINKFILE all log files (all .LDF
files at server), log files because it takes me much more space than
databases? Logical name of log file is usually DatabaseName_Log.
How shrink log file for all databases, is it similar procedure?|||Try this!
It′s recovery system space quickly:
use master
go
declare curdb cursor for select name from sysdatabases where name not in
('tempdb','model','master')
declare @.nmdb varchar(50)
declare @.cmd varchar(1000)
open curdb
fetch next from curdb into @.nmdb
while @.@.fetch_status = 0
begin
print '======='
print @.nmdb
print '======='
select @.cmd = 'Use ' + @.nmdb
select @.cmd = @.cmd + ' if exists(select 1 from sysobjects where name =
''limpa'')
begin
DROP TABLE LIMPA
end
create table limpa (char1 char(4000))
Set NoCount On
dbcc shrinkFILE (2, notruncate)
dbcc shrinkFILE (2, truncateonly)
dbcc shrinkFILE (1, notruncate)
dbcc shrinkFILE (1, truncateonly)
declare @.i int
select @.i = 0
While (@.i < 100)
begin
insert into limpa values (''a'')
select @.i =@.i + 1
end
backup log ' + @.nmdb + ' with truncate_only
drop table limpa'
Execute (@.cmd)
fetch next from curdb into @.nmdb
end
deallocate curdb
set nocount off
anxcomp@.gmail.com wrote:
>Hi,
>May somebody send script which will shrink all databases on server
>(SQL 2005). I think about DBCC SHRINKFILE and DBCC SHRINKDATABASE.
>I need do this at many servers, so I can't do this manually.
>Thanks
>--
>Regards
Message posted via droptable.com
http://www.droptable.com/Uwe/Forum...server/200708/1|||Thanks, if thought it is simples way to do this. Could you me tell me
how at loop take:
1. name for all databases (without system dbs)
2. logical name of databases
2. logical name of transaction logs.
I need this information for this
BACKUP LOG @.DB_NAME
DBCC SHRINKFILE (@.DB_LOG_NAME)
I'd like this two lines will execute for all database.
Thanks
Regards|||I wrote my own script and I'd like share it with you:
CREATE TABLE #TDatabases(
DBName nvarchar(128),
DBLogicalName nvarchar(128)
)
INSERT INTO #TDatabases
SELECT db.name DBName, mf.name DBLogicalName
FROM sys.databases db join sys.master_files mf
on db.database_id = mf.database_id
WHERE db.name not in ('master', 'tempdb', 'model', 'msdb',
'distribution') AND type_desc LIKE 'log'
SET NOCOUNT ON
DECLARE @.VarDBLogicalName nvarchar(128)
DECLARE @.VarDBName nvarchar(128)
DECLARE @.VarRowCount int
SELECT top 1 @.VarDBName = DBName, @.VarDBLogicalName = DBLogicalName
FROM #TDatabases
SET @.VarRowCount = @.@.rowcount
WHILE @.VarRowCount <> 0
BEGIN
EXEC(' use ' + @.VarDBName + ' backup log '+ @.VarDBName + ' with no_log
dbcc shrinkfile(''' + @.VarDBLogicalName + ''', TRUNCATEONLY) WITH
NO_INFOMSGS')
DELETE
FROM #TDatabases
WHERE DBName = @.VarDBName
SELECT top 1 @.VarDBName = DBName, @.VarDBLogicalName =
DBLogicalName
FROM #TDatabases
SET @.VarRowCount = @.@.ROWCOUNT
END
DROP TABLE #TDatabases
SET NOCOUNT OFF
This works but use something what Microsoft NOT recommend "backup log
with no_log" you can modify it how you like
Regards
Script which will shrink all databases
May somebody send script which will shrink all databases on server
(SQL 2005). I think about DBCC SHRINKFILE and DBCC SHRINKDATABASE.
I need do this at many servers, so I can't do this manually.
Thanks
Regards
On Aug 9, 12:41 pm, anxc...@.gmail.com wrote:
> Hi,
> May somebody send script which will shrink all databases on server
> (SQL 2005). I think about DBCC SHRINKFILE and DBCC SHRINKDATABASE.
> I need do this at many servers, so I can't do this manually.
> Thanks
> --
> Regards
There are multiple ways of doing this.
Create a .CMD file with this statement:
------
sqlcmd -s <servername1> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername2> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername3> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername4> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername5> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
------
Replace the <servername> with your servername
For each server add a new line and with the changed servername
(to create a CMD file: open notepad and paste the final script and
save it as scriptfile.cmd)
Once you have the CMD file, open commandprompt and go to the drive
location and enter the name of the file
In my case it is:
------
c:\dbccscript.cmd
------
This will display all the output to the commandprompt. You can
redirect all the output to a log/txt file using -o switch in the cmd
script.
Note that sqlcmd is a new utility in MS SQL server 2005.
in SQL Server 2000 you can achieve the same using "OSQL" books online
has enuf info on this to help you rewrite it for osql.
Regards,
schal
|||Thanks shal, unfortunately I don't have "shell - cmd" access to
server, I have to shrink all databases from SQL script only.
|||What schal posted will shrink all databases for the server you are connected to:
EXEC sp_MSForEachDB @.Command1 = N'DBCC SHRINKDATABASE (?, 10)', @.replacechar = '?'"
But I strongly suggest you read http://www.karaszi.com/SQLServer/info_dont_shrink.asp first.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<anxcomp@.gmail.com> wrote in message news:1186682763.588386.295740@.19g2000hsx.googlegro ups.com...
> Thanks shal, unfortunately I don't have "shell - cmd" access to
> server, I have to shrink all databases from SQL script only.
>
|||It really works, I forgot that is normal storage procedure which I can
run from Query, sorry.
But what if I'd like first use SHRINKFILE all log files (all .LDF
files at server), log files because it takes me much more space than
databases? Logical name of log file is usually DatabaseName_Log.
How shrink log file for all databases, is it similar procedure?
|||Try this!
It′s recovery system space quickly:
use master
go
declare curdb cursor for select name from sysdatabases where name not in
('tempdb','model','master')
declare @.nmdb varchar(50)
declare @.cmd varchar(1000)
open curdb
fetch next from curdb into @.nmdb
while @.@.fetch_status = 0
begin
print '======='
print @.nmdb
print '======='
select @.cmd = 'Use ' + @.nmdb
select @.cmd = @.cmd + ' if exists(select 1 from sysobjects where name =
''limpa'')
begin
DROP TABLE LIMPA
end
create table limpa (char1 char(4000))
Set NoCount On
dbcc shrinkFILE (2, notruncate)
dbcc shrinkFILE (2, truncateonly)
dbcc shrinkFILE (1, notruncate)
dbcc shrinkFILE (1, truncateonly)
declare @.i int
select @.i = 0
While (@.i < 100)
begin
insert into limpa values (''a'')
select @.i =@.i + 1
end
backup log ' + @.nmdb + ' with truncate_only
drop table limpa'
Execute (@.cmd)
fetch next from curdb into @.nmdb
end
deallocate curdb
set nocount off
anxcomp@.gmail.com wrote:
>Hi,
>May somebody send script which will shrink all databases on server
>(SQL 2005). I think about DBCC SHRINKFILE and DBCC SHRINKDATABASE.
>I need do this at many servers, so I can't do this manually.
>Thanks
>--
>Regards
Message posted via droptable.com
http://www.droptable.com/Uwe/Forums.aspx/sql-server/200708/1
|||Thanks, if thought it is simples way to do this. Could you me tell me
how at loop take:
1. name for all databases (without system dbs)
2. logical name of databases
2. logical name of transaction logs.
I need this information for this
BACKUP LOG @.DB_NAME
DBCC SHRINKFILE (@.DB_LOG_NAME)
I'd like this two lines will execute for all database.
Thanks
Regards
|||I wrote my own script and I'd like share it with you:
CREATE TABLE #TDatabases(
DBName nvarchar(128),
DBLogicalName nvarchar(128)
)
INSERT INTO #TDatabases
SELECT db.name DBName, mf.name DBLogicalName
FROM sys.databases db join sys.master_files mf
on db.database_id = mf.database_id
WHERE db.name not in ('master', 'tempdb', 'model', 'msdb',
'distribution') AND type_desc LIKE 'log'
SET NOCOUNT ON
DECLARE @.VarDBLogicalName nvarchar(128)
DECLARE @.VarDBName nvarchar(128)
DECLARE @.VarRowCount int
SELECT top 1 @.VarDBName = DBName, @.VarDBLogicalName = DBLogicalName
FROM #TDatabases
SET @.VarRowCount = @.@.rowcount
WHILE @.VarRowCount <> 0
BEGIN
EXEC(' use ' + @.VarDBName + ' backup log '+ @.VarDBName + ' with no_log
dbcc shrinkfile(''' + @.VarDBLogicalName + ''', TRUNCATEONLY) WITH
NO_INFOMSGS')
DELETE
FROM #TDatabases
WHERE DBName = @.VarDBName
SELECT top 1 @.VarDBName = DBName, @.VarDBLogicalName =
DBLogicalName
FROM #TDatabases
SET @.VarRowCount = @.@.ROWCOUNT
END
DROP TABLE #TDatabases
SET NOCOUNT OFF
This works but use something what Microsoft NOT recommend "backup log
with no_log" you can modify it how you like
Regards
Script which will shrink all databases
May somebody send script which will shrink all databases on server
(SQL 2005). I think about DBCC SHRINKFILE and DBCC SHRINKDATABASE.
I need do this at many servers, so I can't do this manually.
Thanks
--
RegardsOn Aug 9, 12:41 pm, anxc...@.gmail.com wrote:
> Hi,
> May somebody send script which will shrink all databases on server
> (SQL 2005). I think about DBCC SHRINKFILE and DBCC SHRINKDATABASE.
> I need do this at many servers, so I can't do this manually.
> Thanks
> --
> Regards
There are multiple ways of doing this.
Create a .CMD file with this statement:
------
sqlcmd -s <servername1> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername2> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername3> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername4> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
sqlcmd -s <servername5> -Q "EXEC sp_MSForEachDB @.Command1 = N'DBCC
SHRINKDATABASE (?, 10)', @.replacechar = '?'" -c go
------
Replace the <servername> with your servername
For each server add a new line and with the changed servername
(to create a CMD file: open notepad and paste the final script and
save it as scriptfile.cmd)
Once you have the CMD file, open commandprompt and go to the drive
location and enter the name of the file
In my case it is:
------
c:\dbccscript.cmd
------
This will display all the output to the commandprompt. You can
redirect all the output to a log/txt file using -o switch in the cmd
script.
Note that sqlcmd is a new utility in MS SQL server 2005.
in SQL Server 2000 you can achieve the same using "OSQL" books online
has enuf info on this to help you rewrite it for osql.
Regards,
schal|||Thanks shal, unfortunately I don't have "shell - cmd" access to
server, I have to shrink all databases from SQL script only.|||What schal posted will shrink all databases for the server you are connected to:
EXEC sp_MSForEachDB @.Command1 = N'DBCC SHRINKDATABASE (?, 10)', @.replacechar = '?'"
But I strongly suggest you read http://www.karaszi.com/SQLServer/info_dont_shrink.asp first.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<anxcomp@.gmail.com> wrote in message news:1186682763.588386.295740@.19g2000hsx.googlegroups.com...
> Thanks shal, unfortunately I don't have "shell - cmd" access to
> server, I have to shrink all databases from SQL script only.
>|||It really works, I forgot that is normal storage procedure which I can
run from Query, sorry.
But what if I'd like first use SHRINKFILE all log files (all .LDF
files at server), log files because it takes me much more space than
databases? Logical name of log file is usually DatabaseName_Log.
How shrink log file for all databases, is it similar procedure?|||Try this!
It´s recovery system space quickly:
use master
go
declare curdb cursor for select name from sysdatabases where name not in
('tempdb','model','master')
declare @.nmdb varchar(50)
declare @.cmd varchar(1000)
open curdb
fetch next from curdb into @.nmdb
while @.@.fetch_status = 0
begin
print '======='
print @.nmdb
print '======='
select @.cmd = 'Use ' + @.nmdb
select @.cmd = @.cmd + ' if exists(select 1 from sysobjects where name =''limpa'')
begin
DROP TABLE LIMPA
end
create table limpa (char1 char(4000))
Set NoCount On
dbcc shrinkFILE (2, notruncate)
dbcc shrinkFILE (2, truncateonly)
dbcc shrinkFILE (1, notruncate)
dbcc shrinkFILE (1, truncateonly)
declare @.i int
select @.i = 0
While (@.i < 100)
begin
insert into limpa values (''a'')
select @.i =@.i + 1
end
backup log ' + @.nmdb + ' with truncate_only
drop table limpa'
Execute (@.cmd)
fetch next from curdb into @.nmdb
end
deallocate curdb
set nocount off
anxcomp@.gmail.com wrote:
>Hi,
>May somebody send script which will shrink all databases on server
>(SQL 2005). I think about DBCC SHRINKFILE and DBCC SHRINKDATABASE.
>I need do this at many servers, so I can't do this manually.
>Thanks
>--
>Regards
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200708/1|||Thanks, if thought it is simples way to do this. Could you me tell me
how at loop take:
1. name for all databases (without system dbs)
2. logical name of databases
2. logical name of transaction logs.
I need this information for this
BACKUP LOG @.DB_NAME
DBCC SHRINKFILE (@.DB_LOG_NAME)
I'd like this two lines will execute for all database.
Thanks
--
Regards|||I wrote my own script and I'd like share it with you:
CREATE TABLE #TDatabases(
DBName nvarchar(128),
DBLogicalName nvarchar(128)
)
INSERT INTO #TDatabases
SELECT db.name DBName, mf.name DBLogicalName
FROM sys.databases db join sys.master_files mf
on db.database_id = mf.database_id
WHERE db.name not in ('master', 'tempdb', 'model', 'msdb',
'distribution') AND type_desc LIKE 'log'
SET NOCOUNT ON
DECLARE @.VarDBLogicalName nvarchar(128)
DECLARE @.VarDBName nvarchar(128)
DECLARE @.VarRowCount int
SELECT top 1 @.VarDBName = DBName, @.VarDBLogicalName = DBLogicalName
FROM #TDatabases
SET @.VarRowCount = @.@.rowcount
WHILE @.VarRowCount <> 0
BEGIN
EXEC(' use ' + @.VarDBName + ' backup log '+ @.VarDBName + ' with no_log
dbcc shrinkfile(''' + @.VarDBLogicalName + ''', TRUNCATEONLY) WITH
NO_INFOMSGS')
DELETE
FROM #TDatabases
WHERE DBName = @.VarDBName
SELECT top 1 @.VarDBName = DBName, @.VarDBLogicalName =DBLogicalName
FROM #TDatabases
SET @.VarRowCount = @.@.ROWCOUNT
END
DROP TABLE #TDatabases
SET NOCOUNT OFF
This works but use something what Microsoft NOT recommend "backup log
with no_log" you can modify it how you like
--
Regardssql
Tuesday, February 21, 2012
Scope_Identity vs manually increment + 1 clarification needed
Hi
I was wondering if somebody could help clarifying the issue of
Scope_Identity vs manually increment + 1 .
Where I work since the days of sql server 7 we use our own way to get the next Identifier.
By having a table with (name and value column) EG CustomerID 234 and when we do an insert we update this Table by incrementing + 1 EG CustomerID 235.
I beleive that this way of doing things is not good and should be replaced by scope_Identity.
The reason I was given was to avoid Locking Tables whilst inserts executes whether in transactional operation or not.
I totally disagree with the above but cannot demostrate it.
My understanding of Scope_Identity is that is the most secure way to get the next Identifier by getting things
that are only in scope (EG A stored Procedure,statements etcc)
Now my question is:
Does scope_Indentity performs any locks of some sort ,or slows the process down?
Thanks a lot in advance
No there is no lock for IDENTITY columns.
There are lot of points there to say manyally +1 is inefficeint.
1. You have to lock the record - identity never use any lock
2. You have to fetch the last value from your table - identity uses the LOG no query
3. You have to manullay increment the value - idenity does the same
4. You have to wirte the code manually on triggers - identity is internal opertion, you need not to write any code
5. Fetching the last inserted value , again a overhead to write a few line of code - idenity values can be fetched easily
Last but not least Idenity never cause any performance issue, but it may cause GAPs on your sequential value.
Want to know more see this article.. http://blogs.msdn.com/sqlprogrammability/archive/2006/04/04/567724.aspx
|||Fantastic reply.Thanks!!!. If there are anymore points please add it here. I will wait a bit in case somebody else adds something else and then pack in a email and send it to my collegues who were not conviced.
Thanks again for such a clean answer