假如我不知道表中某列的字段名,但我想判断这一列是否为空该如何写呢?
比如:
select * from tb where tb.name = null
假如name在第一列,我如何判断第一列为空.

解决方案 »

  1.   

    你是查找空还是null
    select * from tb where name is null

    select * from tb where name=''
      

  2.   

    select * from tb where tb.name is null 
      

  3.   

    declare @sqlstr nvarchar(2000)
    select @sqlstr=name from syscolumns where id in (select id from sysobjects where name='tb') and colid=1
    set @sqlstr='select * from tb where '+@sqlstr+' is null'
    exec(@sqlstr)
      

  4.   

    declare @field varchar(20)
    select @field= name  from syscolumns where id=object_id(表名) and colid=2exec('select * from 表名where isnull('+@field+','''')<>'''')  --不为空,为空就改=号--
    declare @field varchar(20)
    select @field= name  from syscolumns where id=object_id('LCLBILLBASIC') and colid=1
    exec('select IDNO,FlowNo from LCLBILLBASIC where isnull('+@field+','''')<>''''')   /*
    IDNO                           FlowNo                         
    ------------------------------ ------------------------------ 
    200808060059                   LB200808061a*/
     
      

  5.   

    declare @field varchar(20)
    select @field= name  from syscolumns where id=object_id(表名) and colid=2exec('select * from 表名where isnull('+@field+','''')<>''''')  --不为空,为空就改=号--
    declare @field varchar(20)
    select @field= name  from syscolumns where id=object_id('LCLBILLBASIC') and colid=1
    exec('select IDNO,FlowNo from LCLBILLBASIC where isnull('+@field+','''')<>''''')   /*
    IDNO                           FlowNo                         
    ------------------------------ ------------------------------ 
    200808060059                   LB200808061a*/
      

  6.   


    你第二个字段就 colid=2不就行了?
      

  7.   

    嘿嘿,我的错请问下面这个是什么原因呢?不是可以识别的 OPTIMIZER LOCK HINTS 选项