Skip to content

set and reuse variables locally in sqlpage #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
f8dca opened this issue Aug 6, 2023 · 5 comments
Closed

set and reuse variables locally in sqlpage #49

f8dca opened this issue Aug 6, 2023 · 5 comments

Comments

@f8dca
Copy link

f8dca commented Aug 6, 2023

Would it be possible to add support to declaring local SQL variables and setting their value ?
something along these lines:

declare @max_inv varchar(10) ;
set  @max_inv = func_getMaxInv(); --this is a scalar function to get the latest invoice number

SELECT 'table' as component

select [id] , [invoice_no], [invoice_date], [cust_no] 
from SALES_HISTORY 
where invoice_no = isnull($invoice_no, @max_inv)

This snippet should either show the invoice passed in the parameter or the last one.

@lovasoa
Copy link
Collaborator

lovasoa commented Aug 6, 2023

That would indeed be nice ! But this will require support from sqlparser-rs first. Let's ask them !

@lovasoa
Copy link
Collaborator

lovasoa commented Aug 6, 2023

In your particular example, is there a reason you cannot use

select [id] , [invoice_no], [invoice_date], [cust_no] 
from SALES_HISTORY 
where invoice_no = isnull($invoice_no, func_getMaxInv())

?

@f8dca
Copy link
Author

f8dca commented Aug 9, 2023

Yes, I can do that as workaround in this particular case. In amore generic way I still would love to be able to use local variables so there is no need to call a function many times.

@f8dca f8dca closed this as completed Aug 9, 2023
@lovasoa lovasoa reopened this Aug 9, 2023
@lovasoa
Copy link
Collaborator

lovasoa commented Aug 9, 2023

Let's keep that open to keep track of the feature. It's something I'd like to see in SQLPage too

lovasoa added a commit that referenced this issue Sep 19, 2023
@lovasoa
Copy link
Collaborator

lovasoa commented Sep 19, 2023

SET is supported as of 0.12.0-beta, and implemented on sqlpage's side (the variable are stored in sqlpage, not in the database).

This allows for new interesting use cases, using variable values as inputs to sqlpage functions.

@lovasoa lovasoa closed this as completed Sep 19, 2023
@lovasoa lovasoa changed the title Support for DECLARE and SET statements on mssql to use scalar functions set and reuse variables locally in sqlpage Sep 19, 2023
lovasoa added a commit that referenced this issue Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants