Interesting Observation – Declaring Table Variables impact on Tempdb

Following was an intresting observation when creating table variables.
1. Table variables are created in tempdb.
2. Table variables are created in tempdb even before the declare table is called.

Steps to find that the Table variables are created in tempdb:
1. Restart your local SQL Server instance. (So that the tempdb gets cleared)
2. Connect to the SQL Server in SQL Server management studio and check if there are any records in sys.objects in tempdb.
[sql]
SELECT * FROM tempdb.sys.tables
[/sql]
You will find there are 0 records(ideally)
3. In a new query window run the below script.
[sql]
USE MASTER
GO

DECLARE @TableVariable TABLE (ID INT,VALUE VARCHAR(100))

INSERT INTO @TableVariable
select 1,’A’

WAITFOR DELAY ’00:02:00′
[/sql]
4. While the above code is getting executed run the below script in other query window.
[sql]
SELECT * FROM tempdb.sys.tables
–Take objectid from the above resultset
SELECT * FROM tempdb.sys.columns where OBJECT_ID = 117575457
[/sql]

Result: It can be found that the tempdb.sys.tables and tempdb.sys.columns has records for the table variable. This proves that the table variable is created in tempdb.

Steps to find that the Table variables are created in tempdb even before the declare table is called:
1. Restart your local SQL Server instance. (So that the tempdb gets cleared)
2. Connect to the SQL Server in SQL Server management studio and check if there are any records in sys.objects in tempdb.
[sql]
SELECT * FROM tempdb.sys.tables
[/sql]
You will find there are 0 records(ideally)
3.In a new query window run the below script.

[sql]
SELECT * FROM tempdb.sys.tables

DECLARE @TableVariable TABLE (ID INT,VALUE VARCHAR(100))

INSERT INTO @TableVariable
select 1,’A’
[/sql]

Result: It can be found that there is a record in the tempdb.sys.tables even before the DECLARE Statement is executed. This proves that the table variables are created in tempdb even before the declare table is called.

Wishing all the blog readers advanced happy new year. đŸ™‚

Using @@LOCK_TIMEOUT

@@LOCK_TIMEOUT specifies the number of milliseconds a statement has to wait for the lock on the dependant objects to be released.

Syntax:
[sql]
SET @@LOCK_TIMEOUT [time_out_in_milli_seconds]
[/sql]

Example:
Below example sets the lock timeout to 2000 milli seconds(2 seconds).
[sql]
SET @@LOCK_TIMEOUT 2000
[/sql]

By default the initial value of @@LOCK_TIMEOUT is -1.

@@LOCK_TIMEOUT setting is not applicable to the following:
CREATE DATABASE Statement
ALTER DATABASE Statement
DROP DATABASE Statement

MSDN Link: http://msdn.microsoft.com/en-us/library/ms189470.aspx

Applicable to SQL Server 2000,SQL Server 2005,SQL Server 2008,SQL Server 2008 r2,SQL Server 2012

USING @@MAX_CONNECTIONS VARIABLE

@@MAX_CONNECTIONS Returns the maximum number of simultaneous user connections allowed on an instance of SQL Server. The number returned is not necessarily the number currently configured.

Syntax:


SELECT MAX_CONNECTIONS

Output for the Above query would give the following depending up on your server:

Return Type of @@MAX_CONNECTIONS is integer

USING @@CONNECTIONS VARIABLE

@@CONNECTIONS Variable contains the number of connections that were called on to the SQL Server Service.
This number includes both Successfull and unsuccessful connections.
Basic Syntax for using @@CONNECTIONS:


SELECT @@CONNECTIONS

 

Output for the Above query would give the following depending up on your server: