更新 SQL Server

发布于 2024-08-16 11:21:31 字数 474 浏览 6 评论 0原文

对于你们来说非常简单的查询,我刚刚对此感到陌生。这是场景 - 一张表有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七分※倦醒 2024-08-23 11:21:31

您可以让第一个查询返回每个插入的 ID。
我通常通过在存储过程中放置​​一个名为@ID(或类似)的输出参数来实现此目的。这是基本轮廓:

ALTER PROCEDURE [dbo].[usp_YourInsertProcedure]
(
    @ProgramID INT, other params... ...        
    @ID INT = NULL OUTPUT
) AS BEGIN
    SET NOCOUNT ON;

    begin try
       INSERT INTO .....
    end try
    begin catch  end catch --nothing to do in catch block..
    set @id = coalesce(scope_identity(), -1) --gets the id of the inserted row
END

在您的应用程序代码中,在调用插入过程后获取@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:

ALTER PROCEDURE [dbo].[usp_YourInsertProcedure]
(
    @ProgramID INT, other params... ...        
    @ID INT = NULL OUTPUT
) AS BEGIN
    SET NOCOUNT ON;

    begin try
       INSERT INTO .....
    end try
    begin catch  end catch --nothing to do in catch block..
    set @id = coalesce(scope_identity(), -1) --gets the id of the inserted row
END

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.

说好的呢 2024-08-23 11:21:31
UPDATE tablea
SET field6 = @valueForField6, ...
WHERE programId = @programId
AND id = 
(
  SELECT TOP 1 id
  FROM tablea
  WHERE programId = @programId
  ORDER BY id DESC
)

这将完成你所说的你想要做的事情 - 但我认为如果你试图更新上一页添加的条目,而不仅仅是最后添加的记录,你可能会遇到一些问题。

如果多个用户尝试使用相同的programId 将条目添加到该表中怎么办?

我可能没有在正确的轨道上,但你可以这样做:

INSERT INTO tablea (programId, field1, field2, field3, field4, field5)
OUTPUT INSERTED.Id
VALUES(@programId, @valueForField1, @valueForField2, @valueForField3, @valueForField4, @valueForField5)

然后你可以将返回的 ID 传递到下一页(例如,如果你使用的是 asp.net,你可以更改按钮上的操作,或者你可以在 Session 变量中传递它)。

然后当你去更新时,你可以直接使用Id更新,例如

UPDATE tablea
SET field6 = @valueForField6
WHERE id = @valuePassedThroughToNextPage
UPDATE tablea
SET field6 = @valueForField6, ...
WHERE programId = @programId
AND id = 
(
  SELECT TOP 1 id
  FROM tablea
  WHERE programId = @programId
  ORDER BY id DESC
)

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:

INSERT INTO tablea (programId, field1, field2, field3, field4, field5)
OUTPUT INSERTED.Id
VALUES(@programId, @valueForField1, @valueForField2, @valueForField3, @valueForField4, @valueForField5)

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.

UPDATE tablea
SET field6 = @valueForField6
WHERE id = @valuePassedThroughToNextPage
木格 2024-08-23 11:21:31
UPDATE table SET
    X = x, .......
WHERE programId = @programId
AND id = (
    SELECT MAX(id)
    FROM table
    WHERE programId = @programId
)
UPDATE table SET
    X = x, .......
WHERE programId = @programId
AND id = (
    SELECT MAX(id)
    FROM table
    WHERE programId = @programId
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文