0

Problem: The code reads each row and should determine whether it is empty or not. I want to print out "works" if the column is null. Otherwise, it should not do anything. Right now it is not working. It is giving me an error that the column does not exist.(the one mentioned in the if statement)

Here is the code. Code:

DECLARE Employee_Cursor CURSOR FOR

SELECT [Description]  
FROM tblSubViews ts  
INNER JOIN tblViewSubViewJoin tvs  
ON ts.SubViewId = tvs.SubViewId Where tvs.ViewId = 4

OPEN Employee_Cursor

FETCH NEXT FROM Employee_Cursor
WHILE @@FETCH_STATUS = 0

BEGIN   
if (Description = null)
begin
print 'works'
end
   FETCH NEXT FROM Employee_Cursor
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor
3
  • Why don't you use a set based solution io cursors? Commented Nov 23, 2011 at 14:51
  • @Lieven: because the desired result isn't a set? Commented Nov 23, 2011 at 15:01
  • @onedaywhen - lol, the real query most likely does more than just printing 'works' but with what we know know, this can easily be converted to a set based solution. I'm sure you agree that a set based solution is preferrable to a cursor based one. Commented Nov 23, 2011 at 15:05

4 Answers 4

3

NULL is a special value in SQL - actually, it's the absence of a value.

Therefore, since it's not a value, you cannot compare with regular means - you need to use the IS NULL or IS NOT NULL methods:

if (Description IS NULL)
begin
  print 'works'
end

Comparing with IS NULL or IS NOT NULL will give you the result you're looking for.

Sign up to request clarification or add additional context in comments.

2 Comments

I have executed it within the cursor. FETCH NEXT FROM Employee_Cursor WHILE @@FETCH_STATUS = 0 BEGIN if (Description IS NULL) begin print 'works' end
I have tried that as well but it only prints out two rows. When really, it should be printing out three
2

You cannot compare to NULL using equals, you mush use ID (Description IS NULL)

When using cursors it is also necessary to select into a variable:

  FETCH NEXT FROM Employee_Cursor INTO @Description 
  WHILE @@FETCH_STATUS = 0

You would of course need to declare @Description to be of a type appropriate for the column,

4 Comments

With this it only print out two rows instead of three.
When I do it without FETCH NEXT FROM Employee_Cursor INTO @Description it prints out three rows which are null
and with this FETCH NEXT FROM Employee_Cursor INTO @Description it only prints out two rows. Any suggestions?
you need to fetch into inside the loop as well .I suppose you dont get eny debug prints when alues are non null.
2

There isnt a need for a cursor in this situation

SELECT 
[Description],
ISNULL([Description]   ,'Works') AS text
FROM tblSubViews ts   
INNER JOIN 
tblViewSubViewJoin tvs   
ON 
ts.SubViewId = tvs.SubViewId 
Where 
tvs.ViewId = 4

You could then shorten this down by adding an extra criteria to your WHERE clause that only selects null description columns?

1 Comment

+1 for using set-based op. This sort of query shouldn't need a cursor to start with.
0

Use the isnull() function in your query, according to your logic.

1 Comment

isnull() prints out one row less than the actually number of rows.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.