SQL Synatx error while creating table dynamically

  • 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

    Regards,

    Vipin jha

  • Got the answer

    issue with the exec

    correct one is Exec (@sSQL)

  • +1

    Thanks to have provided the solution .

  • CELKO (4/21/2015)


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

    Please, please read any book on data modeling. A schema is supposed to be a model of a "universe of discourse" (remember that from your first course in Logic?). Each table is either a set of entities or relationships among entities. Each table has to have a key by definition.

    But you live in a magical Universe where you can create things from nothing! Elephants drop out of the sky for you! What you are doing is re-inventing magnetic tape files in SQL. In the 1960's, we used hang a fresh new tape on a drive and give it a label based on the current date! WOW! Just what you are doing now!

    Then to make things worse, you used IDENTITY. This is a physical count of the physical insertions to this one table on this one machine. It is how you can fake a 1960's tape file record number.

    Oh, “getdate()” is an old Sybase/UNIX construct that was replaced a few years ago with the ANSI/ISO Standard CURRENT_TIMESTAMP today. You did the wrong and did it badly. You need to stop programming until you know the most basic RDBMS concepts; you are dangerous to any employer.

    I am going to paraphrase this statement you made:

    You need to stop programming until you know the most basic RDBMS concepts; you are dangerous to any employer.

    Here it is:

    You need to stop trying to help less experienced individuals until you learn to be a real mentor; you are dangerous to any newbie.

  • Hi Lynn ,

    +1 for your reply to the last post of Celko .

    It is not the 1st time that Celko was writing posts which are showing how he is respecting other posters. Now , I am applying what a friend moderator on other forums ( MSDN ) told me when I was complaining about the way he was replying : ignore him.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply