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

استشارة في بناء ال where statement

مغلق
بدأه ابو اسماء في 20 يونيو 2003 · 4 رد · 896 مشاهدة · في قواعد بيانات Microsoft SQL Server
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

الاخوة الكرام ، واخص منهم الاخ مدني المحترم ،

هذا كود استعلام في sql 2000 ، استغنيت عنه لانني لم اتمكن من الحصول على كل من output paras:

TotalRemainingAmount

CountRec

الا اذا كتبتها في جدول insert into ، يرجى مراجعة الكود واعطاء النصيحة .

*********************************************

ALTER        PROCEDURE sp_qryReportsInstl
    @CustomerName varchar(100)= Null,
    @CustomerNo bigint= Null,
    @BranchNo int = Null ,
    @MorNo bigint = Null ,

    @ChequeDueDateFrom datetime= Null,--'01-01-1980',
    @ChequeDueDateTo datetime = Null,--'01-01-2050',

    @RemainingAmountFrom float = 0,
    @RemainingAmountTo float = 5000000000,

    @MorabCatCode int = Null ,
    @ChequeStatus int = Null,
    @ShowResutlt int = 1,
    @TotalRemainingAmount float OUTPUT,
    @CountRec float OUTPUT

AS
Declare @strWhere varchar(1000)
	SELECT @strWhere = 'BranchNo > 0'
	if @CustomerName is not Null select @strWhere = @strWhere + ' AND CustomerName like ''%' + @CustomerName + '%'''
	--if @CustomerName is not Null select @strWhere = @strWhere + ' AND CustomerName = ''' + @CustomerName + ''''
	if @CustomerNo is not Null select @strWhere = @strWhere + ' AND CustomerNo = ' + convert(varchar(20),@CustomerNo)
	if @BranchNo is not Null select @strWhere = @strWhere + ' AND BranchNo =' + convert(varchar(4),@BranchNo)
	if @MorNo is not Null select @strWhere = @strWhere + ' AND MorNo =' + @MorNo
	if @MorabCatCode is not Null select @strWhere = @strWhere + ' AND MorabCatCode =' + convert(varchar(4),@MorabCatCode)	
      if @ChequeDueDateFrom is not Null select @strWhere = @strWhere + ' AND ChequeDueDate >=''' + Convert(varchar(25),@ChequeDueDateFrom) +''''
	if @ChequeDueDateTo is not Null select @strWhere = @strWhere + ' AND ChequeDueDate <=''' + Convert(varchar(25),@ChequeDueDateTo) +''''
	if @RemainingAmountFrom is not Null select @strWhere = @strWhere + ' AND RemainingAmount >=' + Convert(varchar(100),@RemainingAmountFrom)
	if @RemainingAmountTo is not Null select @strWhere = @strWhere + ' AND RemainingAmount <=' + Convert(varchar(100),@RemainingAmountTo)
	if @ChequeStatus is not Null 
      Begin
  if @ChequeStatus = 2 
 	 select @strWhere = @strWhere + ' AND (ChequeStatus = 2 OR ChequeStatus = 5)'
  Else
 	 select @strWhere = @strWhere + ' AND ChequeStatus =' + Convert(varchar(100),@ChequeStatus)
    End

	--select
	IF @ShowResutlt = 1 
  Begin
  IF EXISTS(SELECT name FROM sysobjects WHERE  name = N'select_qryReportsInstl' AND type = 'U') DROP TABLE select_qryReportsInstl
  exec ('select * into select_qryReportsInstl from qryReportsInstl where ' + @strWhere)
  select * from select_qryReportsInstl
  End
	ELSE
  IF EXISTS(SELECT name FROM sysobjects WHERE  name = N'select_qryReportsInstl' AND type = 'U') Truncate TABLE select_qryReportsInstl

	--total
	IF EXISTS(SELECT name FROM sysobjects WHERE  name = N'total_qryReportsInstl' AND type = 'U') DROP TABLE total_qryReportsInstl
	exec ('select Sum(RemainingAmount) as TotalRemainingAmount into total_qryReportsInstl from qryReportsInstl where ' + @strWhere)
	select @TotalRemainingAmount = TotalRemainingAmount from total_qryReportsInstl

	--count
	IF EXISTS(SELECT name FROM sysobjects WHERE  name = N'count_qryReportsInstl' AND type = 'U') DROP TABLE count_qryReportsInstl
	exec ('select count(RemainingAmount) as CountRec into count_qryReportsInstl from qryReportsInstl where ' + @strWhere)
	select @CountRec = CountRec from count_qryReportsInstl
GO

تم تعديل هذه المشاركة بواسطة walcom في 10 مارس 2005 في 12:35

#2

can you discribe the error u are getting ?

#3

الاخ مدني المحترم ،

اولا لك جزيل شكري على ردك على موضوع التاريخ زادك الله علما .

اما بالنسبة لهذا الموضوع فأرجو منك المحاولة ان وجدت فرصة لذلك .

الكود الكامل هو :

********************************

ALTER PROCEDURE sp_qryReportsInstl

@CustomerName varchar(100)= Null,

@CustomerNo bigint= Null,

@BranchNo int = Null ,

@MorNo bigint = Null ,



@ChequeDueDateFrom datetime= Null,--'01-01-1980',

@ChequeDueDateTo datetime = Null,--'01-01-2050',



@RemainingAmountFrom float = 0,

@RemainingAmountTo float = 5000000000,



@MorabCatCode int = Null ,

@ChequeStatus int = Null,

@ShowResutlt int = 1,

@TotalRemainingAmount float OUTPUT,

@CountRec float OUTPUT



AS

Declare @strWhere varchar(1000)

SELECT @strWhere = 'BranchNo > 0'

if @CustomerName is not Null select @strWhere = @strWhere + ' AND CustomerName like ''%' + @CustomerName + '%'''

