用于生成SQL Server数据库对象脚本的选项

ZDNet软件频道 时间:2008-11-24 作者: |  我要评论(2)
本文关键词:SQL Server 2008 SQL Server SQL Server
使用SQL Server管理套件的脚本选项定制工具使你能够让你的脚本如你所需的详细或改进。脚本生成是最适合你的环境的。
要感谢在Microsoft的贡献者们在SQL Server管理套件中提供了一个设置参数选项用于脚本输出的方法了。在ToolsOptions下面有一个用于改变与管理套件各个方面相关的各种选项对话框表单。在这个表单的左边面板中选择Scripting将显示你可以用来使你的脚本更加适合于你的各种格式化设置,Microsoft开发人员正是为所有数据库专业人员考虑的。

  用于生成SQL Server数据库对象脚本的选项1

  图1

  用于生成SQL Server数据库对象脚本的选项2

  图2

  这时,我将编写Northwind数据库的dbo.Categories表。数据库对象的编写是由在对象管理器中选择感兴趣的对象来完成的。右键单击菜单然后选择Script Table AsCREATE ToFile。这将创建一个具有要创建所需表所需要的Transact SQL代码的.sql文件。

  这个表默认的脚本输出如下所示:

  

      USE [Northwind]

  GO

  /****** Object: Table [dbo].[Categories] Script Date: 05/18/2008 21:38:24 ******/

  SET ANSI_NULLS ON

  GO

  SET QUOTED_IDENTIFIER ON

  GO

  CREATE TABLE [dbo].[Categories](

  [CategoryID] [int] IDENTITY(1,1) NOT NULL,

  [CategoryName] [nvarchar](15) NOT NULL,

  [Description] [ntext] NULL,

  [Picture] [image] NULL,

  CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED

  (

  [CategoryID] 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]

现在让我们看看一些更重要的脚本修改选项对这个脚本所起的作用。

  包括描述性的Header: 正如你所期望的,这个选项决定在你的脚本中描述性的header是ON还是OFF。

  

      USE [Northwind]

  GO

  SET ANSI_NULLS ON

  GO

  SET QUOTED_IDENTIFIER ON

  GO

  CREATE TABLE [dbo].[Categories](

  [CategoryID] [int] IDENTITY(1,1) NOT NULL,

  [CategoryName] [nvarchar](15) NOT NULL,

  [Description] [ntext] NULL,

  [Picture] [image] NULL,

  CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED

  (

  [CategoryID] 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]

  包括IF NOT EXISTS条件从句: 这个设置表示是否在创建一个对象之前检查它是否存在。

  

     USE [Northwind]

  GO

  /****** Object: Table [dbo].[Categories] Script Date: 05/18/2008 21:55:28 ******/

  SET ANSI_NULLS ON

  GO

  SET QUOTED_IDENTIFIER ON

  GO

  IF NOT EXISTS (SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[Categories]') AND type IN (N'U'))

  BEGIN

  CREATE TABLE [dbo].[Categories](

  [CategoryID] [int] IDENTITY(1,1) NOT NULL,

  [CategoryName] [nvarchar](15) NOT NULL,

  [Description] [ntext] NULL,

  [Picture] [image] NULL,

  CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED

  (

  [CategoryID] 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]

  END

脚本权限:将产生要生成的这个或这些对象所关联的数据库用户和角色权限。

     USE [Northwind]

  GO

  /****** Object: Table [dbo].[Categories] Script Date: 05/18/2008 22:04:45 ******/

  SET ANSI_NULLS ON

  GO

  SET QUOTED_IDENTIFIER ON

  GO

  CREATE TABLE [dbo].[Categories](

  [CategoryID] [int] IDENTITY(1,1) NOT NULL,

  [CategoryName] [nvarchar](15) NOT NULL,

  [Description] [ntext] NULL,

  [Picture] [image] NULL,

  CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED

  (

  [CategoryID] 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

  GRANT DELETE ON [dbo].[Categories] TO [public]

  GO

  GRANT INSERT ON [dbo].[Categories] TO [public]

  GO

  GRANT REFERENCES ON [dbo].[Categories] TO [public]

  GO

  GRANT SELECT ON [dbo].[Categories] TO [public]

  GO

  GRANT UPDATE ON [dbo].[Categories] TO [public]

脚本索引: 将为要生成的表生成任何关联的索引。

  

      USE [Northwind]

  GO

  /****** Object: Table [dbo].[Categories] Script Date: 05/18/2008 22:11:32 ******/

  SET ANSI_NULLS ON

  GO

  SET QUOTED_IDENTIFIER ON

  GO

  CREATE TABLE [dbo].[Categories](

  [CategoryID] [int] IDENTITY(1,1) NOT NULL,

  [CategoryName] [nvarchar](15) NOT NULL,

  [Description] [ntext] NULL,

  [Picture] [image] NULL,

  CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED

  (

  [CategoryID] 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

  /****** Object: Index [CategoryName] Script Date: 05/18/2008 22:11:32 ******/

  CREATE NONCLUSTERED INDEX [CategoryName] ON [dbo].[Categories]

  (

  [CategoryName] ASC

  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF,

  DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

  使用SQL Server管理套件的脚本选项定制工具使你能够让你的脚本如你所需的详细或改进。脚本生成是最适合你的环境的。


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134