2015-12-21 68 views
2

如果在SELECT语句中有如下一行:数据类型的IIF语句

iif(fr.BoundID=0,'OutBound','InBound') as 'FlightBound' 

后来,当我执行CREATE TABLE语句,我应该包括BoundID字段的实际数据类型是在TINYINT数据库表,或者数据类型是varchar,因为我认为(但不是100%肯定地看着现有的代码)以前写这段代码的人说如果ID是0就显示'OutBound,InBound'?

+0

我认为这取决于你在IIF返回的内容。在你的情况下,它的VARCHAR。 –

+0

谢谢大家的回答,我还有另外一个问题,我需要发布一些我以前没有遇到过的问题。 – BruceyBandit

回答

2

在你的情况下,它将是VARCHAR(8)。您可以使用sys.dm_exec_describe_first_result_set随时检查元数据:

SELECT * 
FROM sys.dm_exec_describe_first_result_set(
    'SELECT iif(BoundID=0,''OutBound'',''InBound'') as ''FlightBound'' 
    FROM #tab',NULL,0) 

LiveDemo

什么数据类型来选择新表TINYINT VS Textual representation是依赖于您的业务需求。我可能会留在TINYINT(搜索名为BoundType的查找表或询问高级开发人员/架构师)。

1

返回类型是最高precendence在true_valuefalse_value类型(reference,见return types

返回与从true_value和false_value的类型最高优先级的数据类型。有关更多信息,请参阅数据类型优先级(Transact-SQL)。

数据类型precendence here了到SQL Server 2016

user-defined data types (highest) 
sql_variant 
xml 
datetimeoffset 
datetime2 
datetime 
smalldatetime 
date 
time 
float 
real 
decimal 
money 
smallmoney 
bigint 
int 
smallint 
tinyint 
bit 
ntext 
text 
image 
timestamp 
uniqueidentifier 
nvarchar (including nvarchar(max)) 
nchar 
varchar (including varchar(max)) 
char 
varbinary (including varbinary(max)) 
binary (lowest)