The reason it returns one is because you assigned exactly one value (some kind of arbitrary value). You should use EXISTS
instead of doing the score manually, as @Sive suggests. The reason is that EXISTS can perform just as well as COUNT, and in the case you are describing, it doesn’t really matter what the actual score is. You want to know if the count is zero or greater than zero.
The problem with assigning this value to a variable as you did it, what happens if there are several lines that match? Let's try:
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 cost. This is unacceptable when a subquery follows = ,! =, <, <=,>,> = or when the subquery is used as an expression.
Now, if varname
unique, I would do the following:
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
not unique, then I'm not sure what you are going to do with a single value. Which one did it? Imagine this scenario:
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 if you only capture one of the potentially many values? Are you going to do something with this line, but not with others?
In addition, if you intend to insert data from this table, why not just throw out the preliminary check, the number of rows, etc. and just say:
INSERT dbo.SomeTable(column1 --,...other columns SELECT var --,...other columns FROM dbo.varDB WHERE varName = @varName;
Insertion will not occur if the row does not exist.