Here is my previous question on how to iterate a string in SQL Server:
Now how can I specifically select column names? here is my code for selecting columns:
SELECT 'Field '+CAST(ROW_NUMBER() OVER (ORDER BY ordinal_position) AS varchar(5))+': ' +
COLUMN_NAME
FROM information_schema.columns
WHERE table_Name = 'SystemDefined' and table_schema = 'schemaAsset'
Here is the output:
Field 1: Asset_No
Field 2: AssetCategory
Field 3: AssetClassification
Field 4: PurchaseType
Field 5: Department
Field 6: RespPerson
Field 7: Status
Field 8: Location
This the output I want when selecting specific column names:
Field 1: Asset_No
Field 2: AssetCategory
Field 3: AssetClassification
Field 4: PurchaseType
Field 5: Department
Field 6: RespPerson
Field 7: Status
3条答案
按热度按时间kq0g1dla1#
How about using
NOT IN
which you can use on theWHERE
clause to specify for another condition.piah890a2#
Unless I am missing something, you can just use a
WHERE
clause to exclude the column(s) you do not want:If you have multiple columns, then you can use
NOT IN ('Location', 'etc')
2vuwiymt3#
Recommend
sys.columns
instead ofINFORMATION_SCHEMA
( here's why ).