SQL с курсором:
Таблицы:
Create Table #formulas
(
f VarChar(25)
)
Insert Into #formulas Values
('A+B'),
('A*B-C'),
('X+Y-Z')
Create Table #v
(
id char(1),
v Int
)
Insert Into #v Values
('A', 10),
('B', 200),
('C', 75),
('X', 100),
('Y', 110),
('Z', 10)
Create Table #t
(
f VarChar(25),
v Float
)
Просмотр ваших записей с помощью курсора: создайте динамический оператор SQL и выполните его в таблице # t.
Declare @vStr nVarChar(50)
Declare @v1 nVarChar(20)
Declare @op1 nVarChar(20)
Declare @v2 nVarChar(20)
Declare @op2 nVarchar(20)
Declare @v3 nVarchar(20)
Declare vCursor Cursor For
Select
(Select distinct v From #v Where id = Substring(f,1,1)) As 'v1',
Substring(f,2,1) As 'op1',
(Select distinct v From #v Where id = Substring(f,3,1)) As 'v2',
Substring(f,4,1) As 'op2',
(Select distinct v From #v Where id = Substring(f,5,1)) As 'v3'
From #formulas;
Open vCursor
Fetch Next From vCursor Into
@v1,
@op1,
@v2,
@op2,
@v3
While @@FETCH_STATUS = 0
Begin
Set @vStr = Case When @v3 Is Null
Then 'Select '''+ @v1 + @op1 + @v2 + ''',(' + @v1 + @op1 + @v2 + ')'
Else 'Select '''+ @v1 + @op1 + @v2 + @op2 + @v3 + ''',(' + @v1 + @op1 + @v2 + @op2 + @v3 + ')'
End
Insert Into #t(f,v) Execute (@vStr)
Fetch Next From vCursor Into
@v1,
@op1,
@v2,
@op2,
@v3
End
Close vCursor
Deallocate vCursor
Select * From #t
Drop Table #formulas
Drop Table #v
Drop Table #t
Результат:
f v
10+200 210
10*200-75 1925
100+110-10 200