SQL Server 2005: Determine datatype of variable

ⅰ亾dé卋堺 提交于 2019-12-18 02:38:23

问题


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.


回答1:


run this

declare @d int

select @d = 500

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



回答2:


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?



来源:https://stackoverflow.com/questions/447944/sql-server-2005-determine-datatype-of-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!