I'm trying to do an Entity Framework walkthrough so I:
downloaded SQL script here: http://www.learnentityframework.com
in SQL Server Management Studio, I right-clicked Database, Create Database, named it
right-clicked on the new database, New Query
clicked on "Open File" and opened the script file: Create_ProgrammingEFDB1_SQLServer2008.sql
clicked "! Execute"
But the script (756K) has been running for 10 minutes now and still says "executing..."
My questions are:
Is this the standard way to read in an SQL script into SQL Server?
Is it supposed to take this long? This is how I would do it in MySQL/PHPMyAdmin it it might take a couple seconds, so I assume I'm not doing something right.
Here is the beginning of the script, I changed the file paths so they point to the right .mdf and .ldf files:
****/
--PART ONE CREATE THE DATABASE. Note the file paths in the first few commands.
--Change them for your own computer.--
USE [master]
GO
/****** Object: Database [ProgrammingEFDB1] Script Date: 01/28/2009 10:17:44 ******/
CREATE DATABASE [ProgrammingEFDB1] ON PRIMARY
( NAME = N'ProgrammingEFDB1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1.mdf' , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'ProgrammingEFDB1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1_log.LDF' , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
ALTER DATABASE [ProgrammingEFDB1] SET COMPATIBILITY_LEVEL = 90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ProgrammingEFDB1].[dbo].[sp_fulltext_database] #action = 'disable'
end
...
ANSWER:
I had already created a database with the same name so it was trying to create a database that was already there which made it hang for some reason. I deleted that database, reran the script and it completed successfully in 3 seconds.
I don't know what does your script do exactly in the next 754K, but the lines you posted seem quite harmless.
Try adding the following to your script:
SET STATISTICS TIME ON
This will show queries execution times as they run, and it will help you to locate the problem more exactly.
But the script (756K)
Must be a lot more than just a CREATE DATABASE in the script, so very hard to say when the script is doing.
You can write progress reports from the script back to the client, or use SQL Profiler to see what commands are being executed.
Related
Below query used to create database in SQL Server 2016
CREATE DATABASE TestDB_v821 ON ( Name = 'TestDB_v821', FILENAME = 'E:\Data\TestDB_v821.mdf', FILEGROWTH = 10%)
LOG ON ( NAME = 'TestDB_v821_Log', FILENAME = 'E:\Log\TestDB_v821_Log.ldf', FILEGROWTH = 10%)
The query taking more time than normal. When i check the log file size using select * from sys.dm_db_log_space_usage
database_id total_log_size_in_bytes used_log_space_in_bytes used_log_space_in_percent log_space_in_bytes_since_last_backup
----------- ----------------------- ----------------------- ------------------------- ------------------------------------
17 2508193792 8749568 0.3488394 225280
Need to know why the simple create database statement creates the hug log file. It is same behavior when try to create other database also. Is it due to some setting. Please help me on this.
It will take the initial size and auto-growth settings from the model database, so you need to reduce it there if you want smaller defaults.
I have a ELMAH database that I want to script the restore of using the following:
RESTORE DATABASE [Elmah]
FROM DISK = N'E:\Elmah_backup_2012_11_02_030003_1700702.bak'
WITH FILE = 1,
MOVE N'Elmah' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Elmah.mdf',
MOVE N'Elmah_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Elmah.ldf',
NOUNLOAD, STATS = 10
GO
Even though I am not including WITH REPLACE each time I execute this statement it restores over the existing database.
I will always drop all databases before this operation and I never want this code to accidentally restore a database over one in production.
How do I change this code so that it will never overwrite an existing database?
I am actually doing this through SMO objects but the principle and results are the same so I am hoping to keep this simplified to just the TSQL necessary in the hopes that I can generalize that information to what needs to be set on the appropriate SMO.Restore object.
You need to (a) give the restored database a new logical name, and (b) you need to define new physical file names, so the existing ones won't be overwritten. Try something like this:
RESTORE DATABASE [Elmah_Restored] <== new (and unique) logical database name
FROM DISK = N'E:\Elmah_backup_2012_11_02_030003_1700702.bak'
WITH FILE = 1,
MOVE N'Elmah' TO
N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Elmah_restored.mdf', <== new (and unique) physical file name
MOVE N'Elmah_log' TO
N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Elmah_restored_log.ldf', <== new (and unique) physical file name
NOUNLOAD, STATS = 10
GO
I am new to this. I have a database (created by someone else) that has 2 .ldf files. (blah_log.ldf and blah_log2.ldf). My manager asked me to remove one of the log files but I cannot. How do I do this? I tried to put it on another server, detach, delete log files, attach, but it gives an error. I thought that way it would create just one, but it wanted both. Then i tried to right click properties and delete the files, would not let me delete. It said the log file was not empty. How in the heck do I achieve this. I just want to make it where the dang database has one freaking log file not two. This shouldn't be this complicated. I am a beginner and know nothing so maybe it isn't really. Please HELP!
I just tried this:
empty SQL Server database transaction log file
backup log [dbname] with truncate_only
go
DBCC SHRINKDATABASE ([dbname], 10, TRUNCATEONLY)
go
Then I deleted the second log file and clicked ok. I guess this is all I need to do? I tried it on a test server from a restore.
This MSDN article describes how to accomplish this at a high-level:
You cannot move transaction log data from one log file to another to
empty a transaction log file. To remove inactive transactions from a
transaction log file, the transaction log must be truncated or backed
up. When the transaction log file no longer contains any active or
inactive transactions, the log file can be removed from the database.
And this blog post shows the actual T-SQL that will accomplish this task:
USE master
IF DB_ID('rDb') IS NOT NULL DROP DATABASE rDb
GO
CREATE DATABASE rDb
ON
PRIMARY
( NAME = N'rDb', FILENAME = N'C:\rDb.mdf' , SIZE = 50MB ,
FILEGROWTH = 1024KB )
LOG ON
(NAME = N'rDb_log2', FILENAME = N'C:\rDb_log2.ldf', SIZE = 3MB,
FILEGROWTH = 2MB)
,(NAME = N'rDb_log3', FILENAME = N'C:\rDb_log3.ldf', SIZE = 3MB,
FILEGROWTH = 2MB)
,(NAME = N'rDb_log4', FILENAME = N'C:\rDb_log4.ldf', SIZE = 3MB,
FILEGROWTH = 2MB)
GO
ALTER DATABASE rDb SET RECOVERY FULL
BACKUP DATABASE rDb TO DISK = 'C:\rDb.bak' WITH INIT
CREATE TABLE rDb..t(c1 INT IDENTITY, c2 CHAR(100))
INSERT INTO rDb..t
SELECT TOP(15000) 'hello'
FROM syscolumns AS a
CROSS JOIN syscolumns AS b
--Log is now about 46% full
DBCC SQLPERF(logspace)
--Check virtual log file layout
DBCC LOGINFO(rDb)
--See that file 4 isn't used at all (Status = 0 for all 4's rows)
--We can remove file 4, it isn't used
ALTER DATABASE rDb REMOVE FILE rDb_log4
--Check virtual log file layout
DBCC LOGINFO(rDb)
--Can't remove 3 since it is in use
ALTER DATABASE rDb REMOVE FILE rDb_log3
--What if we backup log?
BACKUP LOG rDb TO DISK = 'C:\rDb.bak'
--Check virtual log file layout
DBCC LOGINFO(rDb)
--3 is still in use (status = 2)
--Can't remove 3 since it is in use
ALTER DATABASE rDb REMOVE FILE rDb_log3
--Shrink 3
USE rDb
DBCC SHRINKFILE(rDb_log3)
USE master
--... and backup log?
BACKUP LOG rDb TO DISK = 'C:\rDb.bak'
--Check virtual log file layout
DBCC LOGINFO(rDb)
--3 is no longer in use
--Can now remove 3 since it is not in use
ALTER DATABASE rDb REMOVE FILE rDb_log3
--Check explorer, we're down to 1 log file
--See what sys.database_files say?
SELECT * FROM rDb.sys.database_files
--Seems physical file is gone, but SQL Server consider the file offline
--Backup log does it:
BACKUP LOG rDb TO DISK = 'C:\rDb.bak'
SELECT * FROM rDb.sys.database_files
--Can never remove the first ("primary") log file
ALTER DATABASE rDb REMOVE FILE rDb_log2
--Note error message from above
I was trying to create a backup for my SQL Server Database using SQL Server Management Studio 2008 Express. I have created the backup but it is getting saved at some path which I am not able to find. I am saving it on my local HD and I checked in Program Files>Microsoft SQL Server>MSSQL 1.0>MSSQL>DATA> but its not there.
What's the default save path for this DB .bak?
Should be in
Program Files>Microsoft SQL Server>MSSQL 1.0>MSSQL>BACKUP>
In my case it is
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup
If you use the gui or T-SQL you can specify where you want it
T-SQL example
BACKUP DATABASE [YourDB] TO DISK = N'SomePath\YourDB.bak'
WITH NOFORMAT, NOINIT, NAME = N'YourDB Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
With T-SQL you can also get the location of the backup, see here Getting the physical device name and backup time for a SQL Server database
SELECT physical_device_name,
backup_start_date,
backup_finish_date,
backup_size/1024.0 AS BackupSizeKB
FROM msdb.dbo.backupset b
JOIN msdb.dbo.backupmediafamily m ON b.media_set_id = m.media_set_id
WHERE database_name = 'YourDB'
ORDER BY backup_finish_date DESC
If the backup wasn't created in the default location, you can use this T-SQL (run this in SSMS) to find the file path for the most recent backup for all DBs on your SQL Server instance:
SELECT DatabaseName = x.database_name,
LastBackupFileName = x.physical_device_name,
LastBackupDatetime = x.backup_start_date
FROM ( SELECT bs.database_name,
bs.backup_start_date,
bmf.physical_device_name,
Ordinal = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
FROM msdb.dbo.backupmediafamily bmf
JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
WHERE bs.[type] = 'D'
AND bs.is_copy_only = 0 ) x
WHERE x.Ordinal = 1
ORDER BY DatabaseName;
As said by Faiyaz, to get default backup location for the instance, you cannot get it into msdb, but you have to look into Registry. You can get it in T-SQL in using xp_instance_regread stored procedure like this:
EXEC master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer',N'BackupDirectory'
The double backslash (\\) is because the spaces into that key name part (Microsoft SQL Server).
The "MSSQL12.MSSQLSERVER" part is for default instance name for SQL 2014. You have to adapt to put your own instance name (look into Registry).
Set registry item for your server instance. For example:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.2\MSSQLServer\BackupDirectory
Use the script below, and switch the DatabaseName with then name of the database that you've backed up. On the column physical_device_name, you'll have the full path of your backed-up database:
select a.backup_set_id, a.server_name, a.database_name, a.name, a.user_name, a.position, a.software_major_version, a.backup_start_date, backup_finish_date, a.backup_size, a.recovery_model, b.physical_device_name
from msdb.dbo.backupset a join msdb.dbo.backupmediafamily b
on a.media_set_id = b.media_set_id
where a.database_name = 'DatabaseName'
order by a.backup_finish_date desc
I dont think default backup location is stored within the SQL server itself.
The settings are stored in Registry. Look for "BackupDirectory" key and you'll find the default backup.
The "msdb.dbo.backupset" table consists of list of backups taken, if no backup is taken for a database, it won't show you anything
...\Program Files\Microsoft SQL Server\MSSQL 1.0\MSSQL\Backup
have you tried:
C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\Backup
Script to get all backups in the last week can be found at:
http://wraithnath.blogspot.com/2010/12/how-to-find-all-database-backups-in.html
I have plenty more backup SQL scripts there also at
http://wraithnath.blogspot.com/search/label/SQL
So eventually on SQL Server 2019 you can just call built in SERVERPROPERTY function to query the default backup folder:
select serverproperty('InstanceDefaultBackupPath')
You may want to take a look here, this tool saves a BAK file from a remote SQL Server to your local harddrive: FIDA BAK to local
I am trying to shrink my log file using DBCC SHRINKFILE(db_2.ldf), which is the name for log file
It gives me error every time:
8985, Level 16, State 1, Line 1
Could not locate file 'FIelD' for database db in sys.database_files. The file either does not exist, or was dropped.
Can you please suggest what can I do to fix it.
The file name should be the logical file name and not the physical file name. Look in the Database properties, on the Files tab for the Logical Name of the file you are trying to shrink, and use that name.
Are you running it in the context of the database that has the log you are trying to shrink? Make sure you have the right USE statement before running DBCC commands
Had the same problem over here, the solution was to rename the logical file to match the database name. Below is an example to query the logical file names and then perform a rename of the files:
-- retrieve the logical files for the current db
SELECT [name] AS logical_file FROM sys.database_files df
-- rename the logical file (to match the database name)
ALTER DATABASE YourDB
MODIFY FILE (NAME = 'LogicalFile1', NEWNAME='NewLogicalFile1')
GO
ALTER DATABASE YourDB
MODIFY FILE (NAME = 'LogicalFile2', NEWNAME='NewLogicalFile2')
GO
The reason for the two alters is that there are usually two files associated with each database, the data file and the log file.
The command below worked. However I don't see it reducing the size. I see the same size after and before running it. Can you please let me know what I might have missed.
fileid groupid size maxsize growth status perf name filename
2 0 1048 268435456 10 1048642 0 PrimaryLogFileName
Thanks
From SQL Management Studio
Right click the database name, Tasks, Shrink, Database
Ctrl+Shift+N (or Script Action to New Query Window)
Generates the following:
USE [DataBaseName]
GO
DBCC SHRINKDATABASE(N'DataBaseName' )
GO