简体   繁体   English

当前正在插入的TSQL使用记录以选择另一个表中的数据

[英]TSQL Use record currently being inserted to select data in another table

Very new to TSQL... TSQL的新手...

I have the following table called "tblinit": 我有称为“ tblinit”的下表:

Account_Num    UserID        Task_Percent
-----------    ------------  ------------
1              john.smith    0.75

I would like to update the "Task Percent" value in "tblRaw" below. 我想更新下面“ tblRaw”中的“任务百分比”值。

Account_Num    UserID        Task_Percent
-----------    ------------  ------------
1              john.smith    0.5
2              mary.mickle   0.9
3              don.donalds   1

My plan is to use a TSQL stored procedure executed by a trigger on insert into "tblinit". 我的计划是使用插入到“ tblinit”中的触发器执行的TSQL存储过程。 The stored procedure will move the data into "tblRaw" (either a merge or a delete and insert) and then truncate "tblinit" when the procedure is done. 存储过程会将数据移到“ tblRaw”(合并或删除并插入)中,然后在过程完成时截断“ tblinit”。 tblInit is only used to stage incoming data. tblInit仅用于暂存传入的数据。

I have read about SCOPE_IDENTITY and @@IDENTIY but don't fully grasp the concept. 已经阅读了有关 SCOPE_IDENTITY@@IDENTIY但是并没有完全理解这个概念。 Is the scope defined by the trigger which executes the stored procedure? 范围是否由执行存储过程的触发器定义? In attempting my own SELECT statements using SCOPE_IDENTITY and @@IDENTITY I always return with a "NULL" result. 在尝试使用SCOPE_IDENTITY@@IDENTITY进行自己的SELECT语句时,我总是返回“ NULL”结果。 The referenced MSDN article seems to return primary keys that don't correlate to the data specified in the article's example. 所引用的MSDN文章似乎返回了与本文示例中指定的数据不相关的主键。 Clearly I am reading something incorrectly. 显然我读错了什么。 I want to grab the record that was just inserted and use it in my query. 我想获取刚刚插入的记录,并在查询中使用它。

In essence, how do I update john.smith's new percentage value automatically on insert or, alternatively, how do I add a new record entirely? 本质上,如何在插入时自动更新john.smith的新百分比值,或者如何完全添加新记录?

how do I update john.smith's new percentage value automatically on insert 我如何在插入时自动更新john.smith的新百分比值

This trigger could be used to do exactly that: 此触发器可用于精确地做到这一点:

create trigger tblinit_to_tblRaw
on tblinit
for insert
as
begin
    update r
    set r.Task_Percent = i.Task_Percent
    from inserted i
        join tblRaw r on i.UserID = r.UserID -- Join on Account_Num instead?
end

This does not take into account new records (no existing match in tblRaw). 这不考虑新记录(tblRaw中不存在匹配项)。 For that you might want to run if exists(... or merge . 为此, if exists(... ,则可能要运行if exists(...merge

I must confess to being a bit confused as to your intent given the various concepts you've referred to. 考虑到您所提到的各种概念,我必须承认对于您的意图有些困惑。

If you want to delete from the original table after your trigger fires in the update/delete then you're doing it wrong. 如果要在更新/删除中触发触发器后从原始表中删除,那么您做错了。

If you just want to keep a running total in another table for performance reasons then check out "indexed views". 如果出于性能原因,您只想在另一个表中保留运行总计,请查看“索引视图”。

If you want to add something to one table then update another and remove from the original then you are either looking for a queue or simply a stored procedure to perform the update on the appropriate table. 如果要向一个表中添加内容,然后更新另一个并从原始表中删除,则您正在寻找队列或只是一个存储过程来对适当的表执行更新。 You do not need to do complex steps with triggers and stuff. 您无需对触发器和其他东西执行复杂的步骤。

No idea where the IDENTITY stuff comes from. 不知道身份的来源。 Pretty sure you don't need it here. 相当确定您在这里不需要它。

I think you're making it more complex than it needs be. 我认为您正在使它变得比所需的更加复杂。

I could be wrong - feel free to elaborate. 我可能是错的-请随意阐述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM