Original: SQL Server r2--uses the Digital auxiliary table (master.. Spt_values) Implement a digital field to repeat the record display
================================= copyright Notice =================================
Copyright Notice: Original article declined reprint
Please contact me via "contact email ([email protected]" in the right post.
Do not use for academic reference.
Commercial Publications, commercial printing, commercial references and other commercial purposes.
This article does not revise and improve regularly.
This article link: http://www.cnblogs.com/wlsandwho/p/5075789.html
Wall of Shame: http://www.cnblogs.com/wlsandwho/p/4206472.html
=======================================================================
Nothing to say, despise those who disregard copyright random crawl Blog Small site webmaster, Christmas, I wish you see God.
=======================================================================
=======================================================================
have never seen someone to use distinct to heavy, but have not heard who also want to generate duplicate data. Today we have opened my eyes.
On the code bar.
1 --by WLS
2 --20151224
3 --Network code risky copy and paste should be cautious
4 Usetempdb5 GO
6
7 IF object_id(N't_testbywls'N'U') is not NULL
8 DROP TABLEt_testbywls;9 CREATE TABLET_testbywls (IdINTEGER PRIMARY KEY, NameNVARCHAR( the), QtyINTEGER)
Ten GO
One
A INSERT intoT_testbywlsVALUES
-(1,'Thbytwo',3),
-(2,'wlsandwho',2)
the GO
-
- SELECT * fromt_testbywls - GO
+
- -------------------------------------------------------------------------------------------
+ withTempqty A as
at (
- SELECT Number asNum fromMaster.. Spt_valuesWHEREType='P'
- )
- SELECTA.id,a.qty fromT_testbywls A Left JOINTempqty b onB.num>0 andB.num<=A.qty - GO
Step into the line of thought
1 -------------------------------------------------------------------------------------------
2 --step0
3 SELECT Number asNum fromMaster.. Spt_valuesWHEREType='P'
4 GO
5 --Step1
6 withTempqty7 as
8 (
9 SELECT Number asNum fromMaster.. Spt_valuesWHEREType='P'
Ten )
One SELECTA.id,a.qty,b.num fromT_testbywls A Left JOINTempqty b on 1=1
A --Step2
- withTempqty - as
the (
- SELECT Number asNum fromMaster.. Spt_valuesWHEREType='P'
- )
- SELECTA.id,a.qty,b.num fromT_testbywls A Left JOINTempqty b on 1=1 andB.num>0 andB.num<=A.qty + --Step3
- withTempqty + as
A (
at SELECT Number asNum fromMaster.. Spt_valuesWHEREType='P'
- )
- SELECTA.id,a.qty fromT_testbywls A Left JOINTempqty b onB.num>0 andB.num<=A.qty - GO
Execution results
Execution plan
=======================================================================
In line with the principle of business, I asked why the victim had such a wave (wéi) fee (bèi) (Lì) (shǐ), Low (bù) (Rén) rate (Dào), violation (pò) anti (Huài) (rì) of (Nèi) (wǎ) (Tiáo) (yuē) The requirements,
Here is the question and answer.
It appears to be a problem with software architecture design and won't comment.
=======================================================================
(Please scan this for friendly support)
Scan above QR code donation
SQL Server r2--uses the Digital secondary table (master: Spt_values) Implement a digital field to repeat the record display