SQL Server 2008 R2。寻找直接的T-SQL代码作为我可以实现的解决方案。我理解使用while循环的概念过程代码,如果是其他的话,但我没有在生产环境中正式编写一行t-sql代码。 TIA
为了完成表单,我想将最多六行数据转换为一行数据,但是返回的行数不会超过六行并插入到另一个表的一行中。原因(我相信)我想这样做是因为我插入值的表的结构已经知道所以每次动态创建这个表的'价格'将是资源昂贵的。如果这个假设是错误的,请纠正我,但是在这里给出了参数,在每次用户调用表单完成时都有一个预先存在的表结构的惩罚似乎是要走的路?
例如,假设从查询返回的数据(如上所述)最多可返回六行,此处它只返回五行数据,它们是:
Date1 Date2 Code# Location Who
08/22/2014 08/22/2014 1001 Los Angeles Beethoven
08/22/2014 08/22/2014 1002 Munich Brahms
08/22/2014 08/22/2014 1002 Hapsburg Bruckner
08/22/2014 08/22/2014 1004 Vienna Bach
08/22/2014 08/22/2014 1005 Los Angeles Schoenberg
表中要插入上述值的列名是:
Date11,Date12,..... Date16,Date21,Date22,Date26,Code#1,Code#2 ..... Who5,Who6
在此示例中,插入的表总共有30列。我的“现实世界”情况有更多的专栏,但不是更多。要插入到(1x30)行中的(6X5)行的数量是可变的,并且可以是从一个到相当大的数量。这些将被处理,然后我将清除表格直到下次用户要求填写该表格。
如果我认为我想要实现它的方式完全超出了“最佳实践”的范围,我愿意接受建议。但是,如果我的方法并不那么糟糕,我很乐意看到t-sql while循环使用我的示例中的数据来完成它。非常感谢,Stuart
答案 0 :(得分:0)
DECLARE @1billing int, @1sd1 date, @1sd2 date, @1location int, @1mod1 int, @1mod2 int, @1mod3 int, @1mod4 int, @1units int, @1amount money, @1cptcode varchar(64), @2billing int,
@ 2sd1 date,@ 2sd2 date,@ 2location int,@ 2mod1 int,@ 2mod2 int,@ 2mod3 int,@ 2mod4 int,@ 2units int,@ 2amount money,@ 2cptcode varchar(64),@ 3billing int, @3sd1日期,@ 3sd2 date,
@ 3location int,@ 3mod1 int,@ 3mod2 int,@ 3mod3 int,@ 3mod4 int,@ 3units int,@ 3amount money,@ 3cptcode varchar(64),@ 4billing int,@ 4sd1 date,@ 4sd2 date,@ 4location int ,
@ 4mod1 int,@ 4mod2 int,@ 4mod3 int,@ 4mod4 int,@ 4units int,@ 4amount money,@ 4cptcode varchar(64),@ 5billing int,@ 5sd1 date,@ 5sd2 date,@ 5location int,@ 5mod1 int ,@ 5mod2 int,
@ 5mod3 int,@ 5mod4 int,@ 5units int,@ 5amount money,@ 5cptcode varchar(64),@ 6billing int,@ 6sd1 date,@ 6sd2 date,@ 6location int,@ 6mod1 int,@ 6mod2 int,@ 6mod3 int ,@ 6mod4 int,
@ 6units int,@ 6amount money,@ 6cptcode varchar(64)DECLARE HCFAcursor CURSOR for SELECT dbo。[Billing Detail] .Billing,dbo。[Billing Detail]。[Service Date 1],
dbo。[结算明细]。[服务日期2],dbo。[结算明细]。[位置代码],
dbo。[Billing Detail]。[修改器1],dbo。[Billing Detail]。[修改器2],
dbo。[Billing Detail]。[修改器3],dbo。[Billing Detail]。[修改器4],dbo。[Billing Detail] .Units,
dbo。[Billing Detail] .Extended,dbo。[Billing Detail]。[交易代码]
来自[账单明细] INNER JOIN
患者ON dbo。[Billing Detail] .Chart = dbo.Patient。[图表编号]
GROUP BY dbo。[Billing Detail] .Billing,dbo。[Billing Detail]。[服务日期1],
dbo。[结算明细]。[服务日期2],dbo。[结算明细]。[位置代码],
dbo。[Billing Detail]。[修改器1],dbo。[Billing Detail]。[修改器2],
dbo。[Billing Detail]。[修改器3],dbo。[Billing Detail]。[修改器4],dbo。[Billing Detail] .Units,
dbo。[Billing Detail] .Extended,dbo。[Billing Detail]。[交易代码]
HAVING(dbo。[Billing Detail] .Billing = 2)AND
(dbo。[Billing Detail]。[服务日期1] = dbo。[Billing Detail]。[服务日期2])
接下来打开HCFAcursor FETCH
来自HCFACursor
INTO @ 1billing,@ 1sd1,@ 1sd2,@ 1location,@ 1mod1,@ 1mod2,@ 1mod3,@ 1mod4,@ 1units,@ 1amount,@ 1cptcode FETCH next
来自HCFACursor
INTO @ 2billing,@ 2sd1,@ 2sd2,@ 2location,@ 2mod1,@ 2mod2,@ 2mod3,@ 2mod4,@ 2units,@ 2amount,@ 2cptcode FETCH next
来自HCFACursor
INTO @ 3billing,@ 3sd1,@ 3sd2,@ 3location,@ 3mod1,@ 3mod2,@ 3mod3,@ 3mod4,@ 3units,@ 3amount,@ 3cptcode FETCH next
来自HCFACursor
INTO @ 4billing,@ 4sd1,@ 4sd2,@ 4location,@ 4mod1,@ 2mod2,@ 4mod3,@ 4mod4,@ 4units,@ 4amount,@ 4cptcode FETCH next
来自HCFACursor
INTO @ 5billing,@ 5sd1,@ 5sd2,@ 5location,@ 5mod1,@ 5mod2,@ 5mod3,@ 5mod4,@ 5units,@ 5amount,@ 5cptcode FETCH next
来自HCFACursor
INTO @ 6billing,@ 6sd1,@ 6sd2,@ 6location,@ 6mod1,@ 6mod2,@ 6mod3,@ 6mod4,@ 6units,@ 6amount,@ 6cptcode
INSERT
INTO [dbo] .HCFALineitems(Item1Date1,Item1Date2,Item1POS,Item1Mod1,Item1Mod2,Item1Mod3,Item1Mod4,Item1Units,Item1Charge,item1cpt,Item2Date1,Item2Date2,
Item2POS,Item2Mod1,Item2Mod2,Item2Mod3,Item2Mod4,Item2Units,Item2Charge,item2cpt,Item3Date1,Item3Date2,Item3POS,Item3Mod1,Item3Mod2,Item3Mod3,
Item3Mod4,Item3Units,Item3Charge,item3cpt,Item4Date1,Item4Date2,Item4POS,Item4Mod1,Item4Mod2,Item4Mod3,Item4Mod4,Item4Units,Item4Charge,item4cpt,
Item5Date1,Item5Date2,Item5POS,Item5Mod1,Item5Mod2,Item5Mod3,Item5Mod4,Item5Units,Item5Charge,item5cpt,Item6Date1,Item6Date2,Item6POS,Item6Mod1,
Item6Mod2,Item6Mod3,Item6Mod4,Item6Units,Item6Charge,item6cpt)
价值观(@ 1sd1,@ 1sd2,@ 1location,@ 1mod1,@ 1mod2,@ 1mod3,@ 1mod4,@ 1units,@ 1amount,@ 1cptcode,@ 2sd1,@ 2sd2,@ 2location,@ 2mod1,@ 2mod2,@ 2mod3, @ 2mod4,@ 2台,@ 2amount,@ 2cptcode,@ 3sd1,@ 3sd2,@ 3location,@ 3mod1,@ 3mod2,@ 3mod3,@ 3mod4,@ 3台,@ 3amount,@ 3cptcode,@ 4sd1,@ 4sd2,@ 4location @ 4mod1,@ 4mod2,@ 4mod3,@ 4mod4,@ 4units,@ 4amount,@ 4cptcode,@ 5sd1,@ 5sd2,@ 5location,@ 5mod1,@ 5mod2,@ 5mod3,@ 5mod4,@ 5units,@ 5amount,@ 5cptcode,@ 6sd1,@ 6sd2,@ 6location,@ 6mod1,@ 6mod2,@ 6mod3,@ 6mod4,@ 6单位,@ 6amount,@ 6cptcode)
关闭HCFAcursor DEALLOCATE HCFAcursor