转换时错误的SQL案例条件

时间:2017-10-04 14:31:28

标签: sql sql-server tsql

我有这个导致此错误的查询:

  

转换varchar值时失败')'数据类型tinyint。

这是我的问题代码:

select  
    case t.name 
       when 'numeric' 
          then cast (c.precision + ')' as varchar(100)) 
    end 
from 
    sys.columns c 
inner join 
    sys.types t ON t.user_type_id = c.user_type_id
inner join 
    sys.tables tb on tb.name = 'EX_EMPLOYEE' 
where  
    c.name = 'B_CODE' and tb.object_id = c.object_id

4 个答案:

答案 0 :(得分:2)

也许(如果是2012 +)

case t.name when 'numeric' then concat(c.precision,')') end 

答案 1 :(得分:1)

你也可以试试这个:

select  
     case t.name when 'numeric' then cast (c.precision as varchar(100)) +')' end 

因为c.precision显然是tinyint数据类型。当您添加不同的数据类型时,它会在c.precision +')'上失败。

答案 2 :(得分:1)

将您的连接移到.../Library/Preferences/<Bundle Identifier>.plist 之外,同时更改CASTJOIN个参数。

WHERE

答案 3 :(得分:1)

我认为你需要在连接之前将tinyint值转换为varchar。像这样(或者只是将c.precision转换为varchar(100),然后添加&#39;)&#39;:

void main()

{
    string name[5] = { 0 };
    int votes[5] = { 0 };
    int total = 0;

    for (int i = 0; i < 5; i++)
    {
        cout << "What is the name of Candidate number " << i + 1 << "?" << endl;
        cin >> name[i];

        cout << "How many votes did " << name[i] << " receive?" << endl;
        cin >> votes[i];

        total += votes[i];

        cout << total << endl;
    }

    system("pause");

}