• 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 8, 2010

DERIVED TABLE BASICS IN SQL SERVER 2005

08 Sunday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ Leave a comment


DERIVED TABLE BASICS IN SQL SERVER 2005

A derived table is one that is created runtime using the select statement and referenced by the outer select statement like a normal table or a view.Derived tables are not saved permanently. Derived tables exists in memory of the SQL SERVER and can only be referred by the outer select in which they are created.

Example of a simple derived table is given below.

Select * from employee ( select top 5 * from employee ) as DerivedTable.

The inner query select produces a derived table and replaces a regular table.Important thing to notice here is whenever you are using a derived tables is that you must always use an alias. If you remove the alias the system will give you an error like incorrect syntax near ‘)’

You can also use derived tables to implement the database paging.Example is given below.

Declare @StartRowNum INT , @EndRowNum INT

SET @StartRowNum = 100

SET @EndRowNum  = 120

SELECT * FROM

(

SELECT id, EmpName , Salary , Row_number() over ( order by select 2 ) AS RowNumber From Employee

)

Employee

Where RowNumber >= @StartRowNum AND RowNumber <= @EndRowNum

You can also achieve the above functionality with common types expressions.( CTE )

Pawan Kumar

Pawankkmr@hotmail.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

DELETE AND TRUNCATE DIFFERENCES

08 Sunday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ 1 Comment


DELETE AND TRUNCATE DIFFERENCES.

TRUNCATE Command
It will remove all the rows from the table and there will be no data in the table.
Truncate a table deallocates the extents and bulk sets 8 PFS bytes at a time.
Ghost cleanup is not involved in Truncate command as the rows are not deleted.
We cannot have where clause with truncate.
Truncate removed all rows but the table structure, columns, constraints, indexes and so on.
Truncate can be rolled back if used inside a transaction.
It is a DDL Command.
Truncate resets the identity of the table.
Truncate is faster and uses fewer system and transaction log resources than delete.
You cannot truncate table which is referenced by a foreign key constraint.
You cannot use TRUNCATE TABLE on tables that:-
a) Are referenced by a FOREIGN KEY constraint. (You can truncate a table that has a foreign key that references itself.)
b) Participate in an indexed view.
c) Are published by using transactional replication or merge replication.
Delete Command
Delete command removes the rows from a table based on the condition that we provide with a where clause.
Delete removes rows one at a time and record an entry in the transaction log ( .ldf file ) for each deleted row.
Delete can be rolled back.
It is DML command.
Delete does not resets the identity of the table.
Delete uses more resources than truncate.
Delete can be used with where clause or without where clause.

Thank You.

Pawan Khowal

Pawan is a SQL Server Developer. If you need any help in writing code/puzzle or training please email at – pawankkmr”AT”gmail.com. Meanwhile please go throgh the top pages from his blog.

Page Detail URL
☛ SQL Advance Puzzles https://msbiskills.com/tsql-puzzles-asked-in-interview-over-the-years/
☛ SQL Tricky Queries https://msbiskills.com/sql-puzzles-finding-outputs/
☛ SQL Server Perfomance tuning Articles https://msbiskills.com/sql-performance-tuning/
☛ SQL Server Articles https://msbiskills.com/t-sql/
☛ SQL Interview Questions & Answers https://msbiskills.com/sql-server-interview-questions/

My SQL Groups on Facebook:

1. If you like this post, you may want to join my SQL SERVER Interview Puzzles/Interview Questions on Facebook: https://www.facebook.com/groups/1430882120556342/

2. If you like this post, you may want to join my SQL Server Puzzles on Facebook:
https://www.facebook.com/groups/206594023090781/

My SQL Page on Facebook:

2. For all the updates you may follow my page -> https://www.facebook.com/MSBISkillscom-1602779883299222/

Enjoy !!! Keep Learning

Http://MsbiSkills.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

RECOMPILE OPTION IN STORED PROCEDURES IN SQL SERVER 2005

08 Sunday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ Leave a comment


RECOMPILE OPTION IN STORED PROCEDURES IN SQL SERVER 2005

Reasons when recompilation occurs are :-
Inserting column to a table or view.
Dropping column to a table or view.
Inserting constraints to a table.
Dropping constraints to a table.
Inserting an indexing to a table or view.
Inserting a trigger to a table.
Dropping a trigger to a table.

If the database is changed by any reason like adding new indexes or modifying data in the indexed columns , the original query plans should be optimized again by recompiling them.

This optimization happens automatically the first time a stored procedure is run after Microsoft SQL Server 2005 is restarted.It also occurs if an underlying table used by the stored procedure changes. But if a new index is added from which the stored procedure might benefit, optimization does not happen until the next time the stored procedure is run after Microsoft SQL Server is restarted.

In this situation, it can be useful to force the stored procedure to recompile the next time it executes.

Another reason to force a stored procedure to recompile is to counteract, when necessary, the "parameter sniffing" behavior of stored procedure compilation. When SQL Server executes stored procedures, any parameter values used by the procedure when it compiles are included as part of generating the query plan. If these values represent the typical ones with which the procedure is called subsequently, then the stored procedure benefits from the query plan each time it compiles and executes. If not, performance may suffer.

SQL Server provides three ways to force a stored procedure to recompile:

1 ) The sp_recompile system stored procedure forces a recompile of a stored procedure the next time it is run.

