Troubles on After Delete Trigger on SQL Server


So, last week, I stumbled upon an error in an application used frequently in the company I am working for. The problem occurred when executing a statement to delete data preventing redundancies when uploading a new batch of data. Sure, my team was appointed to fix the error since I am working as a support in the company.

When we were trying to debug, we got the error stating that the SQL Exception because the subquery returned more than one value. That was quite frustrating since the command we tried to execute was a simple delete command without using subquery. The frustating part was not that we did not understand the error, but because we did not have access to see what was inside the trigger.

So after we get the access to the trigger, we could understand that the subquery was to select data from DELETED temporary table. Unfortunately, the DELETED table result from query returns many rows and cause the error. That time I did not quite understand why. But after reading the MSDN entry about Using the Insterted and Deleted Tables, I understand the problem.

The statement to delete is

DELETE
FROM [dbo].[dailyUpload]
WHERE DATEDIFF(d, [uploadDate], GETDATE()) = 0

Meanwhile inside the trigger,

DELETE
FROM [dbo].[someAnother]
WHERE DATEDIFF(d, 
    [uploadDate], 
    (SELECT [uploadDate] 
        FROM DELETED)) = 0
    AND [IDDaily] = 
    (SELECT [ID] 
     FROM DELETED)

Surely, the subquery inside the trigger would return so many data since there were thousands of data uploaded to dailyUpload table.

So, I came up on a solution, storing the deleted value then delete it through looping in application side. Why not database side? Since the database was used by many users, it would not be fair to use the resource for some faulty of a user. Here is the code in C#.

// get the data
dailyUploadTableAdapter
    .FillByUploadDate(dailyUploadDataTable,
        DateTime.Now);
// delete data
/** OPTION A **/
foreach(var singleRow 
    in dailyUploadTableDataTable.Rows){
    dailyUploadTableAdapter
        .DeleteByUploadDate(singleRow.uploadDate);
    /** OPTION B will not need the line below **/
    someAnotherTableAdapter
        .DeleteByDUIDAndUploadDate(singleRow.ID,
            singleRow.uploadDate);
}

In the little example above, there are two alternatives. Alternative A will not need the trigger anymore. In my case, we cannot disable the trigger, that is why there is solution B. Option B will stick with the trigger rule.

So I think you can come up with either the solution. Anyway, if there is another way to process, please kindly share and we can talk it here!

Querying Table


Kalau emang belum tau ini apa SQL itu bahasa. Bahasa untuk melakukan operasi tertentu terhadap db.

Gimana aja sih? Yah itu banyak. sql juga punya standarnya untuk aplikasi berbeda. Misal standar Oracle bisa aja beda dengan standar SQL Server 2008.

Nah di sini, saya membahas SQL Server 2008. Yang saya bahas juga bukan sintaks DDL, DML, dll (dan lain lain maksudnya) 😀 aku bakal jelasin gimana caranya kita sebagai pengguna DB manipulasi tuh DB.

Kan gini, pastinya kita sebagai pengguna DB, punya kondisi akhir dan database berisi tabel yang banyak isinya. Nah, kan susah juga tuh manipulasi data jadi hasil akhir. Sebenarnya, kalo udah tau tricknya, baru kita bisa gampang.

Pertama, kita pikirin dlu struktur logika dari hasil. Kita pikirin row yang ditampilkan, datanya diambil dari tabel mana, dll. Nah, setelah siap kita memikirkan itu, maka kita akan menuliskan sintaks sql yang sesuai.

Ga harus langsung siap. Mulai aja dengan select * from [nama tabel]. Nantinya query ini berkembang kok :D. Siap itu kita mikirin, sintaks apa selanjutnya yang cocok, dan dengan apa menghubungkan sintaks tadi dengan sintaks baru ini?

Ya itu diajarinya di MVC. :p

So, think the logic and do the sql step by step, then, it is all yours. Good day everyone!

Posted with WordPress for BlackBerry.