Technical Article

Column Exists or not?

,

We need some time to add the columns to Existing Table,

Then that column name already exists in the table or not?

we can use this function for dynamically execution.

CREATE FUNCTION Axfn_ColumnExists (
@TableName VARCHAR(100)
,@ColumnName VARCHAR(100)
)
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @Result VARCHAR(100);

IF EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = @TableName
AND COLUMN_NAME = @ColumnName
)
BEGIN
SET @Result = 'Already Exsits'
END
ELSE
BEGIN
SET @Result = 'Not Available, You can create now!'
END

RETURN (@Result)  
END
GO

SELECT dbo.Axfn_ColumnExists('TestTable', 'ColumnName')

Rate

2 (7)

You rated this post out of 5. Change rating

Share

Share

Rate

2 (7)

You rated this post out of 5. Change rating