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.

Geetanjali Jain
  • 402
  • 6
  • 19

1 Answers1

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'
)
Sean Lange
  • 33,028
  • 3
  • 25
  • 40
  • 1
    SQL Standard double quotes usually also work with SQL Server (might require an option to be enabled). – Mark Rotteveel Apr 22 '16 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. – Geetanjali Jain Apr 25 '16 at 04:37
  • Is it possible to rename your columns? Spaces and other characters really shouldn't be used in column names. – Sean Lange Apr 25 '16 at 13:15
  • No because it will effect on other objects related to that column like(procedures, triggers, functions). – Geetanjali Jain Apr 27 '16 at 11:28