更新 SQL Server
对于你们来说非常简单的查询,我刚刚对此感到陌生。这是场景 - 一张表有 10 个字段。在第一个 aspx 页面上,我插入了前 5 个字段(programid 是其中之一)。在第二个 aspx 页面上,我更新了在前一个屏幕中输入的相同记录(相同的程序 ID)以用于接下来的 5 个字段。但programid不是唯一的。一个programid可以有多个记录。我有一个唯一且身份增量的 ID 字段。
我需要可以获取最新 ID 和程序 ID 的查询,然后仅更新该记录而不是所有记录
,例如,
update table a
set field 6='abc',...
where programid = @programid AND
ID = (select id from tablea where programid = @programid order by id desc)
我希望您能明白,
提前致谢
pretty simple query for u guys, im just new to this. here's the scenario -
one table with 10 fields. On first aspx page, i do insert first 5 fields (programid is one of them). On the second aspx page, i update the same record entered in previous screen (same programid) for the next 5 fields. but the programid is not unique. one programid can have multiple records. I have an ID field which is unique and identity increment.
I need the query which can take the latest ID and the programid and then update only that record and not all records
e.g
update table a
set field 6='abc',...
where programid = @programid AND
ID = (select id from tablea where programid = @programid order by id desc)
I hope you get the idea
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以让第一个查询返回每个插入的 ID。
我通常通过在存储过程中放置一个名为@ID(或类似)的输出参数来实现此目的。这是基本轮廓:
在您的应用程序代码中,在调用插入过程后获取@id。然后您可以将其传递到第二个 aspx 页面以在更新语句中使用。如果 ID 为 -1,则表示插入失败。
您可以使用返回值来代替使用输出参数,尽管返回值通常用于报告成功或失败代码,并且以这种方式使用它们可能会被视为“hacky”。
请参阅Scope_Identity,这是一个非常有用的t-sql函数。
You can have your first query return the ID of each insert.
I usually do this by putting an output parameter called @ID (or similar) in the stored procedure. this is the basic outline:
In your application code, grab the @id after calling the insert procedure. Then you can then pass that to the second aspx page to use in your update statement. If you get a -1 for the ID, that indicates the insert failed.
Instead of using an output parameter, you could use a return value, though return values are usually used to report success or a failure code, and using them that way may be seen as 'hacky'.
See Scope_Identity, it's a very useful t-sql function.
这将完成你所说的你想要做的事情 - 但我认为如果你试图更新上一页添加的条目,而不仅仅是最后添加的记录,你可能会遇到一些问题。
如果多个用户尝试使用相同的programId 将条目添加到该表中怎么办?
我可能没有在正确的轨道上,但你可以这样做:
然后你可以将返回的 ID 传递到下一页(例如,如果你使用的是 asp.net,你可以更改按钮上的操作,或者你可以在 Session 变量中传递它)。
然后当你去更新时,你可以直接使用Id更新,例如
This will accomplish what you say you are trying to do - but I think you might have a bit of a problem if you are trying to update the entry added by the previous page, and not just the last added record.
What if more than one user attempts to add entries into this table using the same programId?
I might not be on the correct track here, but you could do something like this:
You can then pass that returned ID to the next page (e.g. if you are using asp.net you could change the action on a button, or you could pass it around in the Session variable).
Then when you go to update, you can just update using the direct Id, e.g.