Technical Tips, Tricks and Articles

Archive for the ‘databases’ Category

Avoiding cursors in store procedures

leave a comment »

I recently has joined a new company in which i have been assigned the task of converting Ms-SQL store procedures to MySql Store Procedures. There has been a number of things irritating in MySQL out of those one is that, you only have on SQLSTATE handler for cursor.

If you have nested cursors then you can’t loop thoroughly. At least that was the case with me. So the problem was that how can i avoid creating nested cursors?

I searched the net and came up with a nice technique.

Suppose, here was the thing that i was using in my cursor and wanted to convert it in non cursor technique.


Then we make a HANDLER and loop through it.

Now to remove this cursor i made a Temporaray Table, Inserted all the values that i am getting through the above query and then traverse it … 🙂

`rowId` int(11) NOT NULL auto_increment,
`col1` varchar(8000) default NULL,
`col2` bigint default NULL,
`col3` int(11) default NULL,
`col4` varchar(8000) default NULL,
`col5` varchar (2000) default NULL,
INSERT INTO tempTable (col1, col2, col3, col4, col5) (
select * from someTable

The actuall thing was pretty complex, i am posting a dumy query just to explain. Now all the data that has been collected using select statement will be inserted in this temporary Table.

Now you have to apply your own logic to traverse through it. Here is mine.

First get the total rows inserted in the temporary column.

SELECT COUNT(*) AS RowCount INTO TotalTableRows FROM tempTable;

You remember that i have used a rowID as primary key and it is autoincremented, this will help us a lot. We will get the min of rowID and increment it, then we will get the record for that incremented value, if the record exist, we will use our logic to utilise the record and increment in the tempRowCount. This loop will continue until we have tempRowCount <= TotalTableRows.

SELECT MIN(rowId) INTO tempRowId FROM tempTable;
WHILE (tempRowCount < TotalTableRows) DO
SELECT col1 INTO tempCol1 FROM tempTable where rowId = tempRowId;
/*Explamation Reference 2*/
IF ISNULL(col1) = 0 THEN
SET tempRowCount = tempRowCount + 1;
-- your business logic
SET tempRowId = tempRowId + 1;

Now you may be wandering what i am doing with ISNULL IF. Well, we are selecting the table with a query having a where clause at RowID (which is autoincremented). Suppose, we don’t have a value against the value 1 but have a value against value 2. In the first case, there will be not data will be selected so the ISNULL IF condition will not execute. I then incremented in temoRowID to make it 2. The select statement execute with some data and the IF ISNULL condition will eventually become true causing it to execute the block.

I may not be good in explanation. But I have tried to give you a good idea.

I’ll try to re-write this topic with more elaboration. Right now, it is enough to give an idea. Thanks.


Written by ..alee

February 11, 2007 at 4:08 pm

Posted in databases, MySql, sql

Tagged with , ,

Some Databases

leave a comment »

  • How do you implement one-to-one, one-to-many and many-to-many relationships while designing tables?
    • One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships.
    • One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships.
    • Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table.

It will be a good idea to read up a database designing fundamentals text book.

  • What’s the difference between a primary key and a unique key?
    Both primary key and unique key enforces uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a non clustered index by default. Another major difference is that, primary key doesn’t allow NULLs, but unique key allows one NULL only.
  • Define candidate key, alternate key, composite key.
    • A candidate key is one that can identify each row of a table uniquely. Generally a candidate key becomes the primary key of the table. If the table has more than one candidate key, one of them will become the primary key, and the rest are called alternate keys.
    • A key formed by combining at least two or more columns is called composite key.
  • What is a transaction and what are ACID properties?
    A transaction is a logical unit of work in which, all the steps must be performed or none. ACID stands for Atomicity, Consistency, Isolation, Durability. These are the properties of a transaction.
  • What are defaults? Is there a column to which a default can’t be bound?
    A default is a value that will be used by a column, if no value is supplied to that column while inserting data. IDENTITY columns and timestamp columns can’t have defaults bound to them.
  • What’s the difference between DELETE TABLE and TRUNCATE TABLE commands?
    DELETE TABLE is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow. TRUNCATE TABLE also deletes all the rows in a table, but it won’t log the deletion of each row, instead it logs the deallocation of the data pages of the table, which makes it faster. Of course, TRUNCATE TABLE can be rolled back.
  • What is RAID and what are different types of RAID configurations?
    RAID stands for Redundant Array of Inexpensive Disks, used to provide fault tolerance to database servers. There are six RAID levels 0 through 5 offering different levels of performance, fault tolerance. MSDN has some information about RAID levels and for detailed information, check out the RAID advisory board’s homepage.
  • What is blocking and how would you troubleshoot it?
    Blocking happens when one connection from an application holds a lock and a second connection requires a conflicting lock type. This forces the second connection to wait, blocked on the first.
  • What is an extended stored procedure? Can you instantiate a COM object by using T-SQL?
    An extended stored procedure is a function within a DLL (written in a programming language like C, C++ using Open Data Services (ODS) API) that can be called from T-SQL,just the way we call normal stored procedures using the EXEC statement. Yes, you can instantiate.

Written by ..alee

December 13, 2006 at 3:38 pm

Posted in databases, interview, sql

Tagged with , , ,