XSD in SQL Server

Recently, I have started reading Jacob's (Fellow SQL XML MVP) book and learnt lot of things on XSD and XML schema.

I wish to share the same to my blog readers with my short articles. Let us enter into the topic,

The abbreviation for XSD is XML Schema definition. This will define, how a XML should look like.
If we think about the XML (its worth to write about XML data type in SQL server. Let me do it in my next article) will be of two types,

1. Typed XML (XML bounded to XSD definition)
2. Untyped XML (XML is not bounded to any XSD definition).

As of now, We need to concentrate on typed XML. In this case,

1. A schema will be created - It will explain, how a XML should look like.
2. Create a XML which will abide the schema created already.

On looking into the structure of the shema,
1. It should be started with the XML Namespace.
2. Followed by the root elements.
3. The root element may have any number of child element.

Let's see a sample XSD,

The XMLName space should be inherited from the namespace

Here is the code to create XSD,

We can see how XML is created based on this shema in my future articles.

Thanks and Regards,

Venkatesan Prabu .J


Records in the first table based on the second table reference

Hi All,

Today, am trying to fetch a record from the first table and replace the values with the reference records in the second table. Let's make a try. There will be lot of ways to do this, am taking a legacy method to arrive the same. Let's see,

create table venkat(id int, id1 int, nam varchar(100))
insert into venkat values(1,2,'arun')
insert into venkat values(2,null,'arun1')
insert into venkat values(1,3,'arun2')
select * from venkat
create table venkat1(id int, val varchar(100))
insert into venkat1 values(1,'A')
insert into venkat1 values(2,'B')
insert into venkat1 values(3,'C')
select * from venkat1

select t1.val as id,t.val as id1,t.nam from
select b.val,a.nam from venkat a left outer join venkat1 b
on a.id1=b.id
)t inner join
select b.val,a.nam from venkat a left outer join venkat1 b
on a.id=b.id
on t.nam=t1.nam

Happy Learning!!!

Thanks and Regards,
Venkatesan Prabu .J


User defined function error in SQL Server

Today while trying to create a function, am trying to execute the function created. Am getting an error showing "'Venkat_fun2' is not a recognized built-in function name."

Strange behavior of SQL Server. I need to raise this issue with Microsoft techies. Let me check it and blog the same with the reason.

create function Venkat_fun2(@a int)
returns int
as begin
(select id from venkat where id=@a)

select Venkat_fun2(1)

Thanks and Regards,
Venkatesan Prabu .J

Interesting behaviour of SQL Server

Estimation plan in SQL Server :
I happened to do a check on the estimation plan for a very small query

Executed both the query,
Query 1:
select * from [venkat] order by 1 desc -- Query cost is 82%
This query have scanned a little bit on the table and took all the remaining cost for sorting.
Query 2:
select * from [venkat]order by -- Query cost is 18%
(select 1)
The above query has took 100% of its cost to scan the table. It doesn't spend any thing for sorting. Am confused with this behavior of sql server. But too interesting. I think we need to use second query for best performance (Providing, we need to index the table properly :-) )

Let me check with Microsoft team for a very better explanation on this article.

Thanks and Regards,
Venkatesan Prabu .J

Handling single quotes in SQL Server

In SQL Server, sometimes we will have single quotes in the values and sometimes we need to fetch the values from the tables. Seems to be interesting rights... Let see how can we achieve it,

Considering for example, I want to fetch the name Michael's. In that case, we can write

select * from table where name='michael's' -- More meaning ful

But, SQL Server won't recognise the above query and it will throw an error showing incorrect syntax because, SQL Server will recognise upto the letter "L" after that it won't recognise as a proper SQL Sentence.
select * from table where name='michael'

To over come this problem, SQL Server is providing a handy approach by putting two consecutive single quotes instead of one single quote. So the query should be,

select * from table where name='michael''s' --Query won't show any error and it will server the purpose.

Let see a small sample on this,

create table venkat(id int ,name varchar(100))
insert into venkat values(1,'venkat')
insert into venkat values(2,'venkat1')
insert into venkat values(3,'venkat2')
insert into venkat values(4,'venkat3')
insert into venkat values(5,'venkat4')
insert into venkat values(6,'venkat5')
insert into venkat values(6,'venkat''5')
select * from venkat

create procedure Venkat_sample
(@nam varchar(100))
select * from venkat where name=@nam
exec venkat_sample 'venkat''5'

Thanks and Regards,
Venkatesan Prabu .J

My T-SQL Gallery @code.msdn.microsoft

Created my own T-SQL Gallery in Microsoft site. Do visit the same and share your feedback,


Thanks and Regards,
Venkatesan Prabu .J

SQL Server Interview questions - Part 1

What is the significance of NULL value and why should we avoid permitting null values?
Null means no entry has been made. It implies that the value is either unknown or undefined.We should avoid permitting null values because Column with NULL values can't have PRIMARY KEY constraints. Certain calculations can be inaccurate if NULL columns are involved.

What is SQL whats its uses and its component ?
The Structured Query Language (SQL) is foundation for all relational database systems. Most of the large-scale databases use the SQL to define all user and administrator interactions. It enable us to retrieve the data from based on our exact requirement. We will be given a flexibility to store the data in our own format.

