2012年2月17日星期五

disable implicit casting

Is there a way do disable/disallow implicit casting in Sql Server 2000?

Say for example:

if ' ' = 0 select getdate()

This wil print out the current date and time.

But ' ' (a string, varchar, whatever) is not the same type as 0 (say, int). Implicit casting is nice, but is there also an off switch?

I've looked at the book Itzik Ben-Gan (T-SQL Programming). On the paragraph about implicit conversion he doesn't cite any way to switch it off. He also suggests that in some case may be useful to explicit cast data to sql_variant and reports the following example, that maybe can suggest a workaround:

select
case
when 1>1 then 10
when 1=1 then 'abc'

when 1<1 then 10
end;

select

case

when 1>1 then cast(10 as sql_variant)

when 1=1 then cast('abc' as sql_variant)

when 1<1 then cast(10 as sql_variant)

end;

MS help says also that "Implicit conversions are not visible to the user", so I guess there's no way to disable it.

Take a look also at the following docs:
http://msdn2.microsoft.com/en-us/library/ms191530.aspx
http://msdn2.microsoft.com/en-us/library/ms190309.aspx|||

Thanks.

It's really weird. I would have suspected an feature like 'option strict' like there is in VB (and for a good reason). It's a shame it's not there in T-SQL.

没有评论:

发表评论