2 ) Creating a stored procedure that specifies the WITH RECOMPILE option in its definition indicates that SQL Server does not cache a plan for this stored procedure; the stored procedure is recompiled each time it is executed. Use the WITH RECOMPILE option when stored procedures take parameters whose values differ widely between executions of the stored procedure, resulting in different execution plans to be created each time. Use of this option is uncommon and causes the stored procedure to execute more slowly, because the stored procedure must be recompiled each time it is executed.
If you only want individual queries inside the stored procedure to be recompiled, rather than the entire stored procedure, specify the RECOMPILE query hint inside each query you want recompiled. This behavior mimics SQL Server’s statement-level recompilation behavior noted above, but in addition to using the stored procedure’s current parameter values, the RECOMPILE query hint also uses the values of any local variables inside the stored procedure when compiling the statement. Use this option when atypical or temporary values are used in only a subset of queries belonging to the stored procedure. For more information, see Query Hint (Transact-SQL).

3 ) You can force the stored procedure to be recompiled by specifying the WITH RECOMPILE option when you execute the stored procedure. Use this option only if the parameter you are supplying is atypical or if the data has significantly changed since the stored procedure was created.

Examples are given below for recompilation.

1. CREATE PROCEDURE EmployeeData

(

@EmpName varchar(50)

)
WITH RECOMPILE
AS
SELECT * FROM Employee WHERE EmployeeName = @EmpName

2. EXEC  dbo.EmployeeData  ‘Rahul’  WITH RECOMPILE

Pawan Kumar

Pawankkmr@hotmail.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

ADVANTAGES OF STORED PROCEDURES

08 Sunday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ Leave a comment


ADVANTAGES OF STORED PROCEDURES

1. They are PRECOMPILED which is executed as a unit.

2. They provide you faster execution.

3.You don’t have to recompile it again and again.

4.They are less prone to errors as they are written by experienced programmers.

5.Same business  logic can be used across multiple applications.

6.It reduces network congestion.

7.It provides better security.Only DBA owner has permission to modify and make changes to stored procedures. The logic of the stored procedures are also not revealed to others.

8. SP are compiled and their execution plan is cached in SQL SERVER memory and used again to when the same SP is executed again. This means when you execute the SP second time it will take less amount of time as compared to the time taken at the first time.

Other Notes:

1. You have to execute a stored procedure but you cannot select from a stored procedure.

2. Stored Procedures are logical components of SQL Server.

3.Stored Procedures by default does not return any value. They only return whether the Stored procedure has been successfully executed or not.

4.Stored procedures can have two types to parameters. Input and output parameters.

5.Stored Procedures also have recompile option.If we use this option then every time we execute a stored procedure so first it will first compile the stored procedure first then execute it. This is a bad practice to use this option.It is not recommended.

6.If you want to return some thing from the stored procedure then you have to use stored procedures with output parameters.

Pawan Kumar

Pawankkmr@hotmail.com

 

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

CODD’S RULE

08 Sunday Aug 2010

Posted by Pawan Kumar Khowal in SQL Concepts

≈ Leave a comment


There are 12 rules formulated by E.F Codd for RDBMS in 1970.If a RDBMS satisfies all these 12 rules then full benefits of the relational database results can be obtained.

The 12 rules are having the following main points.

1.Information Representative : In a relational database model , all information should be explicitly and logically represented by entering the data values in the form of tables.The information such as  view and column names should be in the table form.Data stored in data dictionary should be in the tabular form.

2.Guaranteed Access: Every value of data item must be logically addressable by using a combination of table-name , primary key and the column name.

3.Systematic treatment of NULL Values: In a relational database management null values should be supported for the representation of missing and inapplicable information only.The DBMS must have a consistent method for representing null values. Eg- NULL values for character data for numeric it must be different data from a strings of blanks , distinct from zero or any other non numeric values.

4.Database Description Rule : The description of a database is stored and maintained in the form of tables. This allows the users with appropriate authority to query information using the same language. This implies that a data dictionary should be present within the RDBMS that is constructed out of tables and/or views that can be examined using SQL.

5.Comprehensive data sub language : The RDBMS must be completely manageable through its own extension of SQL. The SQL should support DD , Views , DM , integrity , constraints and transactions boundary.

6.View Updating Rule: All the views thats are theoretically update able can updated through the system.

7.High level update , Insert , Delete :- An RDBMS must do more than just be able retrieve relational data sets. It must also be possible to insert , update and delete data items from relational set.

8.Physical Data Independence : Changes made to physical storage or access methods do not require changes to be made to the application programs used to manipulate data in tables.

9.Logical Data Independence : Application program should not be affected by the changes made to the base tables. Changes made to the table should not require changes to be made to application programs , operating on the table.

10.Integrity Independence : ( Reliable , Correct , Consistent ) means integrity hence for that we require constraints. The database language must be capable of defining integrity rules.The integrity constraints must be store-able in the catalog and not in the application programs.

11.The Distribution Rule : The application programs and ad hoc requests are logically unaffected when data is first distributed or when it is redistributed.Database systems built on the relational framework are also well suited for todays client server database design.

12. Non Sub-Version : It must not be possible to by pass the integrity rules defined through the database language by using the lower level language.

Pawan Kumar

Pawankkmr@hotmail.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email
← Older posts

Blog Stats

  • 1,084,784 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