SET a variable inside a CTE

泪湿孤枕 提交于 2019-12-10 10:10:08

问题


This is my first time I work with SQLServer 2012, and I have this code that it doesn't work:

CREATE PROCEDURE ReadCodeBuffer
    @gint bigint,   
    @pres tinyint,
    @level bigint,
    @quantity smallint,
    @xmlGenerated xml OUTPUT
AS

    DECLARE @current_incremental bigint
    DECLARE @counter bigint
    DECLARE @xml XML

    -- Get current incremental.
    set @current_incremental = 
        (SELECT INCREMENTAL
            FROM INCREMENTAL_TABLE
            WHERE GTIN = @gint AND
              PRESENTATION = @pres AND
              LEVEL = @level)

    -- 
    with Numbers as
    (
      select row_number() over(order by 1/0) as N
      from sys.all_objects as o1 cross join
           sys.all_objects as o2
    )
    SET @xml = (
    select @gint as GINT,
           @pres as PRESENTATION,
           @level as LEVEL,
           N + @current_incremental as INCREMENTAL
    from Numbers
    where N < @quantity
    for xml path('row'), root('root'), type)

    SET @xmlGenerated = @xml
GO

There is a problem with this piece of code:

SET @xml = (
    select @gint as GINT,
           @pres as PRESENTATION,
           @level as LEVEL,
           N + @current_incremental as INCREMENTAL
    from Numbers
    where N < @quantity
    for xml path('row'), root('root'), type)

I have these three errors:

Incorrect syntax near SET.
Column name N not valid.
Object name 'Numbers' not valid.

回答1:


You need to replace SET with SELECT and it will work

so this

SET @xml = (

need to be replaced with

SELECT @xml = (


来源:https://stackoverflow.com/questions/22865743/set-a-variable-inside-a-cte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!