Search code examples
sqlsql-servert-sqlalter-tablecreate-table

T-SQL: modified CREATE TABLE script to ALTER TABLE script crashes


This script was generated from SQL Server Management Studio. Why am I getting this error and how can I fix the ALTER script? Note, that I changed the word "CREATE" to "ALTER". So I'm guessing the CREATE syntax is slightly different from the ALTER syntax, or has less restrictions. I'm using SQL Server 2008 Express. I see it's telling me to end with a semicolon, but I want to make sure terminating the statement won't cause me any issues later. If you can describe what each piece of the query does and what we're changing, bonus points awarded.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--CREATE TABLE [dbo].[defect](
ALTER TABLE [dbo].[defect](
    [defect_id] [bigint] IDENTITY(1,1) NOT NULL,
    [defect_number] [nvarchar](50) NULL,
    [defect_type] [nvarchar](50) NULL,
    [date_created] [datetime] NULL,
    [user_identified_by] [nvarchar](50) NULL,
    [status] [nvarchar](50) NULL,
    [date_modified] [datetime] NULL,
    [user_qa] [nvarchar](50) NULL,
    [severity] [nvarchar](50) NULL,
    [environment] [nvarchar](50) NULL,
    [user_assigned] [nvarchar](50) NULL,
    [project] [nvarchar](50) NULL,
    [target_table_affected] [nvarchar](50) NULL,
    [required_for_go_live] [nvarchar](50) NULL,
    [project_phase] [nvarchar](50) NULL,
    [completion_hours] [nvarchar](50) NULL,
    [date_migrate_prod] [datetime] NULL,
    [description] [text] NULL,
    [table_columns_affected] [text] NULL,
    [sample_data] [text] NULL,
    [action_taken] [text] NULL,
    [supp_detail_links] [text] NULL,
    [supp_detail_links_dtml] [text] NULL,
    [thread] [bigint] NULL,
 CONSTRAINT [PK_defect] PRIMARY KEY CLUSTERED 
(
    [defect_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

Error:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('.
Msg 319, Level 15, State 1, Line 29
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.


Solution

  • ALTER TABLE and CREATE TABLE are two totally different SQL statements that you cannot acheive by simply replacing CREATE with ALTER, so the way you generated your ALTER is wrong, what you can do is

    1. you can simply generate the ALTER script using the Change Script option available in SSMS every time when you change any Table or db object.
    2. Alternatively you can generate Alter scripts using VS2010(google for it as I am not gonna write this here) or any third party script generator, easily available on internet

    Additionally, instead of posting SQL statements on SO for others to explain them for you, give sometime to SQL learning (at least using SQL books online, installed with SQL server).