select 'Column_name' = name,
'Type' = type_name(user_type_id),
'Length' = convert(int, max_length) from sys.all_columns
where object_id = (select object_id from sys.all_objects where object_id = object_id('city'))
and name not in (SELECT iskcu.Column_Name FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS istc join INFORMATION_SCHEMA.KEY_COLUMN_USAGE iskcu on istc.CONSTRAINT_NAME = iskcu.CONSTRAINT_NAME and istc.TABLE_NAME = iskcu.TABLE_NAME WHERE istc.CONSTRAINT_TYPE = 'PRIMARY KEY' and istc.table_name = 'city')
Small Code sinppet in SQL Server to Retrive the all the fields except the Primary key field
This sql query is used to retrieve the all the field from a specified table except the primary key. You can give any othre keys like forign key, unique key, which you want to filter from this columns list.


Join the conversation! Your thoughts help the community grow.