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.
| Author |
Topic |
|
mrm23
Posting Yak Master
198 Posts |
Posted - 2010-04-12 : 00:50:54
|
| hi, i have written a SP to insert rows to tables from a diff db.but this is going into infinite loop and the same vlaue is entered for all the rows(say the first record is inserted to the tables infinite times.) Can anyone suggest what may be wrong? Its a bit urgent....Here is the SP:----------------------------------------------------------------------BEGIN BEGIN TRY declare @contactid uniqueidentifier declare @companyname varchar declare @historytypeid int declare @endtime datetime declare @duration int declare @subject varchar declare @ttyperid bigint declare @rid bigint declare @title varchar(100) declare @tasktypeid int declare @flag bigint set @flag = 0 DECLARE cur CURSOR STATIC LOCAL FOR select tc.contactid,tc.companyname, th.historytypeid,th.endtime,th.duration,th.regarding,tht.name from tp.dbo.tbl_contact tc join tp.dbo.tbl_contact_history tch on tc.contactid = tch.contactid join tp.dbo.tbl_history th on th.historyid = tch.historyid join tp.dbo.tbl_historytype tht on tht.historytypeid = th.historytypeid where category is not null and th.historytypeid<>-1 OPEN cur FETCH cur INTO @contactid,@companyname,@historytypeid,@endtime,@duration,@subject,@titleprint @contactidprint @companynameprint @historytypeidprint @endtimeprint @durationprint @subjectprint @title print 'cur fetched' WHILE @@fetch_status = 0 BEGIN BEGIN BEGIN TRANSACTION set @rid = 0 select @rid = max(rid) from hc_clients where clientname = @companyname print @rid set @tasktypeid = 0 select @tasktypeid = max(rid) from hcm_task_type where title = @title if @tasktypeid is null BEGIN insert into hcm_task_type(title) values(@title) select @tasktypeid = max(rid) from hcm_task_type where title = @title and @title is not null END set @flag = @flag + 1 print @flag insert into hc_task_manager(tasktype,targetdate,reminder,duration,subject,status,tasktypeid,ttyperid,remindercheck) values (@tasktypeid,@endtime,0,@duration,@subject,0,1,@rid,0) end COMMIT TRANSACTION END FETCH cur INTO @contactid,@companyname,@historytypeid,@endtime,@duration,@subject,@title CLOSE cur DEALLOCATE cur -- print @flag END TRY BEGIN CATCH print 'Error' ROLLBACK TRANSACTION END CATCHEND |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-12 : 01:14:24
|
if you do format your code, you will see that your FETCH is outside of the cursor while loop.BEGINBEGIN TRY DECLARE @contactid uniqueidentifier DECLARE @companyname varchar DECLARE @historytypeid int DECLARE @endtime datetime DECLARE @duration int DECLARE @subject varchar DECLARE @ttyperid bigint DECLARE @rid bigint DECLARE @title varchar(100) DECLARE @tasktypeid int DECLARE @flag bigint SET @flag = 0 DECLARE cur cursor STATIC LOCAL FOR SELECT tc.contactid,tc.companyname, th.historytypeid, th.endtime, th.duration, th.regarding, tht.name FROM tp.dbo.tbl_contact tc JOIN tp.dbo.tbl_contact_history tch ON tc.contactid = tch.contactid JOIN tp.dbo.tbl_history th ON th.historyid = tch.historyid JOIN tp.dbo.tbl_historytype tht ON tht.historytypeid = th.historytypeid WHERE category IS NOT NULL AND th.historytypeid<>-1 OPEN cur FETCH cur INTO @contactid,@companyname,@historytypeid,@endtime,@duration,@subject,@title print @contactid print @companyname print @historytypeid print @endtime print @duration print @subject print @title print 'cur fetched' WHILE @@fetch_status = 0 BEGIN BEGIN BEGIN TRANSACTION SET @rid = 0 SELECT @rid = MAX(rid) FROM hc_clients WHERE clientname = @companyname print @rid SET @tasktypeid = 0 SELECT @tasktypeid = MAX(rid) FROM hcm_task_type WHERE title = @title IF @tasktypeid IS NULL BEGIN INSERT INTO hcm_task_type(title) VALUES(@title) SELECT @tasktypeid = MAX(rid) FROM hcm_task_type WHERE title = @title AND @title IS NOT NULL END SET @flag = @flag + 1 print @flag INSERT INTO hc_task_manager(tasktype,targetdate,reminder,duration,subject,status,tasktypeid,ttyperid,remindercheck) VALUES (@tasktypeid,@endtime,0,@duration,@subject,0,1,@rid,0) END COMMIT TRANSACTION END FETCH cur INTO @contactid,@companyname,@historytypeid,@endtime,@duration,@subject,@title CLOSE cur DEALLOCATE cur -- print @flagEND TRYBEGIN CATCH print 'Error' ROLLBACK TRANSACTIONEND CATCHEND Also, the query can be re-written into set-based. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mrm23
Posting Yak Master
198 Posts |
Posted - 2010-04-12 : 01:34:29
|
| Hi KHtan,Thanks for the reply. this works fine but the same data is entered for every row. it must take from the TP DB and then insert. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-12 : 01:37:59
|
quote: Originally posted by khtan if you do format your code, you will see that your FETCH is outside of the cursor while loop. KH[spoiler]Time is always against us[/spoiler]
Move the red line to after the COMMIT TRANS KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mrm23
Posting Yak Master
198 Posts |
Posted - 2010-04-12 : 01:39:19
|
| for instance, if i run the query i have written in the cursor, i get the result set like this:CompanyName | historytypeid| endtime | | Regarding|National Starch & Chemical (Thailand) Ltd | 101 | 2006-11-20 | 0 |TheerapongWechwisuthikun.docMercedes-Benz 16 |2005-05-11 09:13:00 | 0 | Subject: TMP Worldwide Recruits A New NameSappasamphat Co., Ltd - Sixsense Resort 3 2005-06-16 08:39:00 0 Status - Candidate-InactiveMazars Ltd. 101 2005-08-25 09:16:00 0 Thasanai Lekprasan.doc |
 |
|
|
mrm23
Posting Yak Master
198 Posts |
Posted - 2010-04-12 : 01:47:56
|
| i am getting:14------2006-11-20 07:49:00.000--0--0---T14------2006-11-20 07:49:00.000--0--0---T14------2006-11-20 07:49:00.000--0--0---T14------2006-11-20 07:49:00.000--0--0---T |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-12 : 01:49:08
|
did you change your query as i have suggested ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mrm23
Posting Yak Master
198 Posts |
Posted - 2010-04-12 : 02:18:58
|
| yes. it is still the same. can we use join in cursors? is it bcoz of that it is not working?also, the cursor query returns over 1 lakh records. but the sp is inserting 9 lakh records and every row has the same data....let me know what went wrong... |
 |
|
|
mrm23
Posting Yak Master
198 Posts |
Posted - 2010-04-12 : 02:34:39
|
| it is inserting into master...(hcm_task_type) after that it is going into the catch block. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-12 : 04:37:07
|
what is the primary key of hcm_task_type ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mrm23
Posting Yak Master
198 Posts |
Posted - 2010-04-13 : 01:26:51
|
| hello KHTan,I got that. the issue was with the Client DB. There the allow nulls for all the columns . So due to null values, it was going to catch block.Anyway, thanks a lot for your time :) |
 |
|
|
|
|
|
|
|