--if @CustomerName is not Null select @strWhere = @strWhere + ' AND CustomerName = ''' + @CustomerName + ''''

if @CustomerNo is not Null select @strWhere = @strWhere + ' AND CustomerNo = ' + convert(varchar(20),@CustomerNo)

if @BranchNo is not Null select @strWhere = @strWhere + ' AND BranchNo =' + convert(varchar(4),@BranchNo)

if @MorNo is not Null select @strWhere = @strWhere + ' AND MorNo =' + @MorNo

if @MorabCatCode is not Null select @strWhere = @strWhere + ' AND MorabCatCode =' + convert(varchar(4),@MorabCatCode) 

if @ChequeDueDateFrom is not Null select @strWhere = @strWhere + ' AND ChequeDueDate >=''' + Convert(varchar(25),@ChequeDueDateFrom) +''''

if @ChequeDueDateTo is not Null select @strWhere = @strWhere + ' AND ChequeDueDate <=''' + Convert(varchar(25),@ChequeDueDateTo) +''''

if @RemainingAmountFrom is not Null select @strWhere = @strWhere + ' AND RemainingAmount >=' + Convert(varchar(100),@RemainingAmountFrom)

if @RemainingAmountTo is not Null select @strWhere = @strWhere + ' AND RemainingAmount <=' + Convert(varchar(100),@RemainingAmountTo)

if @ChequeStatus is not Null 

Begin

if @ChequeStatus = 2 

select @strWhere = @strWhere + ' AND (ChequeStatus = 2 OR ChequeStatus = 5)'

Else

select @strWhere = @strWhere + ' AND ChequeStatus =' + Convert(varchar(100),@ChequeStatus)

End



--select

IF @ShowResutlt = 1 

Begin

IF EXISTS(SELECT name FROM sysobjects WHERE name = N'select_qryReportsInstl' AND type = 'U') DROP TABLE select_qryReportsInstl

exec ('select * into select_qryReportsInstl from qryReportsInstl where ' + @strWhere)

select * from select_qryReportsInstl

End

ELSE

IF EXISTS(SELECT name FROM sysobjects WHERE name = N'select_qryReportsInstl' AND type = 'U') Truncate TABLE select_qryReportsInstl



--total

IF EXISTS(SELECT name FROM sysobjects WHERE name = N'total_qryReportsInstl' AND type = 'U') DROP TABLE total_qryReportsInstl

exec ('select Sum(RemainingAmount) as TotalRemainingAmount into total_qryReportsInstl from qryReportsInstl where ' + @strWhere)

select @TotalRemainingAmount = TotalRemainingAmount from total_qryReportsInstl



--count

IF EXISTS(SELECT name FROM sysobjects WHERE name = N'count_qryReportsInstl' AND type = 'U') DROP TABLE count_qryReportsInstl

exec ('select count(RemainingAmount) as CountRec into count_qryReportsInstl from qryReportsInstl where ' + @strWhere)

select @CountRec = CountRec from count_qryReportsInstl

GO



********************************

اخي الكريم لاحظ الاسطر الاخيرة التالية في الكود :

* 



--count

IF EXISTS(SELECT name FROM sysobjects WHERE name = N'count_qryReportsInstl' AND type = 'U') DROP TABLE count_qryReportsInstl

exec ('select count(RemainingAmount) as CountRec into count_qryReportsInstl from qryReportsInstl where ' + @strWhere)

select @CountRec = CountRec from count_qryReportsInstl



*

علما أن @CountRec هي output parameter وانا احصل عليه عن طريقة كتابته كل مرة في جدول اسمه count_qryReportsInstl ثم اقرأه من ذلك الجدول . صحيح انه شغال ولكن حذف جدول كامل وانشاؤه من جديد ثم الكتابة في والقراءة منه كل مرة يبطىء العمل . وكان من الافضل ان يتم استخراج out parameter بالطريقة التقليدية السريعة وهي كما يلي :

*



exec ('select @CountRec = count(RemainingAmount) as CountRec from qryReportsInstl where ' + @strWhere)



*

وهي عبارة عن امر select يعود بالقيمة المطلوبة ، ولكن صياغة الامر لدى وصله مع @strWhere يعطيني خطأ دائما ، مما اضطرني للف والدوران والكتابة في جدول مستقل .

آمل المساعدة في تصحيح الصياغة ،

#4

use sp_executesql function which simplifies dynamic sql queries

DECLARE @query NVARCHAR(1000)
Set  @query = N'set @CountRec =(select  count(RemainingAmount) from qryReportsInstl where ' + @strWhere + ')'
EXEC sp_executesql @query, N'@CountRec float output', @CountRec output
#5

That command seems great. Thank you very much .

هذا الموضوع مغلق.

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