网络安全 频道

SQL Server语句做数值大小比较的实现

    问:sql server里
  我有①、②二张表:
  ①表有一个字段。通过条件查询出来其中一个字段的返回值。
  ②表有二个字段。通过条件查询出来其中2个字段的返回值,并将其值相加。
  然后把①、②表的返回值做数字的大小比较。
  如果返回①表的值大于②表的值,则返回“错误”的提示。
  反之,则报正常。
  怎么写这个sql语句?


  答:
  create table t1
  (
  idd varchar(10) not null,
  value int not null
  )

  create table t2
  (
  idd varchar(10) not null,
  value1 int not null,
  value2 int not null
  )

  insert into t1
  select ''1'', 10
  union all
  select ''2'', 20

  insert into t2
  select ''1'', 3, 5
  union all
  select ''2'', 12, 9

  declare @Res varchar(10)
  select @Res = case  when ((select t1.value from t1 where idd=''2'')  > (select value=t2.value1+t2.value2 from t2 where idd=''2'') )
  then ''错误'' else ''正确'' end

  select @Res

  drop table t1
  drop table t2

  /*结果
  正确
  */
http://www.cnxhacker.com/Article/program/network/200610/6093.html
0
相关文章