[英]Trouble viewing rows not affected from UPDATE using temp table
I need to update some 600+ rows of customer data, using a query I've generated from Excel.我需要使用我从 Excel 生成的查询更新大约 600 多行客户数据。 I'm generating my update query using data that is more recent than the data we have in our test environment - so I want to view the rows that were not updated when I finish executing within the test environment.
我正在使用比我们在测试环境中拥有的数据更新的数据生成我的更新查询 - 因此我想查看在测试环境中完成执行时未更新的行。
I am trying to achieve this with IF @@ROWCOUNT < 1
.我试图用
IF @@ROWCOUNT < 1
来实现这一点。
Consider the following update query:考虑以下更新查询:
CREATE TABLE #not_updated (
NOT_UPDATED VARCHAR(30)
);
UPDATE TABLE1 SET COL1 = 'text1A' WHERE COL2 LIKE 'text1B'; IF @@ROWCOUNT < 1 INSERT INTO #not_updated SELECT 'text1B'
UPDATE TABLE1 SET COL1 = 'text2A' WHERE COL2 LIKE 'text2B'; IF @@ROWCOUNT < 1 INSERT INTO #not_updated SELECT 'text2B'
-- |
-- V
-- 600+ MORE INSERTS...
SELECT * FROM #not_updated
DROP TABLE #temp
However, when I run the query, SELECT * FROM #not_updated
it does not return any rows - even though I can see in the "Messages" tab 0 rows affected
for some of the entries.但是,当我运行查询时,
SELECT * FROM #not_updated
它不会返回任何行 -即使我可以在“消息”选项卡中看到某些条目0 rows affected
。
If I change my code to IF @@ROWCOUNT = 1
, my select will return that entry.如果我将代码更改为
IF @@ROWCOUNT = 1
,我的选择将返回该条目。 Any ideas on what I'm doing wrong?关于我做错了什么的任何想法? Or if there is an easier way to achieve this?
或者是否有更简单的方法来实现这一目标?
I'm not sure about the reliability of @@ROWCOUNT
when UPDATE fails (entry does not exist).当 UPDATE 失败(条目不存在)时,我不确定
@@ROWCOUNT
的可靠性。 For now, I'm using the following workaround:目前,我正在使用以下解决方法:
UPDATE TABLE1 SET COL1 = 'text1A' WHERE COL2 LIKE 'text1B'; IF (SELECT COUNT(*) TABLE1 WHERE COL2 LIKE 'text1B') < 1 INSERT INTO #not_updated SELECT 'text1B'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.