What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 37409 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > Global variables in Sql Server - Part1

Global variables in Sql Server - Part1

1 vote(s)
Rating: 5 out of 5
Article posted by Saratvaddilli on 11/2/2012 | Views: 2185 | Category: Sql Server | Level: Beginner | Points: 250 red flag


In this article we are going to learn the global variables in sql server 2008

Introduction


In SQL Server they are two types of variables local variables and global variables .

Local variables :

local variable is defined with a declare statement and assigned an initial value within the statement batch where it is declared with a select statement

Declaring a Local Variable:

Use the following syntax to declare a local variable's name and datatype:

declare @variable_name datatype 
[, @variable_name datatype]...

The variable name must be preceded by the @ sign and conform to the rules for identifiers. The datatype can be any datatype except text, image, or sysname.

Global variables:

Global variables are SQL Server-supplied variables that have system-supplied values.The global variables are reserved for system-wide usage. You cannot explicitly create or modify global variables - SQL Server maintains them implicitly.Predefined global variables are distinguished from local variables by having two @ signs preceding their names, for example, @@error@@rowcount. Users cannot create global variables, and cannot update the value of global variables directly in a select statement.

 List of first 10 Global Variables and their Usage are included below:

 1) @@CONNECTIONS    The number of logins or attempted logins since SQL Server was last started.      
Example:
select getdate() as today , @@connections as login_attempts

2)@@CPU_BUSY The Number of milliseconds CPU has spent Working since SQL Server was last started

Example:

select @@CPU_BUSY as cputime_utilized

3)@@CURSOR_ROWS            : Number of rows currently in the last opened cursor (for the current connection)

Example:

select @@CURSOR_ROWS  AS  ROWS_CURSOR

4)@@DATEFIRST : First day of the week. NOTE: unlike what you'd expect @@DATEFIRST returns 1 for Monday, 2 for Tuesday, etc. Default is 7(Sunday).

Example:

select  @@DATEFIRST  AS  FIRSTDAY  

5)@@DBTS : The current value of TIMESTAMP for the current database.

Example:

select @@DBTS AS TIMESTAMP_db

6)@@ERROR : The error number for the last T-SQL statement executed. If this value is zero than there        were no error. 

Example:

select @@ERROR AS ERROR_NO
7)@@FETCH_STATUS : Fetch status of the last FETCH statement of the connection, executed against any cursor opened on the current connection.

Example:
select @@FETCH_STATUS 
8)@@IDENTITY Returns the last IDENTITY value inserted. If there haven't been any IDENTITY values inserted than this variable is NULL.

Example:
select @@IDENTITY AS LAST_IDENTITYVALUE

9)@@IDLE : Number of milliseconds SQL Server has been idle since it was last started


Example:
select @@IDLE AS IDLE_MILLISECONDS

10)@@IO_BUSY : The amount of time, in ticks, that SQL Server has spent doing input and output operations    since it was last started. i.e Number of milliseconds SQL Server has spent performing Input and Output (IO) operations since it was last started.


Example:

	select @@IO_BUSY AS IOBUSY_MILLISECONDS

Conclusion

These are the first 10 global variables in the sql server 2008 , in the next article we will learn the next 10 global variables in the sql server 2008

click below link to go to global variables in Sql Server 2008 Part-2

http://www.dotnetfunda.com/articles/article2017-global-variables-in-sql-server-part2.aspx

Reference

http://manuals.sybase.com/onlinebooks/group-asarc/srg1100e/sqlref/@Generic__BookTextView/72481;pt=73386


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:0 year(s)
Home page:www.dotnetfunda.com
Member since:Monday, July 30, 2012
Level:Bronze
Status: [Member]
Biography:I am sarat vaddilli working on the cutting edge technologies like html5,css3, Jquery and MicroSoft technologies like Asp.Net,Asp.Net MVC, SQL Server.
 Responses
Posted by: Sheonarayan | Posted on: 03 Nov 2012 10:54:36 PM | Points: 25

Great effort dear,

Keep it up!


>> Write Response - Respond to this post and get points
Related Posts

This is part 21 of the series of article on SSIS. In this article we are going to see know about different FTP tasks operations available in SSIS.

In this post we will solve the problem of finding the "Factorial of number(s)" which appeared as beginners challenge.

In this article we will figure out as how to extract values from XML Attributes and present in comma separated values using XQuery

This article describes the features like create report , deploying on report server, access report from aspx page and create a chart graph based report. Code snippets are given wherever necessary.

This is part 31 of the series of article on SSIS. In this article we are going to see on how to use an Aggregate data flow transformation control in SSIS packaging.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 7:47:49 AM