ETidalgo
6/24/2018 - 9:52 PM

Sql If exists

Incomplete list of references How to Check If function Exists | SqlHints.com http://sqlhints.com/tag/how-to-check-if-function-exists/

-- drop user table
if object_id('dbo.blockedhostips','u') is not null
    drop table blockedhostips

-- drop procedure
if object_id('dbo.setlendersgroupmembership','p') is not null
drop procedure [setlendersgroupmembership]
go

-- alter
alter table dbo.dlrmerch drop constraint fk_dlrmerch_apntype
go

-- if at least one record exists
if not exists (select 1 from scwfmast where apn_type='full')
begin
  print 'create'
end
else
begin
  print 'skip'
end

-- column exists
if exists (select * from sys.columns where name = n'd/b code' and object_id = object_id(n'apnmast'))
    print 'found'
else
    print 'not found'
if col_length('apnmast', 'd/b cod') is not null
    print 'found'
else
    print 'not found'

-- SYS.OBJECTS   Type= ‘C’ ‘U’
-- •C: Check constraint
-- •D: Default constraint
-- •F: Foreign Key constraint
-- •L: Log
-- •P: Stored procedure
-- •PK: Primary Key constraint
-- •RF: Replication Filter stored procedure
-- •S: System table
-- •TF: Table value function
-- •TR: Trigger
-- •U: User table
-- •UQ: Unique constraint
-- •V: View
-- •X: Extended stored procedure

dbcc checkident ('Emp', reseed, 1)