通过在不同服务器上生成脚本来运行脚本会产生错误

时间:2010-10-23 07:57:36

标签: sql-server-2008 scripting

以下是使用生成脚本选项生成的脚本。

向导说这可以用来复制,在不同的服务器上创建数据库

所以它为它做了,那么为什么会发生这种错误。??

错误是

Msg 5170, Level 16, State 1, Line 2
Cannot create file 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SECOND\MSSQL\DATA\Script Me.mdf' because it already exists. Change the file path or the file name, and retry the operation.
Msg 1802, Level 16, State 4, Line 2

无法在sysdatabes

中为“script me”添加条目

混淆,

为什么它尝试在同一台服务器上创建,我的意思是为什么脚本是这样编写的?

脚本

USE [master]
GO
/****** Object:  Database [Script Me]    Script Date: 10/23/2010 12:38:57 ******/
CREATE DATABASE [Script Me] ON  PRIMARY 
( NAME = N'Script Me', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SECOND\MSSQL\DATA\Script Me.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'Script Me_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SECOND\MSSQL\DATA\Script Me_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [Script Me] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Script Me].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [Script Me] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [Script Me] SET ANSI_NULLS OFF
GO
ALTER DATABASE [Script Me] SET ANSI_PADDING OFF
GO
ALTER DATABASE [Script Me] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [Script Me] SET ARITHABORT OFF
GO
ALTER DATABASE [Script Me] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [Script Me] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [Script Me] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [Script Me] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [Script Me] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [Script Me] SET CURSOR_DEFAULT  GLOBAL
GO
ALTER DATABASE [Script Me] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [Script Me] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [Script Me] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [Script Me] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [Script Me] SET  DISABLE_BROKER
GO
ALTER DATABASE [Script Me] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [Script Me] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [Script Me] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [Script Me] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [Script Me] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [Script Me] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [Script Me] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [Script Me] SET  READ_WRITE
GO
ALTER DATABASE [Script Me] SET RECOVERY FULL
GO
ALTER DATABASE [Script Me] SET  MULTI_USER
GO
ALTER DATABASE [Script Me] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [Script Me] SET DB_CHAINING OFF
GO
EXEC sys.sp_db_vardecimal_storage_format N'Script Me', N'ON'
GO
USE [Script Me]
GO
/****** Object:  Table [dbo].[TableOne]    Script Date: 10/23/2010 12:38:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TableOne](
 [id] [nchar](10) NOT NULL,
 [name] [nchar](10) NULL,
 CONSTRAINT [PK_TableOne] PRIMARY KEY CLUSTERED 
(
 [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

我在做什么

  • 从一台服务器创建db脚本
  • 尝试在其他地方运行它,以便在那里创建相同的表

2 个答案:

答案 0 :(得分:1)

因为您已打开create database部分,并且错误表明该数据库已存在。只需运行最后一点:

USE [Script Me] 
GO 
/****** Object:  Table [dbo].[TableOne]    Script Date: 10/23/2010 12:38:59 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[TableOne]( 
 [id] [nchar](10) NOT NULL, 
 [name] [nchar](10) NULL, 
 CONSTRAINT [PK_TableOne] PRIMARY KEY CLUSTERED  
( 
 [id] ASC 
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] 
) ON [PRIMARY] 
GO 

更新:如果您的SQL Server是生产框,那么希望数据库文件不在此处:"'C:\Program Files\Microsoft SQL Server"

更改脚本中的数据库路径,使其指向特定计算机的数据库文件的文件夹位置。

答案 1 :(得分:1)

编写脚本以替换自身。任何更改都可以,但它们取决于您。您应该始终(无论使用哪种工具来创建脚本)阅读脚本以确保它能够执行您希望它执行的操作(以及您希望它执行的操作)。

您应该修改脚本,将以下内容替换为您想要的内容:

CREATE DATABASE [Script Me] ON  PRIMARY 
( NAME = N'Script Me', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SECOND\MSSQL\DATA\Script Me.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'Script Me_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SECOND\MSSQL\DATA\Script Me_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB ,     FILEGROWTH = 10%)
GO

在这里,您应该更改路径和文件名。路径取决于您,但文件名通常遵循以下命名:

  • 数据:{数据库名称} .mdf
  • 日志:{数据库名称} _log.ldf

您还应该将Script Me更改为脚本文件中存在的新数据库名称。