

Venkatesan Prabu MCITP,MCAD,MCTS,CCNA. Worked as a ProjectLead(Senior .Net developer,SQL DBA). Now, Managing Director of KAASHIV INFO TECH, Chennai This Blog aims in serving the community in a better way. This blog is read by developers in 159 countries with average of 400 hits per day. Please post your valuable suggestions and hold my hand to serve the community. Lets make a new world with good thoughts and good minds....... This blog serves the SQL server community all over the world.
Planning to buy a house in India :-( Check this URL,
http://venkattechnicalblog.blogspot.com/2008/10/social-article-buy-property-in-india.html
How we are going to stop this? Provide me more inputs on this topics?
Venkatesan Prabu .J
People who are involved / affected:
Increase in rents:
Reason for buying a house:
Reason for reducing land values in india :
My suggestions in buying a property in 2008:
Some URLs for your reference :
Chennai property bubble bursts, prices dip : http://www.ndtv.com/convergence/ndtv/story.aspx?id=NEWEN20080067248
Chennai properties will be reducing soon:
Buy a flat and get BMW free: (People should think about this offer. They will dream about the gift offered and forgot to think about the property price. The business people will increase the property rates like (Actual rate+BMW car rate) and they will sell their property easily.
http://indiarealestatemonitor.com/property-news/buy-flat-get-bmw-free/
Property market gone down due to US breakdown:
http://indiarealestatemonitor.com/property-news/indian-realty-wakes-up-to-the-us-meltdown/
Land value is now Rs. 15,000 per square feet against Rs. 40,000 for a similar property in 2007
Analysts predicts that, the land value will go further down to Rs.10,000 in next 6 months.
http://indiarealestatemonitor.com/property-news/affordable-housing-is-the-new-choice/
Realtors were slowly analysing the market and they are not ready to step ahead:
http://indiarealestatemonitor.com/property-news/realtors-advance-tax-payment-falls-down/
Feel free to post your comments and kindly share this URL to your friends and create an awareness among our people.
My article on increase in rates in India:
http://venkattechnicalblog.blogspot.com/2008/10/social-article-increase-in-price-in.html
Venkatesan Prabu .J
I have see lot of queries asking for down the strings from comma seperated string or semicolon seperated string. Below is a superb function to acheive the same.
CREATE FUNCTION dbo.fnSplit(
@INPUTSTRING VARCHAR(8000) -- Actual string with comma delimeted
, @Delimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))
BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@Delimiter,@INPUTSTRING,0) <> 0
BEGIN
SELECT
@sItem=RTRIM(LTRIM(SUBSTRING(@INPUTSTRING,1,CHARINDEX(@Delimiter,@INPUTSTRING,0)-1))),
@INPUTSTRING=RTRIM(LTRIM(SUBSTRING(@INPUTSTRING,CHARINDEX(@Delimiter,@INPUTSTRING,0)+LEN(@Delimiter),LEN(@INPUTSTRING))))
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END
IF LEN(@INPUTSTRING) > 0
INSERT INTO @List SELECT @INPUTSTRING -- Put the last item in
RETURN
END
GO
The above code with create a function which will split the string based on its input parameter.
Lets execute the below query,
select * from dbo.fnSplit('1,22,333,444,,5555,666', ',')
The above query will fetch the output,
item
-------------------------
1
22
333
444
5555
666
Happy Learning!!!
Regards,
Venkatesan Prabu .J
Local and Global Cursor :
While declaring the cursors, we can specify a cursor as Local or Global cursor.
Local Cursor will exists for the particular object and it cannot be reference by an external object. By default, cursors are Local. If we need to make it as global and can be accessed by all the objects. We need to specify it as global.
If the cursor is declared as Global, it can be accessed by all the objects in the existing connection.
Subquery:
Subquery is an important concept in T-SQL. The outer query will provide the result based on the result given by the subquery.
Select id from table1 where id = (select max(id) from table1) -Red color is the subquery.
The outer table's output "id" depends on the subquery "id" value.
Here equal to (=) is used so that the subquery will retrieve only one id. If the subquery gives more than one value we have to use "IN" operator.
Select id from table1 where id in (select id from table1)
generally, the outer query is waiting for the subquery to finish its execution. So, it will hit the performance in a huge application with complex joins. So, we should avoid subqueries.
Correlated subquery:
These queries are little bit interesting one. The inner query depends on the outer queries input and there will be a mutal co-ordination occurs between these inner and outer query.
select name from venkat1 v
where id in (select id from venkat1 v1 where v.id=v1.id)
In the above query, the inner query depends on the outer queries output.
Happy Learning!!!
Regards,
Venkatesan Prabu .J
I faced a small problem in using order by in views, Let me discuss the exact problem and hack to resolve this problem.
create table venkattable1(id int, val varchar(100))
insert into venkattable1(1,'Venkat')
insert into venkattable1(2,'Venkat')
insert into venkattable1(3,'Venkat')
create view venkatview
as
select * from venkattable1 order by id
I got the below error,
"The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified."
It shows that, we can't use order by clause on the view, I've tried with Top clause in the above statement,
create view venkatview
as
select top 10 * from venkattable1 order by id
It's working fine. Please check the bleow URL for more details related to this problem.
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3908414&SiteID=1
Now, clicking next you will get the destination. you can specify your server and database name.
Clicking next, you will get the next screen.
Right click the package and execute the tasks.
Happy Learning!!!
Regards,
Venkatesan Prabu .J
I got this error,
"The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."
Reason: Its because of the date format. I am trying to insert the date value into the month but, it wont accept beyond 12. Am i right :-)
I have changed the query. Now, its working fine.
select convert(datetime,convert(varchar(10),convert(varchar(10),[day])+'/'+convert(varchar(10),[month])+'/'+convert(varchar(10),[year])))from dbo.VenkatTableVenkatesan Prabu .J
Click apply, now you can achieve your tasks.
You can achieve it using T-SQL too.
Clearly you'll need sa privileges on your SQL instance to run this:
sp_configure 'show advanced options',
RECONFIGURE
GO
sp_configure 'xp_cmdshell',
GO
RECONFIGURE
GO
Regards,
Venkatesan Prabu .J
An interesting topic in SQL Server,
Is it possible to find the last restarted datatime of a server?
When my server is started is the question?
OOPS... where this data is stored. Frankly speaking even I dont know. But, I found a work around to achieve the same.
SQL Server is holding a database named 'tempdb' which gets flushed off during server down or server stop and created once sql server is restarted. So, we can fetch the relative data from tempdb's created date.
Below is the query to fetch this information,
SELECT create_date FROM sys.databases WHERE [name]='tempdb'
Happy Learning!!!
Regards,
Venkatesan Prabu .J
What do you mean by acid property?
Atomicity : The value should be atomic.
Consistency : The data in the database should be consistent. It indicates, if we are maintaining data in two different places. Those data should resemble. If it differs, then it will create confusions. Am I right?
Isolation : Transaction should be isolated and it should not affect the other transactions. If the transactions occured on the same data. Appropriate locks should be posted before starting the transactions.
Durability: The data should be stable.
Regards,
Venkatesan Prabu .J
T-SQL Interview Questions: (SQL Server Interview questions)
Considering am having two tables,
Considering am having two tables,
create table Employees(empid int, empname varchar(10),desig varchar(10),salary int, mgrid int)
insert into employees values(1,'aa','pm',10000,1)
insert into employees values(2,'bb','pm',10000,1)
insert into employees values(3,'cc','pl',500,2)
SELECT * FROM employees
SELECT * FROM employees
create table employeedetails (empid int, City varchar(10), Telephone int)
insert into employeedetails values(1,'sydney',10)
insert into employeedetails values(1,'sydney1',10)
SELECT * FROM employeedetails
The table structure should resemble as below,
Employees (empid, empname, Designation, salary, mgrid )
EmployeeDetails (empid, City, Telephone)
T-SQL Interview Questions: (SQL Interview questions)
Considering am having two tables,
create table Employees(empid int, empname varchar(10),desig varchar(10),salary int, mgrid int)
insert into employees values(1,'aa','pm',10,1)
insert into employees values(2,'aa','pm',10,1)
SELECT * FROM employees
create table employeedetails (empid int, City varchar(10), Telephone int)
insert into employeedetails values(1,'sydney',10)
insert into employeedetails values(1,'sydney1',10)
SELECT * FROM employeedetails
The structure of the table resembles,
1. Select all employee with Salary>2000
Select empid, empname, Designation, salary, mgrid FROM employees
WHERE Salary >2000
2. Select all employee with Designation "Project Manager"
Select empid, empname, Designation, salary, mgrid FROM employees
where Designation='Project Manager'
3. Select all employee who doen'thave Telephone
SELECT DISTINCT A.empid, A.empname, A.Designation, A.salary, A.mgrid, B.city, B.Telephone FROM
employees A INNER JOIN EmployeeDetails B
ON A.empid=B.empid WHERE B.Telephone IS NOT NULL AND B.Telephone <> ''
(OR)
SELECT empid, empname, Designation, salary, mgrid
FROM employee
WHERE (empid IN
(SELECT empid
FROM EmployeeDetails where Telephone IS NOT NULL AND Telephone <> ''
GROUP BY empid
4. Select all employee belong to the city 'Sydney'
SELECT DISTINCT A.empid, A.empname, A.Designation, A.salary, A.mgrid, B.city, B.Telephone FROM
employees A INNER JOIN EmployeeDetails B
ON A.empid=B.empid WHERE B.City='Sydney'
Stored procedure output into the table:
I have seen lots of lots people are querying about, projecting stored procedure output
into a table.
I will take an extended procedure to explain this concept,
exec xp_fixeddrives
The above stored procedure is used to fetch the amount of space available in our hard disk.
My scenario is used to fetch the output of this SP and populate the same in my table.
We used to try the below query to achieve the same,
SELECT * INTO temp_table
exec xp_fixeddrives
OOps, am getting an error,
An object or column name is missing or empty.
For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names.
Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name.
Whats the reason???. Its because, as per the syntax we have to specify the table and we can't specify the stored procedure or execute stored procedure in this context.
Lets see some work around for this problem,
In this case, we need to create a temp table or table and afterwards we have to do an insert statement.
CREATE TABLE temp_table (drive varchar(15),MBfree varchar(500))
INSERT INTO temp_table exec xp_fixeddrives
SELECT * FROM temp_table
Happy Learning!!!
Regards,
Venkatesan Prabu .J
Let's check with the new table created.
SP_HELP NewTable2
Happy Learning!!!
Regards,
Venkatesan Prabu .J
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 procedure
sp_helpdb - Database and its properties
sp_who2 – Gives details about the current user connected to your system.
sp_renamedb – Enable you to rename your database
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 Server
Clustered - 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.
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!!!
Regards,
Venkatesan Prabu .J
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.