T-SQL debug... very brief

In the code below, I get two records as checked by @@rowcount . From the if statement below, I don't expect the message 'sorry, ... is not in the tblCustomer' to be printed but it does?

Question: What is wrong with this if statement?

Thank you.


DECLARE @ta table (
	CustomerID int
	, FirstName varchar(30)
	, LastName varchar(30))
INSERT INTO @ta SELECT 
    CustomerID
    , FirstName
    , LastName
FROM tblCustomers  
WHERE FirstName = 'Mike'

print @@rowcount    -- this prints 2

IF @@rowcount =0
	PRINT 'sorry, ... is not in the tblCustomer.'  - this line shouldn't print but it does, why?

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent DyerIT Security Analyst SeniorCommented:
Removing the INSERT portion of the script..

In a DEBUG sense, what do you get when you change to:

SELECT 
    CustomerID
    , FirstName
    , LastName
FROM tblCustomers  
WHERE FirstName = 'Mike'

Open in new window


If you still only see two results, you may need to check COLLATION to be sure you have Case-sensitivity turned off.

HTH,

Kent
0
mankowitzCommented:
Is it possible that the line "print @@rowcount" is actually resetting rowcount to zero? See this: http://technet.microsoft.com/en-us/library/ms187316.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mankowitzCommented:
see the following fiddle: The first one prints 2, the second one prints 0. http://sqlfiddle.com/#!6/4c315/6

DECLARE @ta table (
	CustomerID int
	, FirstName varchar(30)
	, LastName varchar(30))
INSERT INTO @ta SELECT 
    CustomerID
    , FirstName
    , LastName
FROM tblCustomers  
WHERE FirstName = 'Mike'

select @@rowcount;
select @@rowcount;

Open in new window

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.