--------------------Código de la Función-----------------------------
CREATE FUNCTION ColumnAlreadyExists(@TableName NVARCHAR(128),@ColumnName NVARCHAR(128))
RETURNS INTEGER--Returns 0 if column does not exist. Returns 1 if column exists.
AS
BEGIN
--See if the Table already contains the column.
IF EXISTS
(SELECT * FROM SysObjects O INNER JOIN SysColumns C ON O.ID=C.ID
WHERE ObjectProperty(O.ID,'IsUserTable')=1
AND O.Name=@TableName
AND C.Name=@ColumnName)
RETURN 1
--Table does not contain the column.
RETURN 0
END
GO
----------------------Usar la función-------------------------IF .dbo.ColumnAlreadyExists('CLIENTE','IdCte')=1
print 'Existe la columna IdCte en la tabla.'
Else
print 'No existe la columna IdCte en la tabla.'
--------------------------------------------------------------
No hay comentarios.:
Publicar un comentario