Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Help

Author  Topic 

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2014-07-31 : 11:43:44
Hi All
I have a query to insert records into a table based on a request but the query can only read one record at a time . How do i change the query such that it is able to read multiple records.

In the below query i was able to input only 1 request which is 149906.


Query

declare @num_of_times int
declare @Count INT

DECLARE @newrequestid varchar(50)


DECLARE @Frequency VARCHAR(25), @RequestId INT, @x INT, @Max INT, @RptDesc INT
SET @RequestId = 149906
SET @x = 1


CREATE TABLE #RequestReports (xid int identity(1,1), RptDesc INT)

INSERT INTO #RequestReports (RptDesc)
SELECT RptDesc FROM selected_report where RequestId = @RequestId



SELECT @Frequency = defineschedule from report_request where requestid = @RequestId

IF (@Frequency = 'Quarterly')
BEGIN
select @num_of_times = ISNULL(datediff(month, receiveddate,expirationdate)/3, 0) from report_request where requestid = @RequestId
IF @num_of_times >= 0 SET @num_of_times = @num_of_times + 1
END
ELSE IF (@Frequency = 'Monthly')
BEGIN
select @num_of_times = ISNULL(datediff(month, receiveddate,expirationdate), 0) from report_request where requestid = @RequestId
IF @num_of_times >= 0 SET @num_of_times = @num_of_times + 1
END
ELSE
BEGIN
SET @num_of_times = 0
END



IF @num_of_times > 0
BEGIN

SELECT @Max = MAX(XID) FROM #RequestReports

WHILE (@x <= @Max)
BEGIN

SELECT @RptDesc = RptDesc FROM #RequestReports WHERE XID = @x


set @count = 1

WHILE @count <= @num_of_times
BEGIN

update table_keys
set key_id = key_id + 1
where table_name = 'adhoc'

Select @newrequestid = key_id from table_keys where table_name = 'adhoc'


INSERT INTO [UnderWriting].[dbo].[renoffcyc]
([roc_id]
,[roc_ind]
,[fundtype]
,[prodmth]
,[opa_id]
,[hlthplnid]
,[targetdt]
,[actualdt]
,[freqcd] )
SELECT @newrequestid
,'O'
,RR.fundingtype
,case
when @Frequency = 'Quarterly' Then convert(varchar(30),DATEADD (month , @count*3 ,RR.Receiveddate))
When @Frequency = 'Monthly' Then convert(varchar(30),DATEADD (month , @count+1 ,RR.Receiveddate))

End
,SR.Assignto
,RR.hlthplnid
,''
,''
,RR.defineschedul


FROM [UnderWriting].[dbo].[REPORT_REQUEST] RR INNER JOIN SELECTED_REPORT SR
ON RR.requestid = SR.requestid
where RR.requestid = @RequestId AND SR.RptDesc = @RptDesc

set @count = @count+1

END

SET @x = @x + 1
END

END

DROP TABLE #RequestReports



Thanks in advance

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2014-08-01 : 03:13:29
You are declaring the SET @RequestId = 149906 and pulling out the
records for this rquestID only .If you want to process all requestid exists in your table (I am assuming) u need to use While loop to process each requestID one by one from that table and load the corrosponding records into your destination table. We can help you more if you can provide use the sample data with your expected result set.

Vijay is here to learn something from you guys.
Go to Top of Page
   

- Advertisement -