我应该为每个单独更新的数据库表.列编写一个完整的过程吗?
我有一个广泛使用 AJAX 的应用程序。我有几个地方正在为用户正在积极编辑的记录更新单个数据库列。
到目前为止,我一直在为每个 AJAX 操作创建单独的存储过程...所以我有 UPDATE_NAME、UPDATE_ADDRESS、UPDATE_PHONE 存储过程。
我只是想知道是否有更好的方法来继续使用存储过程,但无需为每一列创建一个存储过程。
如果可能的话,我想避免反映指定列的字符串参数。即我知道我可以有一个 UPDATE_COLUMN 过程,它将列名称作为其参数之一。这让我心烦意乱,但如果这是唯一的方法,那么我可能会多考虑一下。但并非所有列都具有相同的数据类型,因此这似乎不是灵丹妙药。
I have an application that uses AJAX liberally. I have several places where a single database column is being updated for the record the user is actively editing.
So far I've been creating separate stored procedures for each AJAX action... so I've got UPDATE_NAME, UPDATE_ADDRESS, UPDATE_PHONE stored procedures.
I was just wondering if there's a better way to continue utilizing stored procedures, but without creating one for each column.
I'd like to avoid reflecting upon a string parameter which specifies the column, if possible. I.e. I know I could have an UPDATE_COLUMN procedure which takes as one of its parameters the column name. This kind of gives me the willies, but if that's the only way to do it then I may give it some more considering. But not all columns are of the same data type, so that doesn't seem like a silver bullet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
考虑编写一个接受多个列的更新过程,并对所有非强制列使用 DEFAULT NULL(如其他人建议的那样)。
在更新中使用
NVL
将仅更新您提供的列。这种方法的唯一问题是,您无法将值设置为NULL
。然后您可以使用命名参数来调用它:
这允许您在必要时一次更新多个列。
Consider writing a single update procedure that accepts several columns and uses
DEFAULT NULL
for all columns that are not mandatory (as suggested by others).Using
NVL
in the update will then only update the columns you provided. the only problem with this approach is, that you can't set a value toNULL
.You can call it with named parameters then:
This allows you to update several columns at once when necessary.
我想说的是,停止使用存储过程进行简单的活动,没有理由为数据库中的每一列创建如此多的小过程。为此,您最好使用动态 sql(带参数)。
I would say to stop using stored procedures for activities that simple, there is no justification to create so many small procedures for every single column in the database. You are much better off with dynamic sql (with parameters) for that.
创建一个可以更新每一列的过程,但仅更新您为其传递非空参数的列
请注意,如果您希望能够通过过程显式设置空值,则此方法不起作用,但您可以选择不同的值值来指定不改变(-1等),但稍微复杂一些。
Create a procedure that can update every column, but only updates columns for which you pass a non-null parameter
Note that this doesn't work if you want to be able to explicitly set null values through your procedure, but you could choose a different value to specify a non-change (-1, etc) with a little more complexity.
做你正在做的事情并没有坏处,但如果你继续这条路,可能会变得有点疯狂。您可以做的一件事是创建一个存储过程,并将 NULL 值作为默认参数分配给您要更新的所有字段。因此,当您从应用程序调用存储过程时,如果为参数指定了值,则该值将在更新中使用,否则该参数将采用空值。
然后,您可以在存储过程中进行检查
IF @Parameter IS NOT NULL ...
如果您发现自己只需要更新一个字段并且您不想创建一个中央存储过程并传递空值,然后使用我下面的 Octavia 解决方案并编写一个简单的更新过程。
It doesn't hurt to do what you are doing, but it could get a little crazy if you continue that path. One thing you can do is create one stored procedure and assign NULL values as default parameters to all your fields that you are updating. So when you call the sproc from your app, if a parameter is given a value that value will be used in the update, otherwise the parameter will take a null value.
Then you can do a check in the sproc
IF @Parameter IS NOT NULL ...
If you find yourself ever only needing to update just one field and you do not want to create one central sproc and pass nulls, then use Octavia's solution right below mine and write a simple update procedure.