简体   繁体   中英

Can't create database in Windows 8 with SQL Server 2008 R2

I'm trying to run install scripts that create a database for our application they have been working until our upgrade to Windows 8. Installed SQL Server 2008 R2 Express and run scripts that execute the following to create the DB:

USE [master]
GO

SET NUMERIC_ROUNDABORT OFF
GO
SET XACT_ABORT, ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS, NOCOUNT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO

IF EXISTS (SELECT name FROM sys.databases WHERE name = N'$(db)')
BEGIN
    ALTER Database [$(db)] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    DROP Database [$(db)]
END
GO

CREATE DATABASE [$(db)]
GO
USE  [$(db)]
GO

I can run the following command line to execute the script:

sqlcmd -i test.sql -S .\wizrms -d test -U sa -P W1zard

I get the following error:

Msg 4060, Level 11, State 1, Server .\\WIZRMS, Line 1
Cannot open database "test" requested by the login, The login failed.

Msg 18456, Level 14, State 1 Server .\\WIZRMS, Line 1
Login failed for user 'sa'.

I can login using SQL Server Management Studio with the same credentials and create the database with the same command.

And this all works on Windows 7. My assumption is there is a security change in Win 8 that I have to deal with.

It's probably because of the parameter -d test , specifying an initial database that doesn't yet exists. When the script you're running creates the database, you typically login in master DB first, then create the DB, then switch to it for all the remaining work. But trying to open that database before it has been created will for sure yield the error you're posting here.

SSMS does exactly that. It always connect to master initially, then switches databases as you work on different DBs. But the CREATE DATABASE command is always issued in the context of master .

Windows version doesn't matters here. Possibly, in your Win7 install you're connecting to a server with a pre-existing test database, while in your Win8 computer, it doesn't exists yet.

Thanks Alejandro.

I changed -d to master and assign the database by using -v db = fleeb

my command line now reads:

sqlcmd -i test.sql -S sof202\\wizrms -d master -U sa -P W1zard -v db = fleeb

And it ran without error and created a database named fleeb.

Perfect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM