sql server - Can I turn a "with" query into a create procedure? -


    pincounts      (         select gameid, tb.bowlerid, tb.firstname, frame, bowlingball1, bowlingball2, bowlingball3          ,framestatus  = case                             when bowlingball1 = 10 2                             when bowlingball1 + bowlingball2 = 10 1                         else 0                         end          ,next1  = lead(bowlingball1, 1                       ,case                            when frame = 10 , bowlingball1 = 10                                 isnull(nullif(bowlingball2, 10), bowlingball3)                            when frame = 10 , bowlingball1 + bowlingball2 = 10                                 bowlingball3                            end                        ) on                         (                              partition gameid, tb.bowlerid                             order frame                          )          ,next2  = lead(bowlingball2, 1                   ,bowlingball3                          ) on                          (                           partition gameid, tb.bowlerid                           order frame                          )          ,next3  = lead(bowlingball1, 2) on                          (                          partition gameid, tb.bowlerid                          order frame                          )                     tbowlingscores bs                          left outer join tbowlers tb                          on(bs.bowlerid = tb.bowlerid)                           ),      totalscore     (       select gameid, tb.bowlerid, tb.firstname, frame, bowlingball1, bowlingball2, bowlingball3              ,framestatus, next1, next2, next3              ,framepins = case                               when bowlingball1 = 10                                    bowlingball1 + next1 + isnull(next2, next3)                               when bowlingball1 + bowlingball2 = 10                                    bowlingball1 + bowlingball2 + next1                               else bowlingball1 + bowlingball2                               end                      pincounts pc                          left outer join tbowlers tb                          on(pc.bowlerid = tb.bowlerid)                           ),         setscores    (    select gameid, bowlerid, frame, bowlingball1, bowlingball2, bowlingball3           ,totalscore = case                             when (framestatus = 0 , bowlingball2 not null)                                    or                                  (framestatus = 1 , next1 not null)                                    or                                  (framestatus = 2 , isnull(next2, next3) not null)                             sum(framepins) on                                   (                                    partition gameid, bowlerid                                    order frame                                    rows unbounded preceding                                   )                             end                   totalscore                  )  select tb.bowlerid, firstname, lastname, totalscore  setscores  ss       left outer join tbowlers tb         on(ss.bowlerid = tb.bowlerid)  frame = 10  group tb.bowlerid          ,firstname          ,lastname          ,totalscore 

below code sp "with" , @pframe parameter clause

create proc spwith  @pframe int = 10 begin    pincounts      (         select gameid, tb.bowlerid, tb.firstname, frame, bowlingball1, bowlingball2, bowlingball3          ,framestatus  = case                             when bowlingball1 = 10 2                             when bowlingball1 + bowlingball2 = 10 1                         else 0                         end          ,next1  = lead(bowlingball1, 1                       ,case                            when frame = 10 , bowlingball1 = 10                                 isnull(nullif(bowlingball2, 10), bowlingball3)                            when frame = 10 , bowlingball1 + bowlingball2 = 10                                 bowlingball3                            end                        ) on                         (                              partition gameid, tb.bowlerid                             order frame                          )          ,next2  = lead(bowlingball2, 1                   ,bowlingball3                          ) on                          (                           partition gameid, tb.bowlerid                           order frame                          )          ,next3  = lead(bowlingball1, 2) on                          (                          partition gameid, tb.bowlerid                          order frame                          )                     tbowlingscores bs                          left outer join tbowlers tb                          on(bs.bowlerid = tb.bowlerid)                           ),      totalscore     (       select gameid, tb.bowlerid, tb.firstname, frame, bowlingball1, bowlingball2, bowlingball3              ,framestatus, next1, next2, next3              ,framepins = case                               when bowlingball1 = 10                                    bowlingball1 + next1 + isnull(next2, next3)                               when bowlingball1 + bowlingball2 = 10                                    bowlingball1 + bowlingball2 + next1                               else bowlingball1 + bowlingball2                               end                      pincounts pc                          left outer join tbowlers tb                          on(pc.bowlerid = tb.bowlerid)                           ),         setscores    (    select gameid, bowlerid, frame, bowlingball1, bowlingball2, bowlingball3           ,totalscore = case                             when (framestatus = 0 , bowlingball2 not null)                                    or                                  (framestatus = 1 , next1 not null)                                    or                                  (framestatus = 2 , isnull(next2, next3) not null)                             sum(framepins) on                                   (                                    partition gameid, bowlerid                                    order frame                                    rows unbounded preceding                                   )                             end                   totalscore                  )  select tb.bowlerid, firstname, lastname, totalscore  setscores  ss       left outer join tbowlers tb         on(ss.bowlerid = tb.bowlerid)  frame = @pframe  group tb.bowlerid          ,firstname          ,lastname          ,totalscore  end 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -