Home > Enterprise >  Change dynamically generated view culumn's datatype
Change dynamically generated view culumn's datatype

Time:01-27

I've found solutions how to change view's column datatypes which are created basing on existing table's fields (cast method). But how implement this (or another) method to the column which is generated like this by (row_number)

Existing Code

CREATE VIEW v_Test

AS
SELECT

ROW_NUMBER() OVER( ORDER BY [Name] ) AS [ID], -- becomes bigint by default
[Name]

FROM [DB].[dbo].[TestTable]

By default ID column created as a bigint type. I need to have it as a int type

CodePudding user response:

ROW_NUMBER is documented as using bigint as its return [data] type. If you want to change that data type, you need to wrap the function in a CAST/CONVERT.

cast(ROW_NUMBER() OVER( ORDER BY [Name] ) as int) 

should work.

  • Related