SQL SERVER VARIABLES – SQLSERVERLEARNER https://sqlserverlearner.com LEARN SQL SERVER ONLINE Mon, 31 Dec 2012 12:39:34 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 Interesting Observation – Declaring Table Variables impact on Tempdb https://sqlserverlearner.com/2012/12/31/interesting-observation-declaring-table-variables-impact-on-temp-db/ https://sqlserverlearner.com/2012/12/31/interesting-observation-declaring-table-variables-impact-on-temp-db/#respond Mon, 31 Dec 2012 12:39:34 +0000 https://sqlserverlearner.azurewebsites.net/2012/12/31/interesting-observation-declaring-table-variables-impact-on-temp-db/ 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. 🙂

]]>
https://sqlserverlearner.com/2012/12/31/interesting-observation-declaring-table-variables-impact-on-temp-db/feed/ 0
Using @@LOCK_TIMEOUT https://sqlserverlearner.com/2012/05/16/using-lock_timeout/ https://sqlserverlearner.com/2012/05/16/using-lock_timeout/#respond Wed, 16 May 2012 07:12:55 +0000 https://sqlserverlearner.azurewebsites.net/2012/05/16/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

]]>
https://sqlserverlearner.com/2012/05/16/using-lock_timeout/feed/ 0
USING @@MAX_CONNECTIONS VARIABLE https://sqlserverlearner.com/2011/06/20/using-max_connections-variable/ https://sqlserverlearner.com/2011/06/20/using-max_connections-variable/#comments Mon, 20 Jun 2011 13:20:55 +0000 https://sqlserverlearner.azurewebsites.net/2011/06/20/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

]]>
https://sqlserverlearner.com/2011/06/20/using-max_connections-variable/feed/ 1
USING @@CONNECTIONS VARIABLE https://sqlserverlearner.com/2011/06/20/using-connections-variable/ https://sqlserverlearner.com/2011/06/20/using-connections-variable/#respond Mon, 20 Jun 2011 11:35:17 +0000 https://sqlserverlearner.azurewebsites.net/2011/06/20/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:

 

]]>
https://sqlserverlearner.com/2011/06/20/using-connections-variable/feed/ 0