locked
SQL Synatx error while creating table dynamically RRS feed

  • Question

  • I am getting error when I am trying to create table on runtime

    Declare @FileName varchar(100)
    Declare @File varchar(100)
    set @FileName='brkrte_121227102828'
    SET @File = SUBSTRING(@FileName,1,CHARINDEX('_',@FileName)-1)
    --=select @File
    Declare @table_name varchar(100)
    Declare @ssql varchar(1000)
    SET @table_name = 'DataStaging.dbo.Staging_'+ @File

    SET @sSQL = 'CREATE TABLE ' + @table_name + ' ( ' +
    ' [COL001] VARCHAR (4000) NOT NULL, ' +
    ' [Id] Int Identity(1,1), ' +
    ' [LoadDate] datetime default getdate() ' +

    ' )' 
    Exec @sSQL 

    Error massage:-
    Msg 203, Level 16, State 2, Line 16
    The name 'CREATE TABLE DataStaging.dbo.Staging_brkrte ( [COL001] VARCHAR (4000) NOT NULL, [Id] Int Identity(1,1), [LoadDate] datetime default getdate() )' is not a valid identifier.

    Please help me to resolve above error

    Thankx & regards, Vipin jha MCP

    Monday, April 20, 2015 5:34 AM

Answers

  • Got the answer

    issue with the Exec (@sSQL) 

    thanks


    Thankx & regards, Vipin jha MCP

    Monday, April 20, 2015 5:37 AM