السلام عليكم دار علم وايمان وشباب تضع الملائكة باذن ربي لهم اجنحتها رضاً بما يصنعون
بين يدي سؤال تقليدي جدا "احببت الاشارة اليه حتى لا يتضايق اهل الخبرة" ولكنه عندي مهم ومستعجل فنرجو الاستعجال ما استطعتم
الســـــــــــــــــــؤال :
لدي قاعدة بيانات بها جدول به حقل ياخذ احدى القيمتين (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