Good day,
I'm figuring out a legacy procedure and ran into something, which should be easy but does not compute with me, at least not today.
The logic I'm after is I have values in table1, which I wish to feed to stored procedure proc1 and save the results to Table2.
Currently this is called by different users manually filling in the strings which populate the variables. working syntax is for instance:
exec [dbo].[Proc1] @var1=N'10010015',@var2='2008-03-31 00:00:00', @var3=50000
I tried something like
declare var1 varchar, var2 varchar, var3 int
insert into table2 (result1, result2, result3)
Select * from
[dbo].[Proc1] (
select @var1 = var1_tab1,
@var2 = var2_tab1
from table1
)
With regular function I'd just feed it the variables in correct order, but the procedure requires the @var1 formats. And I am not allowed to alter the function.
Previous try just gives me incorrect syntax. Any ideas how to approach this sort of issue would be appreciated. .
**Edit: **
Yeah, it's a duplicate. Or at least I used the other to create solution.
Way too much writing. Too long did not read. The following below worked.
Things that I did not understand, but now make sense:
-You can execute procedures without explicitly setting the variables as long as input is same order as the procedure definition.
-You can just insert the results to table because return (from this specific procedure) is select.
DECLARE @var1 int,
@var2 int,
@var3 float,
@var4 datetime,
DECLARE cur CURSOR FOR SELECT var1, var2, var3, var4 FROM table1
OPEN cur
FETCH NEXT FROM cur INTO @var1, @var2, @var3, @var4
WHILE @@FETCH_STATUS = 0 BEGIN
insert into table2 EXEC [procedure] @var1, @var2, @var3, 0, '', NULL, @var4
FETCH NEXT FROM cur INTO @var1, @var2, @var3, @var4
END
CLOSE cur
DEALLOCATE cur