I have created a stored procedure where I an looping through data and inserting records based on the data. The inserts have the same format, but the data changes slightly.
The decision making is handled with IF/ELSE statements, and I have a dozen or so INSERT queries (one for each decision).
I'd like to put the INSERT into a function that I can just call with the variables that change.
I would like to create the function at the beginning of the stored procedure, and then DROP it at the end.
USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[DataParser]
AS
CREATE FUNCTION [dbo].[InsertFunc]
(@Number VARCHAR(40),
@Line_No INT,
@Topic VARCHAR(30),
@Note VARCHAR(4099),
@Bool VARCHAR(1))
RETURNS bit
AS
BEGIN
INSERT INTO Processing (Number, Line_No, Topic, Note, Activation_Date, Mandatory)
VALUES (@Number, @Line_No, @Topic, @Note,
CONVERT(VARCHAR(8), GETDATE(), 1), @Mandatory)
RETURN @@ROWCOUNT
END
...
...
Decision tree
...
...
InsertFunc(@oid, @lidCounter, 'StrVal1', @strVar2, 'Y')
Right now it will not let me save the stored procedure, and if I try to create the function outside of the stored procedure, I get the following error:
Msg 443, Level 16, State 15, Procedure DataParser, Line 12 [Batch Start Line 23]
Invalid use of a side-effecting operator 'INSERT' within a function.
Any help is appreciated.