views:

93

answers:

6

I'm looking at some SQL code which has a WHERE clause like this:

WHERE 'USD' = CCY

I asked the writer why he's putting the value on the left hand side, and he said it's best practice to do so, stemming from C++ where people could mistakenly assign the value instead of comparing equality by forgetting the second equals sign.

I've never seen this before.

What are your thoughts?

+2  A: 

Never seen it in SQL, where of course the C++ reasoning does not apply, as '=' is not an assignment operator in this context. also, a lot of C++ programmers (including me) don't like this style.

anon
+4  A: 

Best practise in c++ does not make it best practise in SQL. The query optimizer will not care, so it is just a matter of preference, but I have to say it would not be my preference or how I would naturally write it.

Andrew
A: 

If you look at it:

'USD' = CCY

is essentially the same:

 CCY = 'USD'

As for:

it's best practice to do so, stemming from C++ where people could mistakenly assign the value instead of comparing equality by forgetting the second equals sign.

Well, i have never seen this happen, and if this was that important, we would definitely have seen this somewhere and this would have been practiced by most if not by all.

Sarfraz
??????????????????
Sarfraz
+1 No idea why this got down marked, was saying same as rest of us
Andrew
@Andrew: Thanks your very much but i don't know how people are trying to get one down to get votes for themselves, this is really bad practice.
Sarfraz
@Sarfraz Ahmed: Maybe it's because of your bold assertion that "if something is not adopted by majority, it's not important". +0.
Piskvor
@Piskvor: thanks but anyone can make their text bold too, i don't think this is the policy of SO not to bold text??
Sarfraz
@Sarfraz Ahmed: That the text is bold is not my point. I object to the suggestion that "if it was important, everybody would do it." There's a lot of important issues in programming, yet those are not practiced by most.
Piskvor
+7  A: 

Er, C++ is not SQL. There's no == in SQL and no assignments in a WHERE clause.

I'm not sure it qualifies as "best practice" but there is a convention which places the known value on the right-hand side. So, with a literal as in your example that would be

WHERE CCY = 'USD' 
APC
A: 

I personally would not do it that way, but put the column name on the left hand side as this to me is more readable / easier to follow within an SQL query.

I've very rarely seen it done the opposite way, and don't think the reason given is really applicable to SQL (as has been pointed out, it's "=" in SQL, not "==")

AdaTheDev
A: 

If he says it's a best practice, I'd ask him to prove that with SQL not C++ sources. Since 99.9% of the SQL code I've ever read (including our code, other organization's code, Microsoft help files, SQL Blogs, etc) does the opposite of what your dev does, I'd say that violating the normal expectation of the developer who will maintain the code is a bad idea. In SQL we expect to see the form

WHERE CCY = 'USD'  

not

WHERE  'USD' = CCY  

Therefore the professsional would also write code in that manner to ensure it is clear to the maintainer.

HLGEM