if exists (select * from table1 where firld1=@var)
  print  'exists'
else
 print ' not exists'

解决方案 »

  1.   

    if exists (select a.name from syscolumns a join sysobjects b on (a.id=b.id and b.id=object_id('YourTableName')) where a.name='ColName')
    print 'YES'
    else
    print 'NO'
      

  2.   

    麻烦:
    if exists( select * from syscolumns a where a.id=object_id('tablename') and name='colname') 
    print 'yes'
    else
    print 'no'
      

  3.   

    海兄是不是理解错了?
    如果@var的值在列field1里本身并不存在,那么返回'not exists'
      

  4.   

    --很简单
    declare @len int
    select @len= COL_LENGTH( 'table' , 'field1' )
    if @len is null
       print 'not exist'
    else
       print ' exist'
      

  5.   

    create proc test
    @table varchar(20)
    @field varchar(20)
    as
    if exists(select @field from  @table)
    print 'yes'
    else
    print 'no'
      

  6.   

    USE table
    if 'field1' in(SELECT COL_NAME(object_id('table'), ORDINAL_POSITION)
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'table')
    print '存在'
    else
    print '不存在'
      

  7.   

    USE table
    if 'field1' in(SELECT COL_NAME(object_id('table'), ORDINAL_POSITION)
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'table')
    print '存在'
    else
    print '不存在'
      

  8.   


    declare @tablename nvarchar(30)
    declare @colname nvarchar(30)
    Set @tablename='tblBank'
    Set @colname='BankCode'
    exec ( 'if exists( select * from syscolumns a where a.id=object_id(''' +@tablename+''') and name='''+@colname+''') ' +
    ' print ''yes''  '+
    ' else  '+
    ' print ''no''  ')
      

  9.   

    : ItSeeker() (  ) 你的方法不錯, 比我的好。