Impact of TEMPDB Collations on Other Databases

When you create databases with different collations then you have to be very careful when using the temporary tables.

In the below script let us analyze such a scenario.

Creating Sample database with case sensitive collation
[sql]
CREATE DATABASE TESTCOLLATION
COLLATE SQL_Latin1_General_CP1_CS_AS ;
GO
[/sql]

Let us create a sample table in a database and insert data.
[sql]
USE TESTCOLLATION
GO

CREATE TABLE TestTable ( NAME VARCHAR(100))
GO

INSERT INTO TestTable
SELECT ‘TestName’

GO

[/sql]

In the below queries let us query the table.
[sql]
USE TESTCOLLATION
GO
SELECT * FROM TestTable

SELECT * FROM TestTable where NAME = ‘TestName’

SELECT * FROM TestTable where NAME = ‘testname’

GO
[/sql]

output:
[text]
NAME
—————————————————————————————————-
TestName

(1 row(s) affected)

NAME
—————————————————————————————————-
TestName

(1 row(s) affected)

NAME
—————————————————————————————————-

(0 row(s) affected)
[/text]

It can be found that the third select statement above does not return any rows as the database and the columns in the table are with case sensitive collation.

Now let us create a temp table with same data as above and check the results.

[sql]
USE TESTCOLLATION
GO

CREATE TABLE #TestTable ( NAME VARCHAR(100))
GO

INSERT INTO #TestTable
SELECT ‘TestName’

GO

SELECT * FROM #TestTable

SELECT * FROM #TestTable where NAME = ‘TestName’

SELECT * FROM #TestTable where NAME = ‘testname’
[/sql]

Output:
[text]

(1 row(s) affected)
NAME
—————————————————————————————————-
TestName

(1 row(s) affected)

NAME
—————————————————————————————————-
TestName

(1 row(s) affected)

NAME
—————————————————————————————————-
TestName

(1 row(s) affected)

[/text]

It can be found that the third select statement above gives ‘TestName’ as output.

So when we go with an assumption that the temporary tables have the same collation as the database in which it is created then the code would fail.

Resolution:
[sql]
SELECT * FROM #TestTable WHERE NAME collate SQL_Latin1_General_CP1_CS_AS = ‘TestName’

SELECT * FROM #TestTable WHERE NAME collate SQL_Latin1_General_CP1_CS_AS = ‘testname’
[/sql]

Output:
[text]
NAME
—————————————————————————————————-
TestName

(1 row(s) affected)

NAME
—————————————————————————————————-

(0 row(s) affected)

[/text]

Cleanup:
[sql]
DROP DATABASE TESTCOLLATION
GO
[/sql]

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. 🙂

sql server business intelligence interview questions and answers

Following are the links where you can get material regarding sql server business intelligence (bi) interview questions and answers:

Data Warehousing Interview Questions and Answers Complete List Download

SQL Server Reporting Services FAQ

Analysis Services interview questions

Data Warehousing Interview Questions

Data Warehouse Concepts Interview Questions

Business Intelligence interview questions

SSRS Interview questions and answers

SSIS interview questions and answers

Download SSRS Interview Questions and Answers PDF

SQL Server Reporting Services Interview Questions with Answers

Business Intelligence interview questions

SSIS Interview Questions

SSIS Interview Questions – Part 1

SQL Server BI DEV – DBA Q and A

Data warehousing interview questions and answers

bi faqs

MS SQL Server interview questions

Download pdf file using the below link:
SQL Server 2008 Interview Questions Answers pdf

How to Call a stored procedure from other procedure

Following are the very basic questions on stored procedures.

Question: How can i call a stored procedure from another stored procedure?

Answer:
Use EXEC statement.
Following syntax to execute a stored procedure:

[sql]
EXEC STOREDPROCEDURENAME
[/sql]
Replace STOREDPROCEDURENAME with the name of the procedure.

You can add this piece of code in another stored procedure where ever required.

If the procedure has parameters, then below is the syntax.
[sql]
EXEC STOREDPROCEDURENAME @parameter1=value1,@parameter2=value2,..
[/sql]
Replace STOREDPROCEDURENAME with the name of the procedure, parameters with the name of parameters and values with values for the parameters.
If you have one procedure in schema1 which should call another procedure in schema2.

Following is the code that creates two stored procedures. One of the procedure calls the other procedure using EXEC.

[sql]
CREATE PROCEDURE Procedure1 –Creating Procedure1
AS
SELECT ‘1’
GO

CREATE PROCEDURE Procedure2 –Creating Procedure2 that calls Procedure1
AS
EXEC Procedure1 –CALLING PROCEDURE Procedure1
GO

EXEC Procedure2 –Executing Procedure2
[/sql]

Question: How can i call a stored procedure from another stored procedure, both of them are from different schemas?

Answer:
USE EXEC Schemaname.procedurename

[sql]
EXEC SchemaName.ProcedureName
[/sql]

Following is the simulation and solution for this question.

[sql]
CREATE SCHEMA Schema1 –Creating Schema1

GO

CREATE SCHEMA Schema2 –Creating Schema2
GO

CREATE PROCEDURE Schema1.Procedure1 –Creating Procedure1 in Schema1
AS
SELECT ‘1’
GO

–Creating Procedure2 in Schema2 to call Procedure1 in Schema1
CREATE PROCEDURE Schema2.Procedure2
AS
EXEC Schema1.Procedure1 –CALLING SCHEMA1 PROCEDURE FROM SCHEMA2 PROCEDURE
GO

–Executing Procedure2 in Schema2
EXEC Schema2.Procedure2
[/sql]

Now if you execute:

[sql]
EXEC Schema2.Procedure2
[/sql]

You can see that it is calling Schema1.Procedure1 and displaying 1 as the result.