• Home
  • SQL Server
    • Articles
    • T-SQL Puzzles
    • Output Puzzles
    • Interview Questions
    • Performance Tuning
    • SQL SERVER On Linux
    • Resources
  • SSRS
    • SSRS Articles
    • Interview Questions
  • SSAS
    • SSAS Articles
    • DAX
  • SQL Puzzles
  • Interview Questions
    • SQL Interview Questions
    • Data Interview Questions
  • Python Interview Puzzles
  • New Features(SQL SERVER)
    • SQL SERVER 2017
    • SQL SERVER 2016
    • SQL SERVER On Linux
  • Social
    • Expert Exchange
      • Top Expert in SQL
      • Yearly Award
      • Certifications
      • Achievement List
      • Top Expert of the Week
    • HackerRank (SQL)
    • StackOverflow
    • About Me
      • Contact Me
      • Blog Rules

Improving my SQL BI Skills

Improving my SQL BI Skills

Daily Archives: August 13, 2010

ANSI_NULLS ON / OFF IN SQL SERVER 2005

13 Friday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ Leave a comment


SET ANSI_NULLS ON / OFF – ?

Notes:-

1.They are used in objects like stored procedures and functions.

2.This is a setting for ANSI NULL comparisions.

3.If the option is ON , any statement that compares a identifier with a null returns a value 0.

4.IF the option is OFF , any statement that compares a identifier with a null returns a NULL means nothing.

5.You can set the option like SET ANSI_NULLS ON GO at the begining of the stored procedure.

Below example will explain the things in detail

–Create a table
CREATE TABLE TESTANSINNULL
(
id int identity(1,1) primary key,
[name] varchar(100) NULL
)

–Insert some values in the table
INSERT INTO TESTANSINNULL VALUES ( NULL )
INSERT INTO TESTANSINNULL VALUES ( NULL )
INSERT INTO TESTANSINNULL VALUES ( NULL )
INSERT INTO TESTANSINNULL VALUES ( NULL )
INSERT INTO TESTANSINNULL VALUES ( NULL )
INSERT INTO TESTANSINNULL VALUES ( ‘Pawan’ )

SELECT * from TESTANSINNULL — Check the table values

OUTPUT
_________
id name
———– ———-
1 NULL
2 NULL
3 NULL
4 NULL
5 NULL
6 Pawan

(6 row(s) affected)

1.
FIRST MAKE THE OPTION OFF
SET ANSI_NULLS OFF
SELECT * FROM TESTANSINNULL where name = null

OUTPUT — This has return all the rows in which the name is null
________

id name
———– ——–
1 NULL
2 NULL
3 NULL
4 NULL
5 NULL

(5 row(s) affected)

2.
FIRST MAKE THE OPTION ON
SET ANSI_NULLS ON
SELECT * FROM TESTANSINNULL where name = null

OUTPUT — This has return 0 rows.
________

id name
———– ——

(0 row(s) affected)

USE OF IS NULL

If you use IS NULL then the queries will return all the rows in which name is null.
In this case ON or OFF doesnt matter . Try out the below examples.

3.
SET ANSI_NULLS ON
SELECT * FROM TESTANSINNULL where name IS NULL

OUTPUT — This has return all the rows in which the name is null
________

id name
———– ——–
1 NULL
2 NULL
3 NULL
4 NULL
5 NULL

(5 row(s) affected)

4.
SET ANSI_NULLS OFF
SELECT * FROM TESTANSINNULL where name IS NULL

OUTPUT — This has return all the rows in which the name is null
________

id name
———– ——–
1 NULL
2 NULL
3 NULL
4 NULL
5 NULL

(5 row(s) affected)

PLEASE NOTE :

1.ANSI_NULLS affect the result of equal to (=)
2.ANSI_NULLS does not affect the result of ISNULL in case of NULL comparisions.

Hence always use ISNULL for checking NULL values.

Pawan Kumar
Pawankkmr@hotmail.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

INFORMATION SCHEMA VIEW HELP IN SQL SERVER 2005

13 Friday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ Leave a comment


INFORMATION SCHEMA VIEW HELP IN SQL SERVER 2005

INFORMATION_SCHEMA views are SQL-92 ANSI standards which conforms views with database metadata.

We can use SELECT statements to get their contents.

select * from Information_Schema.Check_Constraints

select * from Information_Schema.Column_Domain_Usage

select * from Information_Schema.Column_Privileges

select * from Information_Schema.Columns

select * from Information_Schema.Constraint_Column_Usage

select * from Information_Schema.Constraint_Table_Usage

select * from Information_Schema.Domain_Constraints

select * from Information_Schema.Domains

select * from Information_Schema.Key_Column_Usage

select * from Information_Schema.Parameters

select * from Information_Schema.Referential_Constraints

select * from Information_Schema.Routine_Columns

select * from Information_Schema.Routines

select * from Information_Schema.Schemata

