views:

381

answers:

3

Is it possible to determine the type of a local variable at runtime in TSQL?

For example, say I wanted to do something along these lines:

IF ( @value IS INTEGER )

Or

IF ( TYPEOF(@value) = <whatever> )

Does anyone know of any way to accomplish this?

EDIT: This is not for a specific task, this is more of a general knowledge question. I do appreciate answers that indicate that the type should be known since it is declared within the same batch, I am curious as to whether the type can be determined at runtime.

A: 

I don't think this is possible since you need to declare the datatype of the local variable to begin with.

Eppz
A: 

I don't think so - BUT it is a local variable so are declaring it in the same procedure so you would know the type anyways - or am I missing something?

DJ
+6  A: 

run this

declare @d int

select @d = 500

if cast(sql_variant_property(@d,'BaseType') as varchar(20))  = 'int'
print 'yes'
else
print 'no'
SQLMenace