sql stored procedure when using declare to declare variables

The problem I did find the answer to the second page from Baidu, the answer from others on their own careful study to understand, by the way finishing over.
Original link: https: //zhidao.baidu.com/question/245490659799230284.html


If the stored procedure is seen as a batch statements like to understand more


The stored procedure is just a name with a batch of SQL statements, if a variable is needed in the whole process can be declared, but the variable declaration can only survive in a batch operation (stored procedures), the run is finished after disappears , the format of this statement is: declare the type of a variable name


Wherein the variables are distinguished from each other and called the column names in the database, so the variable name has a front @ sign, for example: declare @count int


@Count is to declare a variable of type int, then you can use a variable @count as an integer variable. Here there may be some misunderstanding, some people think @count is a variable name, also suggested that the count is a variable name, but only a symbol @, in fact, no matter what kind of understanding is without prejudice to the variable declaration is, in fact, it should be @count is called a variable name is more appropriate.


Just said, after the variable declaration only survive in a batch or stored procedure is running it , that is a local variable , in fact, there is a SQL variables are global variables , they start with @@ , but the current SQL also do not allow their customers to declare global variables, only present in the system . For example, the query system version, you can use select @@ version way to view. Because it can not declare, so the situation does not declare a use.


In fact, the variable in the SQL interpretation of the word is very deep, there is a class variable is the name of the table names, column names, stored procedures, etc., these names actually be global variables database , as long as the table exists, then the table name will a table exists in the master database, the column name is the same, this type of variable has a special, all existing object (the object) , so they do not need to be declared, and so was its declaration when building a database or table then you can use a variable in this case in front of the @ symbol is not prompt.


Therefore, under normal circumstances, we can only say @ front of a variable, no front for the database-related variables, and system variables @@ is not required to declare. But as long as when using @ custom variables, you must declare the election, while others do not need .

Guess you like

Origin www.cnblogs.com/donelyorjune/p/11104981.html