Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用联接将此插入更改为更新_Sql Server_Tsql_Sql Server 2008 - Fatal编程技术网

Sql server 使用联接将此插入更改为更新

Sql server 使用联接将此插入更改为更新,sql-server,tsql,sql-server-2008,Sql Server,Tsql,Sql Server 2008,我们需要停止用记录填充表,只需重复更新每条记录的1行。没有必要保留任何历史记录。我使用过这个UPDATE语句,但无法获得正确的语法来考虑JOIN 有人能帮我把这个插入到一个简单的更新中吗?一旦我弄清楚UPDATE语句的语法,我将添加IF EXISTS(执行更新)ELSE(执行插入)。我正在使用sqlfool.com提供的一个优秀脚本,但是已经做了一些修改,我希望这是最后一个。谢谢 Insert Into [testCentral].[ReplMonitor].[dbo].[replMonitor

我们需要停止用记录填充表,只需重复更新每条记录的1行。没有必要保留任何历史记录。我使用过这个UPDATE语句,但无法获得正确的语法来考虑JOIN

有人能帮我把这个插入到一个简单的更新中吗?一旦我弄清楚UPDATE语句的语法,我将添加IF EXISTS(执行更新)ELSE(执行插入)。我正在使用sqlfool.com提供的一个优秀脚本,但是已经做了一些修改,我希望这是最后一个。谢谢

Insert Into [testCentral].[ReplMonitor].[dbo].[replMonitor]
(
      monitorDate
    , publicationName
    , publicationDB
    , iteration
    , tracer_id
    , distributor_latency
    , subscriber
    , subscriber_db
    , Loc_ID
    , subscriber_latency
    , overall_latency
)
Select 
      @currentDateTime
    , @publicationToTest
    , @publicationDB
    , iteration
    , tracer_id
    , IsNull(distributor_latency, 0)
    , subscriber
    , subscriber_db
    , b.LOC_ID
    , IsNull(subscriber_latency, 0)
    , IsNull(overall_latency, 
        IsNull(distributor_latency, 0) + IsNull(subscriber_latency, 0))
From @tokenResults AS a
JOIN LotteryRetail.dbo.casino b
ON 1 = 1 ;

你应该考虑使用合并来实现这一点。但这里有一个关于你的数据的解决方案。不幸的是,我不知道你想如何加入你的团队。所以你需要自己填写。以及应该很明显的柱子

update c
set monitorDate = @currentDateTime,
    publicationName = @publicationToTest
-- fill out the rest of your columns.
from
[testCentral].[ReplMonitor].[dbo].[replMonitor] c
join 
@tokenResults a
on --fill out your join
join 
LotteryRetail.dbo.casino b
on --fill out your join

我同意t-clausen.dk,merge将一次性完成插入和更新

我认为您正在为更新语句寻找类似的内容,但如果不知道LotteryRetail.dbo.casino和@tokenResults是什么,很难判断:

UPDATE [testCentral].[ReplMonitor].[dbo].[replMonitor]
SET monitorDate = @currentDateTime
    ,publicationName = @publicationToTest
    ,publicationDB = @publicationDB
    ,iteration = b.iteration
    ,tracer_id = b.tracer_id
    ,distributor_latency = b.IsNull(distributor_latency, 0)
    ,subscriber = b.subscriber
    ,subscriber_db = b.subscriber_db
    ,Loc_ID = b.b.LOC_ID
    ,subscriber_latency = b.IsNull(subscriber_latency, 0)
    ,overall_latency = b.IsNull(overall_latency, 
    IsNull(distributor_latency, 0) + IsNull(subscriber_latency, 0))
FROM LotteryRetail.dbo.casino b

只有当[testCentral].[ReplMonitor].[dbo].[ReplMonitor]只有一条记录时,此操作才能正常工作,否则您需要添加WHERE子句,以将更新限制为只更改要更改的记录。

看到此,可能会有所帮助:查看此操作:谢谢,但我无法使其与join一起按预期方式工作,不断出现错误: