SQL Server 2012 生成的脚本无法再次运行?
Posted
技术标签:
【中文标题】SQL Server 2012 生成的脚本无法再次运行?【英文标题】:Scripts generated by SQL Server 2012 can not run again? 【发布时间】:2016-03-03 08:45:29 【问题描述】:从数据库生成脚本时遇到问题。这很简单,但我不知道如何解决它。 存储过程对象在表对象之前创建。这是所有脚本:
USE [ComicSellerManagement]
GO
/****** Object: StoredProcedure [dbo].[sp_AddingComicGenre] Script Date: 3/3/2016 3:39:38 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[sp_AddingComicGenre]
@name nvarchar(50),
@info nvarchar(max)
as begin
insert into COMIC_GENRE values (@name, @info)
end
GO
/****** Object: StoredProcedure [dbo].[sp_DeletingComicGenre] Script Date: 3/3/2016 3:39:38 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[sp_DeletingComicGenre]
@id int
as begin
delete from COMIC_GENRE where ID = @id
end
GO
/****** Object: StoredProcedure [dbo].[sp_LoadingComicGenre] Script Date: 3/3/2016 3:39:38 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[sp_LoadingComicGenre]
as begin
select * from COMIC_GENRE
end
GO
/****** Object: StoredProcedure [dbo].[sp_UpdatingComicGenre] Script Date: 3/3/2016 3:39:38 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[sp_UpdatingComicGenre]
@id int,
@name nvarchar(50),
@info nvarchar(max)
as begin
update COMIC_GENRE
set GenreName = @name, Info = @info
where ID = @id
end
GO
/****** Object: Table [dbo].[COMIC] Script Date: 3/3/2016 3:39:38 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[COMIC](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Genre] [int] NULL,
[PublishYear] [int] NULL,
[Publisher] [int] NULL,
[Actor] [nvarchar](50) NULL,
[Price] [money] NULL,
CONSTRAINT [PK_COMIC] 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
/****** Object: Table [dbo].[COMIC_GENRE] Script Date: 3/3/2016 3:39:38 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[COMIC_GENRE](
[ID] [int] IDENTITY(1,1) NOT NULL,
[GenreName] [nvarchar](50) NULL,
[Info] [nvarchar](255) NULL,
CONSTRAINT [PK_COMIC_GENRE] 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
/****** Object: Table [dbo].[COMIC_PUBLISHER] Script Date: 3/3/2016 3:39:38 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[COMIC_PUBLISHER](
[ID] [int] IDENTITY(1,1) NOT NULL,
[PublisherName] [nvarchar](50) NULL,
[Adress] [nvarchar](max) NULL,
[PhoneContact] [char](11) NULL,
[Discount] [float] NULL,
CONSTRAINT [PK_COMIC_PUBLISHER] 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] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[COMIC_PUBLISHER] ADD CONSTRAINT [DF_COMIC_PUBLISHER_Discount] DEFAULT ((0)) FOR [Discount]
GO
ALTER TABLE [dbo].[COMIC] WITH CHECK ADD CONSTRAINT [FK_COMIC_COMIC_GENRE] FOREIGN KEY([Genre])
REFERENCES [dbo].[COMIC_GENRE] ([ID])
GO
ALTER TABLE [dbo].[COMIC] CHECK CONSTRAINT [FK_COMIC_COMIC_GENRE]
GO
ALTER TABLE [dbo].[COMIC] WITH CHECK ADD CONSTRAINT [FK_COMIC_COMIC_PUBLISHER] FOREIGN KEY([Publisher])
REFERENCES [dbo].[COMIC_PUBLISHER] ([ID])
GO
ALTER TABLE [dbo].[COMIC] CHECK CONSTRAINT [FK_COMIC_COMIC_PUBLISHER]
GO
我无法在任何计算机上运行此 sql 文件来恢复我的数据库。有什么建议可以解决这个问题吗?
【问题讨论】:
“我不能运行这个sql文件”,为什么不呢?USE [ComicSellerManagement]
- 希望您已经创建了一个具有该名称的数据库
先建表再建过程
@Raj 好点...请提供您的错误信息
旁注 - CREATE PROCEDURE
:“在命名过程时避免使用 sp_ 前缀。SQL Server 使用此前缀来指定系统过程”
【参考方案1】:
-
在生成脚本时尝试更改此选项 -
另一种解决方案尝试使用dbForge Schema Comparison
使用备份的
【讨论】:
我尝试了和你的一样的格式,但还是不行……无论如何,谢谢你的帮助:) 也许是最好的方法 - 在您的机器上创建备份并在另一台机器上恢复? 那是很好的解决方案,我会尝试制作bak文件,再次感谢:)以上是关于SQL Server 2012 生成的脚本无法再次运行?的主要内容,如果未能解决你的问题,请参考以下文章
SQL server 2008 r2附加数据库出现错误了,见下图,怎么解决?求帮助!!!!!
生成我的数据库数据的 SQL 脚本 (SQL SERVER)