现在有4个整数变量:@A=1;@B=2;@C=3;@D=4.
想得到的结果是1234.
有两种方法:1.@A*1000+@B*100+@C*10+@D
            2.先将整形变量转化为字符串,然后 @A+@B+@C+@D或者直接定义变量的类型为 varchar操作的数据库是 SQL 。版本是SQL 2008 

解决方案 »

  1.   

    下面统计了以上两种方法执行一次的执行时间,使用计算方式确实很慢,单独一次就慢了1毫秒set statistics profile on
    set statistics io on
    set statistics time on
    go
    declare @A int,@B int,@C int,@D int
    select @A=1,@B=2,@C=3,@D=4
    select @A*1000+@B*100+@C*10+@D
    go
    set statistics profile off
    set statistics io off
    set statistics time off
    go
    set statistics profile on
    set statistics io on
    set statistics time on
    go
    declare @A int,@B int,@C int,@D int
    select @A=1,@B=2,@C=3,@D=4
    select cast(@A as varchar(1))+cast(@B as varchar(1))+cast(@C as varchar(1))+cast(@D as varchar(1))
    go
    set statistics profile off
    set statistics io off
    set statistics time off
    go
    set statistics profile on
    set statistics io on
    set statistics time on
    go
    declare @A int,@B int,@C int,@D int
    select @A=1,@B=2,@C=3,@D=4
    select ltrim(@A)+ltrim(@B)+ltrim(@C)+ltrim(@D)
    go
    set statistics profile off
    set statistics io off
    set statistics time off
    go
    /*
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。(1 row(s) affected)SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。(1 row(s) affected)SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。(1 row(s) affected)SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。*/
      

  2.   

    有个怪现象,以文本显示结果与以网络显示结果,居然统计的执行时间不一样。
    上面我贴出来的是以文本形式显示结果,而下面的是以网络形式显示结果。SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。(1 row(s) affected)SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。(1 row(s) affected)SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。(1 row(s) affected)SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
    SQL Server 分析和编译时间: 
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。SQL Server 执行时间:
       CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。