4

I am trying to execute insert query using Apache meta model on a sql server database - Where insert query contains a column name with forward slash(/) in it such as 'col4a/col4b' and query will be created by metamodel as

INSERT INTO dbo."table1" (col1,"col2 Type",col3,col4a/col4b) VALUES ('value1','value2','value3','value4')

When I execute this statement the code throws an error incorrect syntax near '/'.

Can anyone suggest me a solution to escape special character like / in my column name.

1 Answer 1

4

The proper way to handle poorly named columns in sql server is with square brackets [ ].

INSERT INTO dbo.table1 
(
    col1
    , [col2 Type]
    , col3
    , [col4a/col4b]
) 
VALUES 
(
    'value1'
    , 'value2'
    , 'value3'
    , 'value4'
)
4
  • 1
    SQL Standard double quotes usually also work with SQL Server (might require an option to be enabled). Apr 22, 2016 at 15:01
  • I have already tried it, it works fine in sql server ui but I can't implement via code because apache metamodel doesn't identify column and gives an error as [col2 Type] - no such column in table. Apr 25, 2016 at 4:37
  • Is it possible to rename your columns? Spaces and other characters really shouldn't be used in column names.
    – Sean Lange
    Apr 25, 2016 at 13:15
  • No because it will effect on other objects related to that column like(procedures, triggers, functions). Apr 27, 2016 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.