2014-04-04 19 views
0

我有這個表:生成XML上表的列添加自定義值

CREATE TABLE [dbo].[INCREMENTAL_TABLE](
    [GTIN] [bigint] NOT NULL, 
    [PRESENTATION] [tinyint] NOT NULL, 
    [LEVEL] [bigint] NOT NULL, 
    [INCREMENTAL] [bigint] NOT NULL DEFAULT(0) 
CONSTRAINT [PK_INCREMENTAL_TABLE_1] PRIMARY KEY CLUSTERED 
(
    [GTIN] ASC, 
    [PRESENTATION] ASC, 
    [LEVEL] ASC 
) ON [PRIMARY] 

而現在,我創建一個存儲過程:

CREATE PROCEDURE MyProc 
    @gint bigint, 
    @pres tinyint, 
    @level bigint, 
    @quantity smallint 
AS 

    DECLARE @current_incremental bigint 
    DECLARE @counter bigint 

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

    -- 
    SET @counter = @current_incremental 
    WHILE ((@counter - @current_incremental) <= @quantity) 
     BEGIN 

      SET @counter = @counter + 1 
     END 


GO 

這裏面存儲過程我有用@quantity節點創建一個XML。想象一下,我有這樣的電話:

EXEC MyProc @gint = 1 @pres = 2 @level = 3 @quantity = 100 

而且,我有這個初始值:

@current_incremental = 10 

有了這些數據,我必須與這些值返回一個xml:

GTIN | PRESENTATION | LEVEL | INCREMENTAL 
-----+--------------+-------+------------ 
    1 |  2  | 3 | 10 
-----+--------------+-------+------------ 
    1 |  2  | 3 | 11 
-----+--------------+-------+------------ 
    1 |  2  | 3 | 12 

[ ... ] 
-----+--------------+-------+------------ 
    1 |  2  | 3 | 109 

但我不打算把它插入表格。

如果我無法通過選擇該表格來獲取這些數據,我該如何獲得XML?

回答

0

我已經修改Mikael Erikssonanswer適合我的需要:

declare @gint bigint = 1; 
declare @pres tinyint = 2; 
declare @level bigint = 3; 
declare @quantity smallint = 100; 
declare @current_incremental bigint = 20000000; 

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 
) 
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 
1

您可以使用數字表或其他具有足夠行的表。

declare @gint int = 1; 
declare @pres int = 2; 
declare @level int = 3; 
declare @quantity int = 100; 
declare @current_incremental int = 10; 

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 
) 
select @gint as GINT, 
     @pres as PRESENTATION, 
     @level as LEVEL, 
     N as INCREMENTAL 
from Numbers 
where N >= @current_incremental and 
     N < @current_incremental + @quantity 
for xml path('row'), root('root'), type 

結果:

<root> 
    <row> 
    <GINT>1</GINT> 
    <PRESENTATION>2</PRESENTATION> 
    <LEVEL>3</LEVEL> 
    <INCREMENTAL>10</INCREMENTAL> 
    </row> 
    <row> 
    <GINT>1</GINT> 
    <PRESENTATION>2</PRESENTATION> 
    <LEVEL>3</LEVEL> 
    <INCREMENTAL>11</INCREMENTAL> 
    </row> 
    . 
    . 
    . 
    . 
    <row> 
    <GINT>1</GINT> 
    <PRESENTATION>2</PRESENTATION> 
    <LEVEL>3</LEVEL> 
    <INCREMENTAL>108</INCREMENTAL> 
    </row> 
    <row> 
    <GINT>1</GINT> 
    <PRESENTATION>2</PRESENTATION> 
    <LEVEL>3</LEVEL> 
    <INCREMENTAL>109</INCREMENTAL> 
    </row> 
</root> 
+0

謝謝您的回答。我有一個問題:什麼是數字?它是數字表嗎? – VansFannel

+0

@VansFannel在我的答案中的數字是一個CTE,而不是一個數字表。 CTE使用兩個相當大的表之間的交叉連接來提供足夠的行。如果你不想使用CTE,你可以創建一個你自己的數字表,它只有一列從1(或0)編號到你需要的多行。 –

+0

@VansFannel更多關於數字表的信息[here](http://www.sqlservercentral.com/articles/T-SQL/62867/) –

相關問題