help with sql script

Status
Not open for further replies.

MichaelC

Ars Legatus Legionis
34,233
So, I'm trying to update an sql script we use for dumping databases. I want to add a shrink to the script but it's not working and I don't really understand why. SQL is not a strong point for me. The script works as expected until I try to add the dbcc shrink...

Code:
DECLARE @IDENT INT, @sql VARCHAR(1000), @DBNAME VARCHAR(200), @DATE VARCHAR (20)
SELECT @IDENT=MIN(database_id) FROM SYS.DATABASES WHERE [database_id] > 0 AND NAME NOT IN ('TEMPDB')
WHILE @IDENT IS NOT NULL
BEGIN
SELECT @DBNAME = NAME FROM SYS.DATABASES WHERE database_id = @IDENT
/*Date stamp is in YYYY-MM-DD*/
SELECT @DATE = CONVERT (date, GETDATE())
/*Change disk location here as required*/
SELECT @SQL = 'BACKUP DATABASE ['+@DBNAME+'] TO DISK = ''C:\foo\foofoo\'+@DBNAME+'-Full-db-' +@DATE+'.bak''';
/*shrink db*/
SELECT @SQL = 'DBCC SHRINKDATABASE ['+@DBNAME+'],10';
EXEC (@SQL)
SELECT @IDENT=MIN(database_id) FROM SYS.DATABASES WHERE [database_id] > 0 AND database_id>@IDENT AND NAME NOT IN ('TEMPDB')
END
 

Ardax

Ars Legatus Legionis
19,794
Subscriptor
You've overwritten your "BACKUP DATABASE" command with the "DBCC SHRINKDATABASE" command.

Put an "EXEC(@SQL)" in between the two.

Also, the examples on TechNet for DBCC SHRINKDATABASE show the parameters for the command in parenthesis, so those may be required. Also, instead of manually putting brackets around your @DBNAME, you should be using the QUOTENAME function if you're using MS SQL 2005 or newer.

Code:
SELECT @SQL = 'DBCC SHRINKDATABASE ('+QUOTENAME(@DBNAME)',10);';
 

Ardax

Ars Legatus Legionis
19,794
Subscriptor
It may not be the best, most compact compressed backup but it usually does a good enough job.
And it takes a lot less time and space to do it inline than it does to build the backup then compress it. I'm not sure if it's possible to stream MS SQL DB backups and restores so they can be piped through a streaming compressor.
 

MichaelC

Ars Legatus Legionis
34,233
I thought I stated this was for SQL express editions... There are a range from 2005 to 2012. We do have SQL Web edition and... professional? We do use the maintenance plans for those servers, but you cannot do that with SQL Express.

So we run these cron jobs to dump and zip the databases. We do the same with MySQL. I had wanted to update the scripts to change the date format and add the shrink and to do some other things.

These are all usually smaller databases, but there will usually be several on a system. I wanted to add the shrink because of the nature of the databases. The customers are not SQL experts and often use really awful queries, or let certain tables grow exponentially. So often times there may be a lot of free space to clear up which makes a difference on small virtual servers.
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
[url=http:/civis/viewtopic.php?p=29949131#p29949131:1uzu2sev said:
MichaelC[/url]":1uzu2sev]...I had wanted to update the scripts to change the date format ...
Sorry for being late to the party.
Question about the line quoted above.
I am just wondering if date-format is a point you wanted to affect/control, then why did you not use an explicit conversion from the date datatype to a varchar using a specific style?
Your code uses an implicit conversion.

Code:
SELECT @DATE = CONVERT (date, GETDATE())

The left hand side @DATE variable was declared as a varchar(20).
The right hand side results in a date value. It contains only a year, month and day value without further timing.
Assigning this value to the @DATE variable will result in an implicit conversion of the date to a varchar and the actual format depends on server settings / db settings.

Something like
Code:
SELECT @DATE = CONVERT(varchar, CONVERT (date, GETDATE()), desiredstyle)
would give you full controll over the actual format.
 
Status
Not open for further replies.