1

这是我的代码:

SELECT 'Fields 1: ' + COLUMN_NAME FROM information_schema.columns WHERE table_Name = 'SystemDefined' and table_schema = 'schemaAsset'

输出是:

Fields 1: Asset_No
Fields 1: AssetCategory
Fields 1: AssetClassification
Fields 1: PurchaseType
Fields 1: Department
Fields 1: RespPerson
Fields 1: Status
Fields 1: Location

我希望字符串迭代,以便输出为:

Fields 1: Asset_No
Fields 2: AssetCategory
Fields 3: AssetClassification
Fields 4: PurchaseType
Fields 5: Department
Fields 6: RespPerson
Fields 7: Status
Fields 8: Location

我可以使用什么算法使这个输出成为可能?

4

1 回答 1

0

在 SQL-Server 2005+ 中,您可以使用函数ROW_NUMBER()

SELECT 'Fields '+CAST(ROW_NUMBER() OVER (ORDER BY column_name) AS varchar(5))+': ' + COLUMN_NAME
FROM information_schema.columns 
WHERE table_Name = 'SystemDefined' and table_schema = 'schemaAsset'
ORDER BY COLUMN_NAME
于 2013-02-26T08:29:48.607 回答