Tested in 2008 R2, 2012 and 2014 all on the latest SP + CU
When I generate as script from SSMS for the database tables via Generate Scripts there is an issue, when you generate the script for the database objects and there is data in the table with the options set to false for "Constraints and Defaults.
Scripting defaults = false (This always outputs when there is data, I think this is where the bug is)
Scripting Check Constraints = False ( had this also set, but didn't make any difference if it was on or off)
This is my test tables, table A has data and Table B has "no data".
Issue also exists in Powershell using SMO to generate a script for the tables
I don't want to output the Default values in the script? But if there is data then it will output the defaults as shown below.
How do I fix?
This is the output:
USE [tktest1] GO /****** Object: Table [dbo].[TableA] Script Date: 2015-04-18 9:16:48 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[TableA]( [TableID] [int] IDENTITY(1,1) NOT NULL, [Col1] [varchar](50) NULL, [Col2] [varchar](50) NULL CONSTRAINT [DF_TableA_Col2] DEFAULT ('Y'), CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED ( [TableID] 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 SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[TableB] Script Date: 2015-04-18 9:16:48 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[TableB]( [TableID] [int] IDENTITY(1,1) NOT NULL, [Col1] [varchar](50) NULL, [Col2] [varchar](50) NULL, CONSTRAINT [PK_TableB] PRIMARY KEY CLUSTERED ( [TableID] 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 SET ANSI_PADDING OFF GO
This is using the Script table as from SSMS:
Table A
USE [tktest1] GO /****** Object: Table [dbo].[TableA] Script Date: 2015-04-18 10:07:41 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[TableA]( [TableID] [int] IDENTITY(1,1) NOT NULL, [Col1] [varchar](50) NULL, [Col2] [varchar](50) NULL CONSTRAINT [DF_TableA_Col2] DEFAULT ('Y'), CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED ( [TableID] 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 SET ANSI_PADDING OFF GO
Table B
USE [tktest1] GO /****** Object: Table [dbo].[TableB] Script Date: 2015-04-18 9:23:02 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[TableB]( [TableID] [int] IDENTITY(1,1) NOT NULL, [Col1] [varchar](50) NULL, [Col2] [varchar](50) NULL, CONSTRAINT [PK_TableB] PRIMARY KEY CLUSTERED ( [TableID] 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 SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[TableB] ADD CONSTRAINT [DF_TableB_Col2] DEFAULT ('Y') FOR [Col2] GO