简体   繁体   中英

Add Row_number() to table variable in SQL Server 2012

I have a stored procedure which shows error on adding row_number :

alter proc xmliterate
    @xmlstring xml
as
begin
    declare @tablexml table(ids int,
                            names varchar(50),
                            parent varchar(50),
                            nodes varchar(50))

    insert into @tablexml (ids, names, parent, nodes)  
        select 
            ids = row_number() over (order by names),---error Invalid column name 'names'.
            PARENT = B.value('(.)[1]','varchar(50)'),
            names = b.value('local-name(.)','varchar(50)'),
            nodes = b.value('local-name(..)','varchar(50)')
        from 
            @xmlstring.nodes('SampleXML/*/*')A(B)

     select * from @tablexml
end

If this is not possible how can I add row_number to the result table variable?

Your names column is alias column. You should change your code, from: row_number() over (order by names) to row_number() over (order by b.value('local-name(.)','varchar(50)'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM