OSQL 中的 @@ROWCOUNT 返回与 TSQL 中不同的结果
我在使用 OSQL 时遇到一些奇怪的行为,希望得到任何帮助。
我有一个批处理文件,它将数据库字段从一列复制到另一列。下面是一个示例脚本:
SET NOCOUNT ON;
UPDATE Table1 SET Table1.EmailAddress = Table2.GenericField FROM Table1
INNER JOIN Table2 ON Table1.ID = Table2.ID WHERE GenericField LIKE '%@%.%'
AND EmailAddress IS NULL;
SELECT @@ROWCOUNT;
假设所有 EmailAddress 字段不再为 NULL,我希望更新语句返回 @@ROWCOUNT 为 0。
- 示例 1:
在查询分析器中运行上述查询,@@ROWCOUNT 为 0。那挺好的。
- 示例 2:
Test.Sql 包含与上面完全相同的 SQL 语句。如果我使用以下 OSQL 语句,@@ROWCOUNT 也会得到 0:
osql.exe -D TestConn -U UserID -P pwd -s , -h-1 -w 100 -n ^
-i "C:\Scripts\Test.sql"
- 示例 3:
如果我在批处理文件而不是 SQL 文件中包含 SQL 语句,我得到的 @@ROWCOUNT 为 2< /strong>:
osql.exe -D TestConn -U UserID -P pwd -s , -h-1 -w 100 -n -Q ^
"SET NOCOUNT ON;
UPDATE Table1 SET Table1.EmailAddress = Table2.GenericField FROM Table1
INNER JOIN Table2 ON Table1.ID = Table2.ID WHERE GenericField LIKE '%@%.%'
AND EmailAddress IS NULL;
SELECT @@ROWCOUNT;"
示例 3 中的开关是否设置错误?为什么我会突然得到 2 而不是 0 的 @@ROWCOUNT?
我第一次运行该脚本时,@@ROWCOUNT 为 5,此时仅更新了 3 条记录。
I am experiencing some strange behavior with OSQL, and would appreciate any help.
I have a batch file that will copy a database field from one column to another. Here is a sample script:
SET NOCOUNT ON;
UPDATE Table1 SET Table1.EmailAddress = Table2.GenericField FROM Table1
INNER JOIN Table2 ON Table1.ID = Table2.ID WHERE GenericField LIKE '%@%.%'
AND EmailAddress IS NULL;
SELECT @@ROWCOUNT;
Assuming all the EmailAddress fields are NOT NULL anymore, I would expect the update statement to return a @@ROWCOUNT of 0.
- Example 1:
Running the above query in Query Analyzer gives me 0 for the @@ROWCOUNT. That's good.
- Example 2:
Test.Sql contains the exact same SQL statement as above. If I use the following OSQL statement, I also get a 0 for the @@ROWCOUNT:
osql.exe -D TestConn -U UserID -P pwd -s , -h-1 -w 100 -n ^
-i "C:\Scripts\Test.sql"
- Example 3:
If I have the SQL statement in the batch file instead of a SQL file, I get a @@ROWCOUNT of 2:
osql.exe -D TestConn -U UserID -P pwd -s , -h-1 -w 100 -n -Q ^
"SET NOCOUNT ON;
UPDATE Table1 SET Table1.EmailAddress = Table2.GenericField FROM Table1
INNER JOIN Table2 ON Table1.ID = Table2.ID WHERE GenericField LIKE '%@%.%'
AND EmailAddress IS NULL;
SELECT @@ROWCOUNT;"
Do I have a switch set wrong for example 3? Why would I suddenly get a @@ROWCOUNT of 2 instead of 0?
The first time I ran the script, I got a @@ROWCOUNT of 5, when only three records were updated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
批处理文件将
%
扩展为变量。您可以使用双%%
来转义它:Batch files expand
%
to a variable. You can escape it by using double%%
s: