Home > database >  How to use dynamic variables WHILE processing the data
How to use dynamic variables WHILE processing the data

Time:12-06

Here are known to have three fixed variable @ pay1, @ pay2, @ pay2, starts to do subtraction @ total work (actual requirements and other statements to handle matters),

Declare @ total int, @ pay1 int, @ pay2 int, @ pay3 int
The set @ total=100
The set @ pay1=10
The set @ pay2=20
The set @ pay3=30
The begin
If @ total>=@ pay1
The begin
- other statements processed items...
Set @ total=@ total - @ pay1
End
The else
The begin
- other statements processed items...
Goto mark
End

If @ total>=@ pay2
The begin
- other statements processed items...
Set @ total=@ total - @ pay2
End
The else
The begin
- other statements processed items...
Goto mark
End

If @ total>=@ pay3
The begin
- other statements processed items...
Set @ total=@ total - @ pay3
End
The else
The begin
- other statements processed items...
Goto mark
End

End
Mark:

But the actual demand is the three fixed variable @ pay1, @ pay2, @ pay2 is dynamic change, may be 0 ~ N variables, want to do with WHILE statement, such as how to write, please?

CodePudding user response:

How to dynamically define @ pay1 - N, then how to use the WHILE or other statement cycle with @ total reduction @ pay1 - N this statement?

CodePudding user response:

The N of them each executes, know value, but with different values of each execution,

CodePudding user response:

Including this N can be from your table tb1, the fields in the table tb1 containing pay1, pay2, pay3 pay4... PayN, the N value is obtained by the field name quantity,
Question 1: how to DECLARE the dynamic @ PAY1 ~ N variables?
Question 2: how to use the WHILE loop, or other statements put @ total this variable to reduce the dynamic @ pay1 ~ N?
  • Related