开发者

Advanced database modification "script" - how to do it

开发者 https://www.devze.com 2023-01-29 11:56 出处:网络
I have to go through a database and开发者_运维知识库 modify itaccording to a logic. The problem looks something like this. I have a history table in my database and I have to modify.

I have to go through a database and开发者_运维知识库 modify it according to a logic. The problem looks something like this. I have a history table in my database and I have to modify. Before modifying anything I have to look at whether an object (which has several rows in the history table) had a certain state, say 4 or 9. If it had state 4 or 9 then I have to check the rows between the currently found row and the next state 4 or 9 row. If such a row (between those states) has a specific value in a specific column then I do something in the next row. I hope this is simple enough to give you an idea. I have to do this check for all the objects. Keep in mind that any object can be modified anywhere in its life cycle (of course until it reaches a final state).

I am using a SQL Sever 2005 and Hibernate. AFAIK I can not do such a complicated check in Transact SQL! So what would you recommend for me to do? So far I have been thinking on doing it as JUnit test. This would have the advantage of having Hibernate to help me do the modifications and I would have Java for lists and other data structures I might need and don't exist in SQL. If I am doing it as a JUnit test I am not loosing my mapping files! I am curious what approaches would you use?


I think you should be able to use cursors to manage the complicated checks in SQL Server. You didn't mention how frequently you need to do this, but if this is a one-time thing, you can either do it in Java or SQL Server, depending on your comfort level.

If this check needs to be applied on every CRUD operation, perhaps database trigger is the way to go. If the logic may change frequently over the time, I would much rather writing the checks in Hibernate assuming no one will hit the database directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号