The DML component of SQL comprises four basic statements:
* SELECT to get rows from tables
* UPDATE to update the rows of tables
* DELETE to remove rows from tables
* INSERT to add new rows to tables

What is DTS in SQL Server ?
Data Transformation Services is used to transfer the data from one source to our required destination. Considering am having some data in sql server and I need to transfer the data to Excel destination. Its highly possible with dialogue based tool called Data Transformation services. More customization can be achieved using SSIS. A specialized tool used to do such migration works.

What is the difference between SQL and Pl/Sql ?

Straight forward. SQL is a single statement to finish up our work.Considering, I need some data from a particular table. “Select * from table” will fetch the necessary information. Where as I need to do some row by row processing. In that case, we need to go for Procedural Logic / SQL.

What is the significance of NULL value and why should we avoid permitting null values?
Null means no entry has been made. It implies that the value is either unknown or undefined.We should avoid permitting null values because Column with NULL values can't have PRIMARY KEY constraints. Certain calculations can be inaccurate if NULL columns are involved.

Difference between primary key and Unique key?
Both constraints will share a common property called uniqueness. The data in the column should be unique. The basic difference is,
· Primary key won’t allow null value. Whereas, unique key will accept null value but only one null value.
· On creating primary key, it will automatically format the data inturn creates clustered index on the table. Whereas, this characteristics is not associated with unique key.
· Only one primary key can be created for the table. Any number of Unique key can be created for the table.

Select Statement in SQL Server

Select Statement in SQL Server

String Functions in sql server

String Functions in sql server

SQL Server Interview Question - Part 2

What is normalization?

Normalization is the basic concept used in designing a database. Its nothing but, an advise given to the database to have minimal repetition of data, highly structured, highly secured, easy to retrieve. In high level definition, the Process of organizing data into tables is referred to as normalization.

What is a stored procedure:
Stored procedures are precompiled T-SQL statements combined to perform a single task of several tasks. Its basically like a Macro so when you invoke the Stored procedure, you actually run a set of statements. As, its precompiled statement, execution of Stored procedure is compatatively high when compared to an ordinary T-SQL statement.

What is the difference between UNION ALL Statement and UNION ?
The main difference between UNION ALL statement and UNION is UNION All statement is much faster than UNION,the reason behind this is that because UNION ALL statement does not look for duplicate rows, but on the other hand UNION statement does look for duplicate rows, whether or not they exist.

Example for Stored Procedure?
They are three kinds of stored procedures,1.System stored procedure – Start with sp_2. User defined stored procedure – SP created by the user.3. Extended stored procedure – SP used to invoke a process in the external systems.Example for system stored proceduresp_helpdb - Database and its propertiessp_who2 – Gives details about the current user connected to your system. sp_renamedb – Enable you to rename your database

What is a trigger?

Triggers are precompiled statements similar to Stored Procedure. It will automatically invoke for a particular operation. Triggers are basically used to implement business rules.

What is a view?
If we have several tables in a db and we want to view only specific columns from specific tables we can go for views. It would also suffice the needs of security some times allowing specfic users to see only specific columns based on the permission that we can configure on the view. Views also reduce the effort that is required for writing queries to access specific columns every time.

What is an Index?
When queries are run against a db, an index on that db basically helps in the way the data is sorted to process the query for faster and data retrievals are much faster when we have an index.

What are the types of indexes available with SQL Server?

There are basically two types of indexes that we use with the SQL ServerClustered -

1. It will format the entire table, inturn physically sort the table.

2. Only one clustered index can be created for a table.

3. Data will be located in the leaf level.

4. By default, primary key will create clustered index on the table.

Non-Clustered Index

1. It wont touch the structure of the table.

2. It forms an index table as reference to the exact data.

3. A reference to the data will be located in the leaf level.

4. For a table, we can create 249 non clustered index.

Happy Learning!!!
Venkatesan Prabu .J

SQL Interview question

Extent Vs Page?

Pages are low level unit to store the exact data in sql server. Basically, the data will be stored in the mdf, ldf, ndf files. Inturn, pages are logical units available in sql server.The size of the page is 8KB.

Eight consecutive pages will form an extent 8 * 8KB = 64KB.

Thus I/O level operation will be happening at pages level.The pages will hold a template information at the start of each page (header of the page).

They are,

1. page number,

2. page type,

3. the amount of free space on the page,

4. the allocation unit ID of the object that owns the page.

Extents will be classifed into two types,

1. Uniform extents

2. Mixed extents

Uniform Extents:It occupied or used by a single object. Inturn, a single object will hold the entire 8 pages.Mixed

Extents:Mulitple objects will use the same extent. SQL Server will allow a max of eight objects to use a shared extent.

Property of SQL Server :Initally if an object is created, sql server will allocate the object to the mixed extent and once if the size reaches 8 pages and more... immediately, a new uniform extent will be provided for that particular object.

Herecomes, our fragmentation and reindexing concepts.

Best Joke - Enjoy it

Best Joke - Enjoy it