我有这张桌子:
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 :(得分: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>
答案 1 :(得分:0)
我修改了Mikael Eriksson的answer以满足我的需求:
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