الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

سؤال في الكيرسور

بدأه الحلفاوي في 2 يونيو 2012 · 1 رد · 507 مشاهدة · في قواعد بيانات Microsoft SQL Server
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم دار علم وايمان وشباب تضع الملائكة باذن ربي لهم اجنحتها رضاً بما يصنعون

بين يدي سؤال تقليدي جدا "احببت الاشارة اليه حتى لا يتضايق اهل الخبرة" ولكنه عندي مهم ومستعجل فنرجو الاستعجال ما استطعتم

الســـــــــــــــــــؤال :

لدي قاعدة بيانات بها جدول به حقل ياخذ احدى القيمتين (1/0) هذا الحقل ياخذ القيمة 1 عندما يكون الحقل

الزمني هو اكبر تاريخ تم ادخاله فيما تبقى بقية السجلات لنفس الID في الحقل المعني 0

كتبت هذه السطور (كيرسور) ولكن لم افلح

First cusor code

 set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER  PROCEDURE [dbo].[Set_Active_Qist_2]
	-- Add the parameters for the stored procedure here
--(@ByDays_Monthes_Remaind int,
--@LowLimitSpin int,
--@MaxLimitSpin int)
as
Begin

declare	    @Cust_ID int;
-- Create first cursor here
declare  GetLastQist_Date cursor for
Select Distinct Customer_Id from Query11 where TypeID= 1 and ContractTypeID = 0

Update customers set Qist_Activity = 0

open  GetLastQist_Date

fetch next from GetLastQist_Date into  @Cust_ID
while @@Fetch_Status = 0    
BEGIN

Update customers set Qist_Activity = 1 where Qist_begin_date = (select max(Qist_begin_date) from customers where Customer_Id =@Cust_Id)
--------------------------Fetch next outer cursor---------------------------------  
fetch next from GetLastQist_Date into  @Cust_ID
--------------------------------------------------------------------------------------------       
END
--   Deallocate cursor----------------------------------------------------------------
close  GetLastQist_Date;
Deallocate GetLastQist_Date; 
--------------------------------------------------------------------------------------------         
end

Second try cusor code

 set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER  PROCEDURE [dbo].[Set_Active_Qist]
	-- Add the parameters for the stored procedure here
--(@ByDays_Monthes_Remaind int,
--@LowLimitSpin int,
--@MaxLimitSpin int)
as
Begin

declare	    @Cust_ID int, 
            @Conract_ID Varchar , 
--            @Qualify_ID int ,
--            @Years int,
            @Last_Qist_Date DateTime;
-- Create first cursor here
declare  GetLastQist_Date cursor for
Select Distinct Customer_Id,Contract_ID,max(Qist_begin_date) from customers group by Customer_Id,Contract_ID

--Update customers set Qist_Activity = 0

open  GetLastQist_Date

fetch next from GetLastQist_Date into  @Cust_ID,@Conract_ID,@Last_Qist_Date
while @@Fetch_Status = 0    
BEGIN

Update customers set Qist_Activity = 1 where Customer_Id=@Cust_ID and Contract_ID = @Conract_ID and Qist_begin_date = @Last_Qist_Date
--------------------------Fetch next outer cursor---------------------------------  
fetch next from GetLastQist_Date into  @Cust_ID,@Conract_ID,@Last_Qist_Date
--------------------------------------------------------------------------------------------       
END
--   Deallocate cursor----------------------------------------------------------------
close  GetLastQist_Date;
Deallocate GetLastQist_Date; 
--------------------------------------------------------------------------------------------         
end
#2

لمل لا تجر بهذا الكود

اول كود
Update customers set Qist_Activity = 0
يلي هذا الكود
Update customers set Qist_Activity = 1 
FROM
	customers 
	INNER JOIN 
	(Select Customer_Id,Contract_ID,max(Qist_begin_date) AS maxQist_begin_date from customers group by Customer_Id,Contract_ID) AS t
	ON
	customers.Customer_Id=t.Customer_Id
	and
	customers.Contract_ID=t.Contract_ID
          and
	customers.Qist_begin_date=t.maxQist_begin_date

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…