on 2019 Mar 22 3:43 PM
How do I do this correctly? Want to use sum inside sa_rowgenerator.
CREATE TABLE t ( mat NVARCHAR(20), quantity INTEGER NOT NULL ); INSERT t VALUES ( 'ABC', 10 ); INSERT t VALUES ( 'ABC', 5 ); INSERT t VALUES ( 'ABC21', 7 ); INSERT t VALUES ( 'ABC21', 13 ); COMMIT; SELECT NoOfRovs.row_num FROM t CROSS APPLY sa_rowgenerator(1, sum(t.quantity)) NoOfRovs WHERE mat = 'ABC';
Request clarification before answering.
Have no chance to test code currently but with v16 and above, it is allowed to use a subquery as argument for a procedure, so I guess it should also be possible to use the "SELECT SUM..." directly for parameter two, right? That would avoid the cool CROSS APPLY.
FWIW, here's the according quote from the v16 What's New topic on System procedures and functions:
Subselect and subqueries supported for parameter expressions in functions and procedures.
Subselect and subqueries are supported for parameter expressions in functions and procedures.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Indeed... it works in V17, which means you can put "sum inside sa_rowgenerator" 🙂
SELECT row_num FROM sa_rowgenerator ( 1, ( SELECT SUM ( t.quantity ) FROM T WHERE mat = 'ABC' ) ); row_num ----------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 (15 rows)
User | Count |
---|---|
79 | |
21 | |
8 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.