Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Check if column exists in SQL Server table?

sys.columns catalogue view Returns a row for each column of an object that has columns, such as views or tables. The following is a list of object types that have columns:

1.  Table-valued assembly functions (FT)
2.  Inline table-valued SQL functions (IF)
3.  Internal tables (IT)
4.  System tables (S)
5.  Table-valued SQL functions (TF)
6.  User tables (U)
7.  Views (V)

 

Example:

IF EXISTS(SELECT * FROM sys.columns 
            WHERE Name = N'IsACtive' and Object_ID = Object_ID(N'Products'))
BEGIN
    -- Column Exists
    PRINT 'Column IsACtive Exists'
End


Output:

Column IsACtive Exists

 

Update from select query in sql server?

      Update from select using sql server:

 You can update records from another table, explained in below example


Example without using join:


Update Products 
SET isActive=1,
Products.[Description]=A.[Description]
FROM ActiveProducts A
Where Products.ProductCode=A.ProductCode


update using inner join :

Update P
SET P.isActive=1,
P.[Description]=A.[Description]
FROM Products P
INNER JOIN ActiveProducts A  ON P.ProductCode=A.ProductCode