Search code examples
sqlsql-serverdatabasecreate-table

Create SQL Server table error


I'm trying to create a new table with the results from a query. I'm working in SQL Server 2012

CREATE TABLE [Service Contract Data].[dbo].[filtered_data] AS 
(
    SELECT * 
    FROM [Service Contract Data].[dbo].[New Data]
    LEFT JOIN [Service Contract Data].[dbo].[OctMktSeg] ON [Service Contract Data].[dbo].[New Data].[Customer ID] = [Service Contract Data].[dbo].[OctMktSeg].[Account Number]
    LEFT JOIN [Service Contract Data].[dbo].[tblMktSeg&MPI] ON [Service Contract Data].[dbo].[New Data].[Customer ID] = [Service Contract Data].[dbo].[tblMktSeg&MPI].[Customer ID1]
    WHERE 
        (MKTSEG LIKE '%Repair%' OR MKTSEG LIKE 'Auto Glass' OR 
         MKTSEG LIKE 'IA - Individual' OR MKTSEG IS NULL)
        AND (MPI LIKE 'N' OR MPI IS NULL)
        AND [Item Category] LIKE '%Term'
        AND [Month] >= '2015-01-01 00:00:00.000')
    ;

It keeps telling me there's "Incorrect syntax" near the first open parentheses and I have no idea why. Thoughts?


Solution

  • I don't know if that syntax is valid in SQL SERVER, Have you tried this before ? This is valid syntax in order to create a new table:

    SELECT 
    -- SPECIFY THE NAME OF THE COLUMNS IN THE SELECT LIST, AVOID *
    * 
    --INSERT DATA INTO NEW TABLE
    INTO [Service Contract Data].[dbo].[filtered_data]
    from [Service Contract Data].[dbo].[New Data] t1
    left join [Service Contract Data].[dbo].[OctMktSeg] t2 ON t1.[Customer ID] = t2.[Account Number]
    left join [Service Contract Data].[dbo].[tblMktSeg&MPI] t3 on t1.[Customer ID] = t3.[Customer ID1]
    WHERE (MKTSEG like '%Repair%' or MKTSEG like 'Auto Glass' or MKTSEG like 'IA - Individual' or MKTSEG is null)
        and (MPI like 'N' or MPI is null)
        and [Item Category] like '%Term'
        and [Month] >= '2015-01-01 00:00:00.000'