accessing a table where the column name is a reserved keyword - sql-server

I am setting up Castle Active Record to access a legacy database on an Microsoft SQL Server. The table in question has a column named function. When I attampt to load a record it gives me this error:
Incorrect syntax near the keyword 'function'
This error comes about because the SQL query nhibernate generates attempts to access the column named function, which happens to be a restricted keyword. The fix is to wrap the column name in square braces ([function]) in the SQL statement. Is it possible to tell nhibernate to do this in its autogenerated SQL?

This will help you:
http://quomon.com/question-How-do-I-handle-database-reserved-words-in-NHibernate-1148.aspx
Example where table is a reserved keyword:
<class name="User" table="[user]" lazy="false">
I think the same applies for column names. Wrap them in square braces.

Related

Incorrect syntax near 'Case'. Expecting ID, QUOTED_ID, or '.' Error in SQL Server

I multiple tables in my database. All of my tables are showing output using select query instead of only 1 table "case". it also have data and columns but when I use it in my query it shows syntax error. I have also attached picture which have list of table and a simple query. This code is not developed by me so I am not sure why it is showing error. Is there any kind of restriction we can set so that it cannot be used in queries?
CASE is a reserved keyword in SQL Server. Therefore, you must escape it in double brackets:
SELECT * FROM dbo.[Case];
But best naming practice dictates that we should avoid naming database objects using reserved keywords. So, don't name your tables CASE.
Reserved words are not recommended for use as a database, table, column, variable, or other object names. If you desire to use a reserved word is used as an object name in ANSI standard syntax, it must be enclosed in double-quotes OR "[]" to allow the Relational Engine (whichever that one is) that the word is being used as an object and not as a keyword in the given context. Here is the sample code.
SELECT * FROM dbo."Case"
Or
SELECT * FROM dbo.[Case]

Dynamic column name in snowflake

I am new to snowflake so please bear with me.
I am trying to do a very simple thing - specify a column name by literal but am getting sql compilation error
insert into MYDB.MYSCHEMA.MYTABLE (identifier('MYCOLUMN')) values (10);
SQL compiler points into unexpected parenthesis before MYCOLUMN. Skipping the word identifier and single qotes works fine.
Just got a response from Snowflake support. Currently "identifier" is only supported for select statements. It is not implemented for inserts for identifying columns. It does work for identifying tables n both select and insert.

How to create SqlDataConnection for db with table 'System'?

I want to create a SqlDataConnection type provider for a database with a table, which name is exactly 'System'.
During compilation I get an error:
The type provider 'Microsoft.FSharp.Data.TypeProviders.DesignTime.DataProviders' reported an error:
tmpB600.cs(12,7): warning CS0437: The type 'System' in 'c:\Users\krajewa1\AppData\Local\Temp\tmpB600.cs' conflicts with the imported namespace 'System' in 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Configuration.dll'. Using the type defined in 'c:\Users\krajewa1\AppData\Local\Temp\tmpB600.cs'.
[...] (hundreds of errors following)
To workaround it I've tried :
Using LocalSchemaFile with dbml file which does not contain 'System'
table. It did not help, still the same error.
Using ContextTypeName parameter (which I hoped would put types in another namespace). It did not help, still the same error.
Creating a synonym for System table. Of course original table was still there, so I got the same error.
Obviously, changing table name to '_System' does help, but is not feasible in practice in my case.
My problem is exactly the same as the one asked in this question:
SqlDataConnection type provider generates massive error with SQL Server 2008 R2.
However, in that thread users did not the find real cause of the problem and did not provide an answer.
I don't know F# but could you not create a SQL Server synonym for the problematic table name?
http://msdn.microsoft.com/en-gb/library/ms177544.aspx
CREATE SYNONYM [SystemTable] for [System]
And then refer to the table by the synonym instead of it's real name in your F# Code.
It might be possible to simply rename the table instead but if there are any other queries running against it they would need to be changed as well. The synonym means both names are valid

web2py, microsoft sql server and restricted names for fields

Is there any option to set the words "file", "key" and "trigger" as field names for ms-sql server?
We have pretty big application written with web2py with PostgreSQL as db, ans some of the fields has those names. One customer wishes to use ms-sql as db server, And I'm trying not to break compatibility within the DB structure.
Using square brackets (found in google) didn't help (could not use '[file]') - the ms-sql rejected it.
The documentation is pretty clear on this:
Although it is syntactically possible to use SQL Server reserved
keywords as identifiers and object names in Transact-SQL scripts, you
can do this only by using delimited identifiers.
The delimiters used in SQL Server are either double quotes or []. So, you can define them as:
[file]
or
"file"
Note that you need to use the delimiters wherever they appear.
The use of reserved words for such columns is discouraged. However, you might actually have a use case of compatibility between different databases where this capability will be useful.
I don't know why square bracket would fail. It works on SQL Fiddle.
I stumbled into this same issue when trying to use a legacy SQL Server table with fields that don't conform to identifier name rule, such as 'My File'.
Reading the source code I found that a rname field attribute exists for these cases:
Field('my_file', 'string', rname='[My File]'),
With this, Web2Py works with the my_file field name, the actual SQL/DML generated to interact with the database will use [My File] instead.

I have a problem creating stored procedure when I put a field with /

I have a field [Product/Services] in my table in sql server 2005. Now I want to create a stored procedure for that table, but it keep giving an error and when I put only Product in my table than stored procedure is working fine. Now I want to put [Product/Services] in my table so how can I do that?????
it is always a bad idea to try to include special characters in a column/variable/parameter/table/view/procedure/etc names. All of your code will have to dance around this bad decision forever.
Without any detail on your particular code and/or error message, all I can provide are these links on the the rules for naming things in SQL Server:
Identifiers
Delimited Identifiers
from the second link:
Microsoft SQL Server does not
recognize variable names and stored
procedure parameters that are
delimited. These types of identifiers
must comply with the rules for regular
identifiers.
Your best bet is to just name the column something like Product_Services or ProductServices and you can have local variables and parameters named #Product_Services or #ProductServices.
Your next best bet is to leave the table alone and just names the local variables as #Product_Services or #ProductServices even though the table column is named [Product/Services].
Make sure you are properly quoting the name Product/Services as [Product/Services] whenever you are referring to it or using it of redefining it. [] are the quote characters in MS SQL Server.

Resources