Introduction
In this article, we learn how to concatenate a data table by using the | and ^ separators in SQL server stored procedure & assign it to a single parameter, easily passing from the backend to the frontend whenever needed.
This happens when a primary table & child table are there, and we collect the information of the primary table along with the child table, which has multiple data of the reference id of the primary table.
Here we use the below code for this with a user-defined function.
declare @vch_DeductChkList VARCHAR(512)=NULL
BEGIN
DECLARE @req_values VARCHAR(1024)='',@int_ductTypId INT=NULL,@dec_ductAmt Dec(18,2)=NULL,@dec_ductgst dec(18,2)=NULL,@dec_ductcgst dec(18,2)=NULL,@ductigst dec(18,2)=NULL,@totalDuctAmt dec(18,2)=NULL,@taxCode varchar(10)=NULL;
DECLARE @TempRefundDeductionTypes as TABLE (id smallint identity(1,1),req_Values VARCHAR(MAX))
INSERT INTO @TempRefundDeductionTypes
SELECT * FROM [UDF_Split](@vch_DeductChkList,'^');
DECLARE @loop SMALLINT=1,@count SMALLINT=0;
select @count=count(id) from @TempRefundDeductionTypes where req_Values<>''
WHILE @loop<=@count
BEGIN
select @req_values=req_values from @TempRefundDeductionTypes where id=@loop;
create table
INSERT INTO
SELECT Val FROM [dbo].[UDF_Split](@req_values,'|');
SELECT @int_ductTypId=req_value FROM
SELECT @dec_ductAmt=req_value FROM
SELECT @dec_ductgst =req_value FROM
SELECT @dec_ductcgst=req_value FROM
SELECT @ductigst=req_value FROM
SELECT @totalDuctAmt =req_value FROM
SELECT @taxCode =req_value FROM
INSERT INTO [dbo].[T_RFD_DEDUCTION_DETAILS]
([INT_DEDUCTION_TYPE_ID]
,[INT_CUSTOMER_ID]
,[INT_REFUND_ID]
,[VCH_SSC_NUMBER]
,[DEC_AMOUNT]
,[DEC_CGST]
,[DEC_SGST]
,[DEC_IGST]
,[DEC_DEDUCTION_AMOUNT]
,[INT_CREATED_BY]
,[DT_CREATED_ON]
,[INT_UPDATED_BY]
,[DT_UPDATED_ON]
,[BIT_DELETED_FLAG]
,[vch_TaxCode])
VALUES
(@int_ductTypId
,(SELECT int_CustomerId FROM T_RFD_REFUND_REQUEST WHERE int_RefId=@int_RefId and bit_DeletedFlag=0 )
,@int_RefId
,(select VCH_SSC_NUMBER from M_RFD_DEDUCTION_TYPE WHERE INT_DEDUCTION_TYPE_ID=@int_ductTypId and BIT_DELETED_FLAG=0)
,@dec_ductAmt
,@dec_ductcgst
,@dec_ductgst
,@ductigst
,@totalDuctAmt
,@int_CreatedBy
,GETDATE()
,NULL
,NULL
,0
,@taxCode)
drop table
set @loop=@loop+1;
END;
END;
User Define Function