For what reason doesn’t SQL allow a variable to be assigned a value using syntax @i =100;? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing 开发者_运维问答this post.
Closed 7 years ago.
Improve this questionUnlike 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;
I believe they did this deliberately just to annoy you.
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.
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.
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++.
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.
精彩评论