[翻译]  How to use @@ROWCOUNT in IF statement as well as within BEGIN..END block?

[CHINESE]  如何在IF语句中以及BEGIN..END块中使用@@ ROWCOUNT?


My code is as follows:

我的代码如下:

SET @var = (SELECT var FROM dbo.varDB WHERE varName = @varName)

IF (@@ROWCOUNT > 0)
    BEGIN
        //carry out insert
    END

Am I right in saying this will always return a 1 for @@ROWCOUNT as it carries out an assignment last?

我是否正确地说这将总是为@@ ROWCOUNT返回1,因为它最后执行一项任务?

If so, what would be the most elegant solution to allow me to:

如果是这样,那么最优雅的解决方案是什么让我:

  • Create variable with the result from my query stored in it
  • 使用存储在其中的查询结果创建变量
  • Test if the query returns any results
  • 测试查询是否返回任何结果

4 个解决方案

#1


8  

The reason it returns one is because you've assigned exactly one value (some arbitrary value). You should be using EXISTS instead of doing a manual count anyway, as @Sive suggests. The reason is that EXISTS can perform no worse than COUNT, and in the use case you're describing, it doesn't really matter what the actual count is anyway. You want to know if the count is zero or greater than zero.

它返回一个的原因是你只分配了一个值(某个任意值)。你应该使用EXISTS而不是手动计数,正如@Sive建议的那样。原因是EXISTS的性能不会低于COUNT,并且在您描述的用例中,无论如何实际计数并不重要。您想知道计数是零还是大于零。

The problem with assigning that value to a variable in the way you've done it, is what happens if there are multiple rows that match? Let's try it:

如果有多个行匹配会发生以您完成方式将值赋给变量的问题?我们来试试吧:

DECLARE @foo TABLE([var] INT, varname SYSNAME);
INSERT @foo VALUES (1,N'bob'),(2,N'bob');
DECLARE @var INT;
SET @var = (SELECT [var] FROM @foo WHERE varname = N'bob

Result:

结果:

Msg 512, Level 16, State 1, Line 4
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

消息512,级别16,状态1,行4子查询返回的值超过1。当子查询遵循=,!=,<,<=,>,> =或子查询用作表达式时,不允许这样做。

Now, if varname is unique, I would do this:

现在,如果varname是唯一的,我会这样做:

SELECT @var = [var] FROM dbo.varDB WHERE varName = @varName;
IF @var IS NOT NULL
BEGIN
    // carry out insert
END
ELSE
BEGIN
    PRINT 'Already existed! ' + RTRIM(@var);
END

If varname is not unique, then I'm not sure what you're going to do with a single value. Which one did it pull? Imagine this scenario:

如果varname不是唯一的,那么我不确定你要用单个值做什么。它拉了哪一个?想象一下这种情况:

DECLARE @foo TABLE([var] INT, varname SYSNAME);
INSERT @foo VALUES (3,N'bob'),(2,N'adam'),(1,N'bob');
DECLARE @var INT;
SELECT @var = [var] FROM @foo WHERE varname = N'bob';
PRINT @var;

Will @var be 1 or 3? Who knows? Why does it matter which one it is if you're only capturing one of potentially many values? Are you going to do something with that row but not the others?

@var会是1还是3?谁知道?如果您只捕获可能的多个值之一,为什么它是重要的呢?你打算用那一行而不是其他人做点什么吗?

In addition, if your intention is to insert data from this table, why not simply throw out the up-front check, the row count etc. and just say:

另外,如果你打算从这个表中插入数据,为什么不简单地抛弃前期检查,行数等,然后说:

INSERT dbo.SomeTable(column1 --,...other columns
  SELECT var --,...other columns
  FROM dbo.varDB
  WHERE varName = @varName;

The insert won't happen if the row doesn't exist.

如果行不存在,则不会发生插入。

#2


5  

You can do something like this using EXISTS. This will check if at least one record exists for the given value in @varName before carrying out your statements enclosed between BEGIN and END.

你可以使用EXISTS做这样的事情。这将检查在执行BEGIN和END之间的语句之前,@varName中是否存在给定值的至少一条记录。

Script:

脚本:

 IF EXISTS (SELECT var FROM dbo.varDB WHERE varName = @varName)
    BEGIN
         //carry out insert
    END

MSDN Documentation: EXISTS (Transact-SQL)

MSDN文档:EXISTS(Transact-SQL)

Based on comments, update #1:

I don't understand the requirement to know the value. I think you might want to use a COUNT, if you are looking to fetch the count.

我不明白了解价值的要求。我想你可能想要使用COUNT,如果你想要获取计数。

DECLARE @recordsCount
SELECT @recordsCount = COUNT(var) FROM dbo.varDB WHERE varName = @varName

IF recordsCount > 0 
BEGIN
  // your statements
END

Based on comments, update #2:

If you would like to use the result set itself inside the BEGIN and END, then the question needs more details about how the result set would be used inside the statements. Based on further inputs, may be you won't even need the IF statement. Further details needed to provide a clear solution to your problem.

如果您想在BEGIN和END中使用结果集本身,那么问题需要有关如何在语句中使用结果集的更多详细信息。根据进一步的输入,您可能甚至不需要IF语句。需要进一步的细节来为您的问题提供明确的解决方案。

#3


5  

You can set your variable to NULL (just to be sure).

您可以将变量设置为NULL(只是为了确定)。

If your query returns no rows, @var is still NULL.
If it returns one row, @var will have a value
If it returns more than one row you will have an exception.

如果查询未返回任何行,则@var仍为NULL。如果它返回一行,@ var将有一个值如果它返回多行,你将有一个例外。

SET @var = NULL

SET @var = (SELECT var FROM dbo.varDB WHERE varName = @varName)
IF @var IS NOT NULL
BEGIN
//carry out insert
END

Update

更新

If you really want to use @@ROWCOUNT instead you can assign your value using select instead of set.

如果你真的想使用@@ ROWCOUNT,你可以使用select而不是set来赋值。

SELECT @var = var FROM dbo.varDB WHERE varName = @varName
IF (@@ROWCOUNT > 0)
BEGIN
//carry out insert
END

#4


3  

You can select and test the count of returned rows:

您可以选择并测试返回行的计数:

SELECT @var = var FROM dbo.varDB WHERE varName = @varName
IF (@@ROWCOUNT > 0)
BEGIN
   //carry out insert
END

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号