Archive

Archive for August, 2016

SQL Trivia – Multiple ways to get list of all tables in a database

August 22, 2016 Leave a comment

 
This is one of a typical Interview Question you might face while going for an opening of a SQL Developer. This looks very simple question but its also an important question to judge a person if he/she knows SQL basics and has really worked in SQL Server or not.

–> So, here are following ways you can query the SQL Server metadata and system tables to get list of all tables in a particular database, by using:

1. sys.tables

select * from sys.tables

2. sys.objects with filter of type = ‘U’

select * from sys.objects where type = 'U'

3. sys.sysobjects with filter of type = ‘U’

select * from sys.sysobjects where type = 'U'

4. INFORMATION_SCHEMA.TABLES

select * from INFORMATION_SCHEMA.TABLES where TABLE_TYPE = 'TABLE'

5. sp_msforeachtable undocumented function

exec sp_msforeachtable 'print ''?'''

 

Out of all these the best option is to use the 4th one, i.e. INFORMATION_SCHEMA.TABLES views, as Microsoft itself suggest to use it instead of first three & the 5th option.


Advertisement
Categories: SQL Trivia

SQL Error – SHOWPLAN permission denied in database

August 10, 2016 Leave a comment

 

So, you are working on a SQL Query Performance issue in your Production environment, but you just have read access and can just execute SELECT statements to check the data.

But when you try to check what execution plan that query is creating/using you are getting blocked as you don’t have sufficient privileges for the same, and getting this error:

Msg 262, Level 14, State 4, Line 283
SHOWPLAN permission denied in database ‘AdventureWorks2014’.

 

For checking Execution plans you don’t need to have admin privileges, just granting access on SHOWPLAN setting will work for you.

So, you can your DBA to grant you access by using below query:

USE AdventureWorks2014
GO

GRANT SHOWPLAN TO user_name -- replace user_name here
GO

Categories: SQL Errors Tags: , ,

SQL Trivia – What all schemas cannot be dropped in a SQL Server database?

August 4, 2016 Leave a comment

 
–> The following four built-in database schemas cannot be dropped:
 

1. The “dbo” schema: is the default database schema for new objects created by users having the db_owner or db_ddl_admin roles. The dbo schema is owned by the dbo user account. By default, users created with the CREATE USER Transact-SQL command have dbo as their default schema.

2. The “guest” schema: is used to contain objects that would be available to the guest user. This schema is rarely used.

3. The “sys” schema: is reserved by SQL Server for system objects such as system tables and views.

4. The “INFORMATION_SCHEMA” schema: is used by the Information Schema views, which provide ANSI standard access to metadata. This schema is contained in each database. Each information schema view contains metadata for all data objects stored in that particular database. Information schema views are based on sys catalog view definitions.
 

… notes from: Training Kit (Exam 70-461) Querying Microsoft SQL Server 2012


Categories: SQL Trivia Tags: