Incorrect syntax near '2290' - sql-server

Query:
INSERT INTO 2290_Form(FK_C_B_key,is_final_return,is_amendment,first_used_month,tax_year)
VALUES ('CF3B908B-8120-493B-993A-B899A61AE77B',0,0,4,2014)
Error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '2290'.
How do I fix this error?

You cannot have a table with a name that starts with a number. However, you can do this by enclosing the table name in []. So in this instance, your query should be:
INSERT INTO [2290_Form] (FK_C_B_key,is_final_return,is_amendment,first_used_month,tax_year)
VALUES ('CF3B908B-8120-493B-993A-B899A61AE77B',0,0,4,2014)
As per Rules for Regular Identifiers:
Rules for Regular Identifiers
The first character must be one of the following:
A letter as defined by
the Unicode Standard 2.0. The Unicode definition of letters includes
Latin characters from a through z and from A through Z, in addition to
letter characters from other languages.
The underscore (_), "at" sign (#), or number sign (#). Certain symbols
at the beginning of an identifier have special meaning in SQL Server.
An identifier beginning with the "at" sign denotes a local variable or
parameter. An identifier beginning with a number sign denotes a
temporary table or procedure. An identifier beginning with double
number signs (##) denotes a global temporary object.
Some Transact-SQL
functions have names that start with double at signs (##). To avoid
confusion with these functions, it is recommended that you do not use
names that start with ##.
Subsequent characters can be:
Letters as defined in the Unicode Standard 2.0.
Decimal numbers from either Basic Latin or other national scripts.
The "at" sign, dollar sign ($), number sign, or underscore.
The identifier must not be a Transact-SQL reserved word. SQL Server
reserves both the uppercase and lowercase versions of reserved words.
Embedded spaces or special characters are not allowed.
When used in Transact-SQL statements, identifiers that fail to comply with these rules must be delimited by double quotation marks or brackets.
Emphasis is mine.

Because your table name starts with a number you have to use [] arround your table name:
INSERT INTO [2290_Form](FK_C_B_key,is_final_return,is_amendment,first_used_month,tax_year) VALUES ('CF3B908B-8120-493B-993A-B899A61AE77B',0,0,4,2014

Related

Are user-defined identifiers beginning with a single underscore non-problematic?

Is this identifier non-problematic:
_var
C11, 7.1.3 Reserved identifiers, 1
All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
Does it follow from this that user-defined identifiers beginning with a single underscore are non-problematic?
Yes. As long as:
are at block scope (includes enum/struct/union tags)
OR are struct/union members
OR are function parameters
_ is followed by neither capital nor another underscore
E.g.
struct X { int _a; };
int main() { int _a; }
void foo(int _a);
No, there is a problem.
You forgot to include one more quote
All identifiers that begin with an underscore and either an uppercase
letter or another underscore are always reserved for any use.
So you may not declare an identifier beginning with one underscore followed by an uppercase letter.
In general it is a bad style of programming using identifiers starting with underscore because the reader of the code can think that this identifier is reserved by the implementation.
Does it follow from this that user-defined identifiers beginning with a single underscore are non-problematic?
No, that list item merely tells you certain things are problematic. It makes no statement that other things are non-problematic.
The same paragraph tells you that all identifiers listed in the header subclauses are reserved if header that declares them is included, possibly for any use, so they are problematic. There are additional issues listed in that paragraph.
C 2018 6.4.2.1 5 and 6 tell you that identifiers longer than the minimums listed in 5.2.4.1 (63 characters for internal identifiers, 31 for external) may be a problem; the behavior is not defined if two identifiers differ only beyond the limit of significant characters the implementation imposes.
C 2018 6.4.2 also allows identifiers with implementation-defined characters, so such identifiers may work in some implementations and not others.
Inside the main function or user defined function, you can write like that
You won't get any compile error.
int main()
{
int _var;
float _var1;
}

Can you quote variable identifiers and parameter identifiers in TSQL?

Is it possible to quote variable and parameter identifiers in TSQL. The documentation appears to suggest that it is possible. However an error is thrown when executing variable names in quotes.
-- This works fine, as one would expect
DECLARE #ExampleVariableGood bigint
-- Error - "can" variable names be quoted identifiers?
DECLARE [#ExampleVariableBad] bigint
When executing script the following error is thrown:
Msg 155, Level 15, State 2, Line 5
'bigint' is not a recognized CURSOR option.
The documentation states, variable identifiers must follow the same rules as for all identifiers:
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-2017
This is an odd request, the only reason we need this to work is a SQL Studio tool auto formatting bug incorrectly quotes variable names, something the client does not want to change!
Any quotes are taken from Database Identifiers
Variables follow the rules for "regular identifiers". These are classed as:
Regular identifiers
Comply with the rules for the format of identifiers. Regular identifiers are not delimited when they are used in Transact-SQL statements.
An object that is quoted (wraped in brackets ([])), is not a regular identifier but a delimited identifier:
Delimited identifiers
Are enclosed in double quotation marks (") or brackets ([ ]). Identifiers that comply with the rules for the format of identifiers might not be delimited. For example:
The fact that a variable follows the rules for a regular Identifier can be found in the Rules for Regular Identifiers section:
The names of variables, functions, and stored procedures must comply
with the following rules for Transact-SQL identifiers.
The first character must be one of the following:
A letter as defined by the Unicode Standard 3.2. The Unicode definition of letters includes Latin characters from a through z, from
A through Z, and also letter characters from other languages.
The underscore (_), at sign (#), or number sign (#).
Certain symbols at the beginning of an identifier have special meaning in SQL Server. A regular identifier that starts with the at
sign always denotes a local variable or parameter and cannot be used
as the name of any other type of object. An identifier that starts
with a number sign denotes a temporary table or procedure. An
identifier that starts with double number signs (##) denotes a global
temporary object. Although the number sign or double number sign
characters can be used to begin the names of other types of objects,
we do not recommend this practice.
Some Transact-SQL functions have names that start with double at signs (##). To avoid confusion with these functions, you should not
use names that start with ##.
Therefore, as a variable's name must be a regular identifier, the delimited identifier [#ExampleVariable] is not a valid name.
Although, oddly, you can make an SP/function that has a delimited identifier, which infers the documentation is out of date.

Database name restrictions

What I wonder what are the database, table, and column naming restrictions in databases? (MySQL, MSSQL, Oracle, etc.)
To give an example:
database_name
columnName
TableName
column_1
table.name
we can use the symbol "_" and also there no problem with numbers, etc.
but we can not use "." when we are naming.
The rules for the format of regular identifiers depend on the database compatibility level. This level can be set by using ALTER DATABASE . When the compatibility level is 100 , the following rules apply:
The first character must be one of the following:
A letter as defined by the Unicode Standard 3.2. The Unicode definition of letters includes Latin characters from a through z, from A through Z, and also letter characters from other languages.
The underscore (_), at sign (#), or number sign (#).
Certain symbols at the beginning of an identifier have special meaning in SQL Server. A regular identifier that starts with the at sign always denotes a local variable or parameter and cannot be used as the name of any other type of object. An identifier that starts with a number sign denotes a temporary table or procedure. An identifier that starts with double number signs (##) denotes a global temporary object. Although the number sign or double number sign characters can be used to begin the names of other types of objects, we do not recommend this practice.
Some Transact-SQL functions have names that start with double at signs (##). To avoid confusion with these functions, you should not use names that start with ##.
Subsequent characters can include the following:
Letters as defined in the Unicode Standard 3.2.
Decimal numbers from either Basic Latin or other national scripts.
The at sign, dollar sign ($), number sign, or underscore.
The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words.
Embedded spaces or special characters are not allowed.
Supplementary characters are not allowed.
When identifiers are used in Transact-SQL statements, the identifiers that do not comply with these rules must be delimited by double quotation marks or brackets.
Please read this article about DataBase Naming Conventions:
https://launchbylunch.com/posts/2014/Feb/16/sql-naming-conventions/

Matching words in ANSI C

How can I match a word (1-n characters) in ANSI C? (in addition: What is the pattern to match a constant in C-sourcecode?)
I tried reading the file and passing it to regexec() (regex.h).
Problem: The tool I'm writing should be able to read sourcecode and find
all used constants (#define) to check if they're defined.
The pattern used for testing is: [a-zA-Z_0-9]{1,}. But this would match words such as the "h" in "test.h".
Identifiers must start with a letter or underscore, so the pattern is
[A-Za-z_][A-Za-z0-9_]*
I know of no syntactic difference between C and preprocessor identifiers. There is a convention to use upper case for preprocessor and lowercase for C identifiers, but no actual requirement. Unless defines are guaranteed to use a distinct naming convention you would basically have to find every identifier in the source file and any included files and sort them into preprocessor identifiers, C identifiers and undeclared identifiers.
From the GCC manual:
Preprocessing tokens fall into five broad classes: identifiers, preprocessing numbers, string literals, punctuators, and other. An identifier is the same as an identifier in C: any sequence of letters, digits, or underscores, which begins with a letter or underscore. Keywords of C have no significance to the preprocessor; they are ordinary identifiers. You can define a macro whose name is a keyword, for instance. The only identifier which can be considered a preprocessing keyword is defined.
Another option besides doing regex searches over C source code would be to use a preprocessor library like Boost Wave or perhaps something like Coan instead of starting from scratch.
Here is the Lexer grammar and the Parser grammar (in flex and bison format, respectively) for the entire c language. In particular, the part relevant to identifiers is:
D [0-9]
L [a-zA-Z_]
{L}({L}|{D})* { count(); return(check_type()); }
So the id can start with any uppercase or lowercase letter or an underscore, and then have more uppercase or lowercase letters, underscores, and numbers. I believe it doesn't match parts of file names because they're quoted and it handles quotes separately.

Which ASCII characters are forbidden for use in SGML attributes?

Apart from whitespace, quotation mark, equal sign, and tab, which other characters of the printable subset of ASCII are forbidden to be used as attribute names in SGML?
By default, SGML allows only alphanumeric values for SGML names. What additional characters are allowed for SGML names is controlled by the SGML declaration; specifically UCNMCHAR and LCNMCHAR under NAMING.
For example, if you look at the SGML declaration for HTML 4, you'll see:
LCNMCHAR ".-_:"
UCNMCHAR ".-_:"
This means that the characters ., -, _, and : are also allowed in SGML names (element/attribute/entity/etc).
NOTE: Only a letter is allowed as the first character of an SGML name.

Resources