SQL Server 2005:确定变量的数据类型 [英] SQL Server 2005: Determine datatype of variable

查看:25
本文介绍了SQL Server 2005:确定变量的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 TSQL 中在运行时确定局部变量的类型?

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 )

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

有没有人知道有什么方法可以做到这一点?

Does anyone know of any way to accomplish this?

这不是针对特定任务,这更像是一个一般知识问题.我很欣赏那些表明类型应该是已知的答案,因为它是在同一批次中声明的,我很好奇是否可以在运行时确定类型.

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.

推荐答案

运行这个

declare @d int

select @d = 500

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

这篇关于SQL Server 2005:确定变量的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