| Author |
Topic |
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-09-13 : 09:19:22
|
Hi everyoneI have been using the below logic to insert a single record from one table(CustomerInfo) into another table(Customerrenewal).How can i use the same logic to insert n number of records into customerrenewal table from customerinfo table based on calculating diffrence of months between two datetime feilds in the customerinfo tablethe number of times n should be calculated based on the belowselect datediff(month, orgeffdt,nxtrnwldt) from customerinfowhere requetno in (111111 )inserting a record logicSET NOCOUNT ONGO DECLARE @oldrequestid varchar(50),@newrequestid varchar(50)DECLARE db_cursor CURSOR FOR SELECT Reqno from customerinfo where reqno in (111111)OPEN db_cursor FETCH NEXT FROM db_cursor INTO @oldrequestidupdate requesttableset key_id = key_id + 1 where table_name = 'adhocrequests'Select @newrequestid = key_id from requesttable where table_name = 'adhocrequests'WHILE @@FETCH_STATUS = 0 BEGIN --do work here INSERT INTO [UnderWritin].[dbo].[customerrenewal] ([reqno] ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population] ,[expirydate]) SELECT @newrequestid ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population]) FROM [UnderWriting].[dbo].[customerinfo]where reqno = @oldrequestid |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-09-13 : 09:21:14
|
| in the above iam using the belo code to update the reqno in a table everytime a record is insertedOPEN db_cursor FETCH NEXT FROM db_cursor INTO @oldrequestidupdate requesttableset key_id = key_id + 1 where table_name = 'adhocrequests'Select @newrequestid = key_id from requesttable where table_name = 'adhocrequests' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 10:15:44
|
you dont need cursor here. the above code can be simplifies asDECLARE @newrequestid varchar(50)update requesttableset key_id = key_id + 1 where table_name = 'adhocrequests'Select @newrequestid = key_id from requesttable where table_name = 'adhocrequests' INSERT INTO [UnderWritin].[dbo].[customerrenewal] ([reqno] ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population] ,[expirydate]) SELECT @newrequestid ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population]) FROM [UnderWriting].[dbo].[customerinfo]where reqno = 111111 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-09-13 : 10:27:25
|
| Thank you.how would i use the logic in insert n number of records baased on the diffrence of two datetime feilds |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 10:29:02
|
| difference of which two datetime fields? and what should be exact logic?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-09-13 : 10:37:18
|
using the below datediff logic i would need to run the insert logic n number of times to insert n number of records in the customerrenewal tableselect datediff(month, orgeffdt,nxtrnwldt) from customerinfowhere requetno in (111111) so say the datediff between the two feilds is 10 i should be able to insert 10 records in the customerrenewal table using the below logicDECLARE @newrequestid varchar(50)update requesttableset key_id = key_id + 1 where table_name = 'adhocrequests'Select @newrequestid = key_id from requesttable where table_name = 'adhocrequests' INSERT INTO [UnderWritin].[dbo].[customerrenewal] ([reqno] ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population] ,[expirydate]) SELECT @newrequestid ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population]) FROM [UnderWriting].[dbo].[customerinfo]where reqno = 111111 quote: Originally posted by visakh16 difference of which two datetime fields? and what should be exact logic?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 10:39:19
|
| ok thats fine. But you'll end up putting same value for reqno field. is that what desire? whats the PK of the table customer renewal?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-09-13 : 10:43:28
|
nope.we should always increment the requestid feild in the requesttableand use the newrequestid for inserting a new recordreqno is the primarykey in customerrenewal tablequote: Originally posted by visakh16 ok thats fine. But you'll end up putting same value for reqno field. is that what desire? whats the PK of the table customer renewal?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 10:50:30
|
then your current logic wont work. you're just incrementing it in table once (i didnt understand why you're changing it in table in first place) and then using it afterin my opinion. you should get maxid from table then keep on incrementing based on record count to generate next sequnetial valuesie something likeDECLARE @newrequestid varchar(50)Select @newrequestid = max(key_id) from requesttable where table_name = 'adhocrequests' INSERT INTO [UnderWritin].[dbo].[customerrenewal] ([reqno] ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population] ,[expirydate]) SELECT @newrequestid + (v.number-1) ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population]) FROM [UnderWriting].[dbo].[customerinfo] cCROSS JOIN master..spt_values vwhere reqno = 111111and v.number BETWEEN 1 AND datediff(month, orgeffdt,nxtrnwldt) and v.type='p' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-09-13 : 10:55:19
|
| I was never sure that my initial cursor logic would work for the repetetive insertions based on the count from the datediffI will test the logic that you have given and let you knowThank you for this |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 11:12:05
|
| ok...will wait for you to come back------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-09-13 : 12:41:40
|
The query executed scuccessfully but it did not insert records into the customerrenewal table and also did not increment the adhocrequests in the requesttableWhat might be going wrong?quote: Originally posted by visakh16 then your current logic wont work. you're just incrementing it in table once (i didnt understand why you're changing it in table in first place) and then using it afterin my opinion. you should get maxid from table then keep on incrementing based on record count to generate next sequnetial valuesie something likeDECLARE @newrequestid varchar(50)Select @newrequestid = max(key_id) from requesttable where table_name = 'adhocrequests' INSERT INTO [UnderWritin].[dbo].[customerrenewal] ([reqno] ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population] ,[expirydate]) SELECT @newrequestid + (v.number-1) ,[receiveddate] ,[neededby] ,[busneed] ,[rptcomments] ,[reportcontent] ,[reportfrequency] ,[sbotext] ,[includeprior] ,[clmsrvdt] ,[clmsrvdtfrom] ,[clmsrvdtthru] ,[clmpddt] ,[clmpddtfrom] ,[clmpddtthru] ,[spclhnlg] ,[orgeffdt] ,[nxtrnwldt] ,[lstrnwldt] ,[enmem] ,[ReqSummary] ,[EligFromDate] ,[EligThruDate] ,[InclPHI] ,[BeneTypeMed] ,[BeneTypeMH] ,[BeneTypeRx] ,[BeneTypeSA] ,[BeneTypeOth] ,[BeneEffDate] ,[Population]) FROM [UnderWriting].[dbo].[customerinfo] cCROSS JOIN master..spt_values vwhere reqno = 111111and v.number BETWEEN 1 AND datediff(month, orgeffdt,nxtrnwldt) and v.type='p' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 13:07:11
|
| see if you've required records in [UnderWriting].[dbo].[customerinfo] table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-09-13 : 13:33:27
|
| the requestid which i used to test exists in the customerinfo table.1 record is inserted into the customerrenewal tablebut 2 issues are noted1.It did not increment the adhocrequests in the requesttable(this is important step of the process because the next records requestid is fetched from this table)2.because it did not increment the requesttable may be it is not able to insert another record as it is creating duplicates |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 13:35:10
|
| 1. if this is a part of your requirement include earlier update2. try it after including update too------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2012-09-13 : 13:45:21
|
| i am not sure what you meant by earlier update |
 |
|
|
|
|
|