select * from Information_Schema.Table_Constraints

select * from Information_Schema.Table_Privileges

select * from Information_Schema.Tables

select * from Information_Schema.Views

select * from Information_Schema.View_Column_Usage

select * from Information_Schema.View_Table_Usage

I got this information from an excellent blogger.Please spread the knowledge.

Pawan Kumar
Pawankkmr@hotmail.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

SYSTEM TABLES HELP IN SQL SERVER 2005

13 Friday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ Leave a comment


SYSTEM TABLES HELP IN SQL SERVER 2005

1.
SELECT * FROM sys.objects  
Info : Contains a row for each user-defined, schema-scoped object that is created within a database.
sys.objects does not show DDL triggers, because they are not schema-scoped.

2.
select * from sysobjects
SELECT * FROM sys.sysobjects      
Info: Contains one row for each object that is created within a database, such as a constraint, default, log, rule, and stored procedure.    
 
3.
SELECT * FROM sys.all_columns
Info : Shows the union of all columns belonging to user-defined objects and system objects. 

4.
SELECT * FROM sys.all_objects  
Info : Shows the UNION of all schema-scoped user-defined objects and system objects. 
 
5.
SELECT * FROM sys.all_parameters
Info : Shows the union of all parameters that belong to user-defined or system objects.

6.
SELECT * FROM sys.all_sql_modules
Info : Returns the union of sys.sql_modules and sys.system_sql_modules.

7.
SELECT * FROM sys.all_views
Info : Shows the UNION of all user-defined and system views.

8.Select * from sys.triggers
Info : All triggers, both DML and DDL, are found in sys.triggers. sys.triggers supports a mixture of name-scoping rules for the various kinds of triggers. 

9.
Select * from sys.types
Info : Contains a row for each system and user-defined type.

10.select * from sys.xml_schema_collections
Info : Returns a row per XML schema collection. An XML schema collection is a named set of XSD definitions.
The XML schema collection itself is contained in a relational schema, and it is identified by a schema-scoped Transact-SQL name.
The following tuples are unique: xml_collection_id, and schema_id and name.

Please go through MSDN for detailed explainations.

Pawan Kumar
pawankkmr@hotmail.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

LIST OF THINGS IN SQL SERVER 2005 — Very interesting.

13 Friday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ Leave a comment


LIST OF THINGS IN SQL SERVER 2005 — Very interesting.

FIND PRIMARY KEYS OF ALL THE TABLES IN A DATABASE.
____________________________________________________

SELECT SCHEMA_NAME(schema_id) as ‘Schema Name’,OBJECT_NAME(parent_object_id) AS ‘Table Name’,OBJECT_NAME(object_id) AS ‘Primary Key Name’, create_date , modify_date
FROM   sys.objects
WHERE  type_desc IN (‘PRIMARY_KEY_CONSTRAINT’) and type = ‘PK’

FIND LIST OF FORIEGN KEYS IN A DATABASE
____________________________________________________

SELECT SCHEMA_NAME(schema_id) as ‘Schema Name’,OBJECT_NAME(object_id) AS ‘Table Name’, create_date , modify_date
FROM   sys.objects
WHERE  type_desc IN (‘FOREIGN_KEY_CONSTRAINT’) and type = ‘F’

FIND LIST OF STORED PROCEDURES IN A DATABASE
____________________________________________________

SELECT SCHEMA_NAME(schema_id) as ‘Schema Name’,OBJECT_NAME(object_id) AS ‘Stored Procedure Name’ , create_date , modify_date
FROM   sys.objects
WHERE  type_desc IN (‘SQL_STORED_PROCEDURE’) and type = ‘P’

FIND LIST OF USER TABLES IN A DATABASE
____________________________________________________

SELECT SCHEMA_NAME(schema_id) as ‘Schema Name’,OBJECT_NAME(object_id) AS ‘Table Name’, create_date , modify_date
FROM   sys.objects
WHERE  type_desc IN (‘USER_TABLE’) and type = ‘U’

CHECK out the below table for details.

SELECT * from sys.objects

Pawan Kumar
Pawankkmr@hotmail.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

OBJECT ID OF AN OBJECT IN SQL SERVER 2005

13 Friday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ Leave a comment


OBJECT ID OF AN OBJECT IN SQL SERVER 2005

We can use a function called Object_Id to find out the object id of the object.

Syntax is OBJECT_ID (‘ObjectName’)

The returns the database object identification number of a schema-scoped object.

Examples are given below.

1.SELECT OBJECT_ID ( ‘NewEmployee’ ) as ‘Object Identification Number’

OUTPUT
_________

Object Identification Number

749961748

2.It will return if the object is not there in the database.

SELECT OBJECT_ID ( ‘Employee’ ) as ‘Object Identification Number’

OUTPUT
_________

Object Identification Number

NULL — Returned NULL because the object new employee doesnot exists in the database.

3.One more way is there which is given below.

