Unlike programming languages, SQL doesn’t allow variable to be assigned a value using the following syntax ( instead we must use SET
or SELECT
):
@i=100; //error
Is there particular reason why SQL doesn’t allow this?
thank you
Unlike programming languages, SQL doesn’t allow variable to be assigned a value using the following syntax ( instead we must use SET
or SELECT
):
@i=100; //error
Is there particular reason why SQL doesn’t allow this?
thank you
Why does any language use the syntax it uses?
To maintain conventions, prevent ambiguity, and simplify parsing.
Notice that the syntax of SET
statements (properly called assignment statements) is similar to the syntax in the SET
clause of an UPDATE
statement:
UPDATE mytable SET somecolumn = 100;
Well, SQL isn't a programming language.
I can't say that I know the reason. However, it likely has something to do with the intended use of SQL. Namely, working with data. Who works with a lot of data? Business people. The language, as such, uses more words and fewer symbols. Compare the style of VisualBasic to C++.
You may as well say why can't I do this in c#?
SELECT * FROM MyArray[]
Why must I iterate and mess around with Console.WriteLine? It's just a set collection of data that I want to see in one go.
After a variable is declared, it is initialized to NULL. Use the SET statement to assign a value that is not NULL to a declared variable.
So says TSQL SET documentation. I don't remember having to use the SET keyword in Oracle's PLSQL.
Those variables are part of Transact-SQL, not the SQL standard.
SQL was not designed to write procedural code, so its support for anything other than set operations is largely bolted on in proprietary extensions.
why do your need a "@" at the beginning of a variable name? to help parse the commands
The SET
or SELECT
helps differentiate assignment from comparisons, it is just the way TSQL was designed.
use:
SET @i=100 --for single assignments
and
SELECT @i=100,@a=200,@c=300 --for multiple assignements, which is
--faster than the equivalent multiple SET commands
The first reason that comes to mind is that SQL uses the '='-sign for both comparing of values as setting of values. This means that it requires a different syntax to distinguish between comparing and setting of the values. In (some) other programming there is a distinction between setting and comparing the values with the use of the '='-sign and the '=='-sign. For example in SQL:
SET @a = 100 --To set the value of @a.
if (@a = 100) -- To compare the value of @a.
For example in C#:
a = 100; //To set the value of a.
if (a == 100) //To compare the value of a.