Technical Article

spGetFieldsExists

,

spGetFieldsExists takes a tablename and a field name, and checks if that field exists in that table.
It simply returns 0 or 1 (false or true).

/**********************************************************
  spGetFieldsExists
**********************************************************/
Print 'spGetFieldsExists'
go

If Exists (Select * From SysObjects
             Where ID = Object_ID('spGetFieldsExists')
               And ObjectProperty(ID, 'IsProcedure') = 1)
  Drop Procedure spGetFieldsExists
go

Create Procedure spGetFieldsExists 
  @TableName varchar(50),
  @FieldName varchar(50),
  @exists integer output
as

  set NoCount on
  --try

    declare 
      @result int

    if exists 
      (
        select *
          from 
            SysObjects O, 
            SysConstraints C, 
            SysColumns L
          where ObjectProperty(O.ID, 'IsUserTable') = 1
            and O.ID = C.ID
            and O.ID = L.ID
            and L.Name like @FieldName
            and O.Name like IsNull(@TableName, O.Name)
      )
      set @exists = 1
    else
      set @exists = 0

  --finally
    SuccessProc:
    return 0  /* success */
  --except
    ErrorProc:
    return 1 /* failure */
  --end
go

Grant Execute On spGetFieldsExists To Public
go

Rate

★ ★ ★ ★ ★ ★ ★ ★ ★ ★

You rated this post out of 5. Change rating

Share

Share

Rate

★ ★ ★ ★ ★ ★ ★ ★ ★ ★

You rated this post out of 5. Change rating