Select id as ‘Object Identification Number’ , [name] as ‘Object Name’ from sysobjects where name = ‘NewEmployee’

OUTPUT
_________

Object Identification Number Object Name
—————————- ————-
749961748 NewEmployee

Please let me know if you have any other way.

Pawan Kumar
Pawankkmr@hotmail.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

Blog Stats

  • 1,085,224 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 1,131 other subscribers

Pawan Khowal

502 SQL Puzzles with answers

Achievement - 500 PuzzlesJuly 18, 2018
The big day is here. Finally presented 500+ puzzles for SQL community.

200 SQL Server Puzzle with Answers

The Big DayAugust 19, 2016
The big day is here. Completed 200 SQL Puzzles today

Archives

August 2010
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
« Jul   Sep »

Top Articles

  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…

Archives

  • October 2020 (29)
  • September 2018 (2)
  • August 2018 (6)
  • July 2018 (25)
  • June 2018 (22)
  • May 2018 (24)
  • April 2018 (33)
  • March 2018 (35)
  • February 2018 (53)
  • January 2018 (48)
  • December 2017 (32)
  • November 2017 (2)
  • October 2017 (20)
  • August 2017 (8)
  • June 2017 (2)
  • March 2017 (1)
  • February 2017 (18)
  • January 2017 (2)
  • December 2016 (5)
  • November 2016 (23)
  • October 2016 (2)
  • September 2016 (14)
  • August 2016 (6)
  • July 2016 (22)
  • June 2016 (27)
  • May 2016 (15)
  • April 2016 (7)
  • March 2016 (5)
  • February 2016 (7)
  • December 2015 (4)
  • October 2015 (23)
  • September 2015 (31)
  • August 2015 (14)
  • July 2015 (16)
  • June 2015 (29)
  • May 2015 (25)
  • April 2015 (44)
  • March 2015 (47)
  • November 2012 (1)
  • July 2012 (8)
  • September 2010 (26)
  • August 2010 (125)
  • July 2010 (2)

Article Categories

  • Analysis Services (6)
    • DAX (6)
  • Data (2)
    • Data warehousing (2)
  • Integration Services (2)
  • Magazines (3)
  • Python (29)
  • Reporting Services (4)
  • SQL SERVER (820)
    • Download SQL Interview Q's (212)
    • SQL Concepts (323)
    • SQL Performance Tuning (155)
    • SQL Puzzles (331)
    • SQL SERVER 2017 Linux (6)
    • SQL Server Interview Questions (308)
    • SQL SERVER Puzzles (332)
    • T SQL Puzzles (547)
    • Tricky SQL Queries (439)
  • UI (30)
    • ASP.NET (5)
    • C# (13)
    • CSS (9)
    • OOPS (3)
  • Uncategorized (5)

Recent Posts

  • Python | The Print and Divide Puzzle October 30, 2020
  • Python | Count consecutive 1’s from a list of 0’s and 1’s October 30, 2020
  • Python | How to convert a number into a list of its digits October 26, 2020
  • Python | Validate an IP Address-IPV6(Internet Protocol version 6) October 26, 2020
  • Python | Print the first non-recurring element in a list October 26, 2020
  • Python | Print the most recurring element in a list October 26, 2020
  • Python | Find the cumulative sum of elements in a list October 26, 2020
  • Python | Check a character is present in a string or not October 26, 2020
  • Python | Check whether a string is palindrome or not October 26, 2020
  • Python | Find the missing number in the array of Ints October 26, 2020
  • Python | How would you delete duplicates in a list October 26, 2020
  • Python | Check whether an array is Monotonic or not October 26, 2020
  • Python | Check whether a number is prime or not October 26, 2020
  • Python | Print list of prime numbers up to a number October 26, 2020
  • Python | Print elements from odd positions in a list October 26, 2020
  • Python | Print positions of a string present in another string October 26, 2020
  • Python | How to sort an array in ascending order October 26, 2020
  • Python | How to reverse an array October 26, 2020
  • Python | Find un-common words from two strings October 26, 2020
  • Python | How to convert a string to a list October 26, 2020
  • Python | Find unique words from a string October 26, 2020
  • Python | Calculate average word length from a string October 26, 2020
  • Python | Find common words from two strings October 26, 2020
  • Python | Find the number of times a substring present in a string October 26, 2020
  • Python | Find maximum value from a list October 26, 2020
  • Python | How to find GCF of two numbers October 26, 2020
  • Python | How to find LCM of two numbers October 26, 2020
  • Python | How to convert a list to a string October 26, 2020
  • Python | Replace NONE by its previous NON None value October 26, 2020
  • Microsoft SQL Server 2019 | Features added to SQL Server on Linux September 26, 2018

Create a website or blog at WordPress.com

  • Follow Following
    • Improving my SQL BI Skills
    • Join 231 other followers
    • Already have a WordPress.com account? Log in now.
    • Improving my SQL BI Skills
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar