SQL Server 2005数据库IN运算的一个错误

  • 来源: 赛迪网 作者: 若水   2008-04-29/13:34
  • 这篇论坛文章(赛迪网技术社区)主要介绍了SQL Server 2005数据库中,有关IN运算的一个错误示例,具体内容如下:

    /*

    测试in运算容易出错的一种情况,就是在子查询中的查询列实际不存在,会返回所有数据。

    */

     

    create table t_1(uid int)

    create table t_2(id int)

    insert into t_1

    select 1

    union all

    select 2

    union all

    select 3

    union all

    select 4

     

     

    insert into t_2

    select 1

    union all

    select 2

    union all

    select 7

    union all

    select 8

     

    select * from t_1 where uid in (select [uid] from t_2 where id like '[0-9]')

     

    drop table t_1,t_2

     

     

     

    /**//*

    从例子中能够看出,子查询中如果使用本来不存在的列,如果编译没报错的话,查询的结果是错误的,并且不会有任何提示。还有就是子查询中的这个列名,并不是随便写就行,要重现这个错误,需要这个列名在t_1表中存在。

     

    当然,如果单独执行select [user_id] from t_2 where id like '[0-9]' ,就会报错:

     

    消息207,级别16,状态1,第26 行

    列名'user_id' 无效。

     

    */


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多