When we use @ and @@ in sql [Resolved]

Posted by Pkanwar under Sql Server on 9/13/2013 | Points: 10 | Views : 1068 | Status : [Member] | Replies : 3
When we use @ and @@ in sql.








Thanks




Responses

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Identifiers that begin with the@symbol are used as local variables. Note that the@symbol can appear only as the first character in an identifier.

example:
@Addresses :This is only a valid identifier for creating a variable.
Table@Address : Invalid identifier. The@symbol canonly be the first character in an identifier.

Identfier starting with @@ symbol is called as global variables... there are predefined global variables in sql sever such as @@error, @@trancount, @@rowcount etc


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
@ is used for local variable and table variables declarations..
whereas @@ is used for global variable declaration

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mahsa.Hassankashi on: 9/17/2013 [Member] Starter | Points: 25

Up
0
Down
Firstly whenever you want to declare or set a variable by yourself, I mean user define variable like below codes:

Declare @x datetime
set @x=GetDate()
print x

Secondly in SQL we have two shape to show functions
1. Function Name() like GetDate() --> Give us current time
App_name --> give us current application
all of user define functions
2. @@Function Name (without parentheses)
@@servername
@@version
@@error which is used for TSql instruction and check codes before itself and if it has error it is @@error<> 0 , otherwise @@error=0

@@fetch_status which is checked if there is any record in cursor to read it or not and return integer number it is used with while loop to make decision to read record until there is anything to read.

To wrap up whenever something is defining by user it is with @
when something is reserved by system it has @@

Best Regards
Mahsa

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response