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 |
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-04-13 : 06:07:56
|
| hi,In oracle: Cursor c1 is select * from -----------;In sql server: Declare c1 cursor foe select * from -------;What i required is:In Oracle:for i in c1 loopupdate----set---what we have to use for the above in sql servercould anyone help me out.How to use that? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-13 : 06:16:43
|
| 1 What are you trying to do with cursor?2 Try googleing Cursor+SQL ServerMadhivananFailing to plan is Planning to fail |
 |
|
|
thiyagu_rind
Starting Member
46 Posts |
Posted - 2009-04-13 : 06:18:11
|
| Hey,If i understand correctly, you want to know how to loop through the cursor right?RegardsRT |
 |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-04-13 : 06:26:32
|
| hi,i have written like thisCREATE Procedure [dbo].[usp_PatientidUpdate](@OldPatient Varchar(255), @NewPatient Varchar(255))ASBegindeclare @temp table (temp_table_name varchar(100))-- New Script - Startdeclare c1 cursor forselect t1.Patient_id, t1.Patient_firstname, t1.Patient_lastname, t2.Patient_address1, t2.Patient_address2, t2.Patient_city, t2.Patient_zipcode, t3.State_code FROM EMRPATIENTSMASTER t1, EMRPATIENTSPERSNLINFO t2, EMRSTATESLKUP t3 WHERE t1.Patient_id = t2.Patient_id AND t2.state_id = t3.state_id-- New Script - Endinsert into @temp select C.table_namefrominformation_schema.columnsC,INFORMATION_SCHEMA.TABLES T where column_name = 'PATIENT_ID' and C.Table_name =T.Table_name and T.Table_name not in ('EMRPatientsMaster','EMRPatientsPersnlInfo','EMRPatientsPharmaInfo','EMRPatientWorkmansComp','EMRPatientInsurance','EMRPatientOtherInfo')andT.TAbLE_TYPE='BASE TABLE'declare @table_namevarchar(100)while (exists(select temp_table_name from @temp))begin select @table_name=temp_table_name from @temp exec (' update '+@table_name+' set PATIENT_ID='''+@NewPatient+''' where PATIENT_ID='''+@OldPatient+'''')delete from @temp wheretemp_table_name = @table_nameend-- New Script - Startopen c1FETCH c1 INTO @temp tableWHILE @@FETCH_STATUS = 0begin UPDATE EMRCorrespondenceHistory th SET th.RECIPIENT_FULLNAME = i.PATIENT_FIRSTNAME || i.PATIENT_LASTNAME, th.RECIPIENT_FIRSTNAME=i.PATIENT_FIRSTNAME, th.RECIPIENT_ADDRESS1 =i.PATIENT_ADDRESS1, th.RECIPIENT_ADDRESS2 =i.PATIENT_ADDRESS2, th.RECIPIENT_CITY =i.PATIENT_CITY, th.RECIPIENT_ZIP =i.PATIENT_ZIPCODE, th.RECIPIENT_STATE =i.STATE_CODE WHERE th.Patient_id = i.Patient_id -- New Script - EndFETCH c1 INTO @temp tableendCLOSE fix_data_cursorDEALLOCATE fix_data_cursorendendEnd |
 |
|
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2009-04-13 : 07:06:32
|
| Hi,this is for oracle and want this block in sql server could anyone help me outFOR i IN c1 LOOP UPDATE EMRCorrespondenceHistory th SET th.RECIPIENT_FULLNAME = i.PATIENT_FIRSTNAME || i.PATIENT_LASTNAME, th.RECIPIENT_FIRSTNAME=i.PATIENT_FIRSTNAME, th.RECIPIENT_ADDRESS1 =i.PATIENT_ADDRESS1, th.RECIPIENT_ADDRESS2 =i.PATIENT_ADDRESS2, th.RECIPIENT_CITY =i.PATIENT_CITY, th.RECIPIENT_ZIP =i.PATIENT_ZIPCODE, th.RECIPIENT_STATE =i.STATE_CODE WHERE th.Patient_id =i.Patient_id; --NEW End End Loop; |
 |
|
|
|
|
|
|
|