开发者

Updating records from a XML

I need to provide 4 MySQL stored procedures for each table in a database. They are for get, update, insert and delete.

"Get", "delete" and "insert" are str开发者_开发百科aightforward. The problem is "update", because I don't know which parameters will be set and which ones not. Some parameters could be set to NULL, and other simply won't change so they won't be provided.

As I'm already working with XML, after several search in Google I've found that is possible to use a function called UpdateXML, but the examples are too complex and some articles are from 2007. So I don't know if there is a better technique at this moment or something easier.

Any comment, documentation, link, article or whatever of something that you've used and you're happy with, will be well appreciated :D

Cheers.


Usually when you have data from a row in your database in the front-end, you should have all of the values that you might use to update that row in the database. You should pass all of those values into your update, regardless of whether or not they have actually changed. Otherwise, your database doesn't really know whether it's getting a NULL value for a column because that's what it's supposed to be or because you just didn't pass the real value along.

If you are going to have areas of the application where you don't need certain columns from a table, then it's possible to set up additional stored procedures that do not use those columns. It's often easier though to just retrieve all of the columns from the database when you fill your front-end object. The overhead of the extra columns is usually minimal and worth the saved maintenance of multiple update stored procedures.

Here's an example. It's MS SQL Server syntax, so you may have to alter it slightly, but hopefully it illustrates the idea:

CREATE PROCEDURE Update_My_Table
    @my_table_id    INT,
    @name           VARCHAR(40),
    @description    VARCHAR(500),
    @some_other_col INT
AS
BEGIN
    UPDATE
        My_Table
    SET
        name           = @name,
        description    = @description,
        some_other_col = @some_other_col
    WHERE
        my_table_id = @my_table_id
END

CREATE PROCEDURE Update_My_Table_Limited
    @my_table_id    INT,
    @name           VARCHAR(40),
    @description    VARCHAR(500)
AS
BEGIN
    UPDATE
        My_Table
    SET
        name           = @name,
        description    = @description
    WHERE
        my_table_id = @my_table_id
END

As you can see, just eliminate those columns that you're not updating from the UPDATE statement. Just don't go overboard and try to have a stored procedure for every possible combination of columns that you might want to update. It's much easier to just get the extra columns from the DB when you select from the table in the first place. You'll end up passing the same value back and your server will wind up updating the column with the same exact value, but that's not a big deal. You can code your front end to make sure that at least one column has changed before it will actually try to update anything in the database.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