[英]Problems with view
Good afternoon. 下午好。 Help to understand please.
请帮助理解。 there is table Purchasing.PurchaseOrderDetail, Purchasing.PurchaseOrderHeader, Production.Product in base AdventureWorks2008R2.
在基本AdventureWorks2008R2中有表Purchasing.PurchaseOrderDetail,Purchasing.PurchaseOrderHeader,Production.Product。
Create a test database in its view, which displays information from these tables as follows: 在其视图中创建一个测试数据库,该数据库显示来自这些表的信息,如下所示:
For each unit OrderQty should be a separate line. 对于每个单位,OrderQty应该是单独的一行。 If OrderQty = 5, the view will be 5 lines
如果OrderQty = 5,则视图将为5行
like this: 像这样:
OrderQty = 4, ReceivedQty = 3. Result in view: OrderQty = 4,ReceivedQty =3。查看结果:
ItemNumber ReceivedInd
1 1
2 1
3 1
4 0
I tried to use a cursor, but the database does not work. 我尝试使用游标,但是数据库无法正常工作。 How to write?
怎么写?
declare @in table(i int)
declare @out table (j int)
declare @i int
insert into @in values(7)
insert into @in values(10)
insert into @in values(5)
declare Cur cursor local fast_forward for
select i from @in
open Cur
fetch next from Cur into @i
while ( @@FETCH_STATUS = 0 )
begin
with t as
(
select 1 as val
union all
select val+1 from t where val<@i
)
insert into @out
select val from t
fetch next from Cur into @i
end
close cur
select * from @out
A cursor is not necessary (and not advisable), if you want to repeat a record n number of times, just join to a numbers table, if you don't have one you can easily create a sequence of numbers using: 游标不是必需的(并且不建议使用),如果要重复记录n次,只需加入数字表,如果没有游标,则可以使用以下方法轻松创建数字序列:
SELECT ROW_NUMBER() OVER(ORDER BY object_id) FROM sys.all_objects;
So with your simple example you can use: 因此,通过简单的示例,您可以使用:
declare @in table(i int)
insert into @in values(7)
insert into @in values(10)
insert into @in values(5)
SELECT *
FROM @in AS i
CROSS APPLY
( SELECT ROW_NUMBER() OVER(ORDER BY object_id)
FROM sys.all_objects
) AS n (Number)
WHERE i.i >= n.Number
ORDER BY i.i, n.Number;
To make the example slightly more complex: 为了使示例稍微复杂一点:
DECLARE @T TABLE (ID INT IDENTITY(1, 1), OrderQty INT, ReceivedQty INT);
INSERT @T (OrderQty, ReceivedQty) VALUES (4, 3), (5, 2), (2, 2);
SELECT t.ID,
ItemNumber = n.Number,
ReceivedInd = CASE WHEN n.Number > t.ReceivedQty THEN 0 ELSE 1 END
FROM @T AS t
CROSS APPLY
( SELECT ROW_NUMBER() OVER(ORDER BY object_id)
FROM sys.all_objects
) AS n (Number)
WHERE t.OrderQty >= n.Number
ORDER BY t.ID, n.Number;
The same query works with JOIN, and the execution plan is identical: 相同的查询适用于JOIN,并且执行计划相同:
DECLARE @T TABLE (ID INT IDENTITY(1, 1), OrderQty INT, ReceivedQty INT);
INSERT @T (OrderQty, ReceivedQty) VALUES (4, 3), (5, 2), (2, 2);
SELECT t.ID,
ItemNumber = n.Number,
ReceivedInd = CASE WHEN n.Number > t.ReceivedQty THEN 0 ELSE 1 END
FROM @T AS t
INNER JOIN
( SELECT Number = ROW_NUMBER() OVER(ORDER BY object_id)
FROM sys.all_objects
) AS n
ON t.OrderQty >= n.Number
ORDER BY t.ID, n.Number;
I think you're just trying to combine the results of GarethD's script with data from additional tables, which is not as hard as you seem to think. 我认为您只是想将GarethD脚本的结果与其他表中的数据结合起来,这并不像您想的那样难。 Example based on AdventureWorks2008 below:
以下基于AdventureWorks2008的示例:
WITH
E1(N) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4 )
SELECT
poh.PurchaseOrderID,
pod.PurchaseOrderDetailID,
p.ProductID,
p.Name,
p.ProductNumber,
poh.OrderDate,
poh.ShipDate,
n.N AS ItemNumber,
CASE WHEN n.N > pod.ReceivedQty THEN 0 ELSE 1 END AS ReceivedInd
FROM
Purchasing.PurchaseOrderDetail pod
INNER JOIN
Purchasing.PurchaseOrderHeader poh ON
pod.PurchaseOrderID = poh.PurchaseOrderID
INNER JOIN
Production.Product p ON
pod.ProductID = p.ProductID
INNER JOIN
cteTally n ON
pod.OrderQty >= n.N
ORDER BY pod.PurchaseOrderDetailID, n.N;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.