NJC__Documentation::DB

dbo.MyRUMUSWhereAndSortView

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

Name Data Type Length Description
@PARAMETER xml -1
@XMLMAPING xml -1
@fixWhere varchar 2000
@Sort varchar 255
@desc bit 1
@OutSortBy varchar 255
@rumusWhere varchar -1
@search xml -1

SQL Script

SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
create procedure dbo.MyRUMUSWhereAndSortView
 @PARAMETER xml,
 @XMLMAPING xml,
 @fixWhere varchar(2000),
 @Sort varchar(255)  ,
 @desc bit,
 @OutSortBy varchar(255) output,
 @rumusWhere varchar(max) output,
 @search xml  output
WITH 
---XencX---encryption,
EXECUTE AS CALLER
AS
BEGIN
set nocount on
declare @searchX table(xid varchar(3), isi varchar(255));

declare @tblMapping table (
 ifield varchar(255) primary key,
 iname varchar(max),
 iBolehSearch bit ,
 iSearchtype int , --= 1= STRING ,2=ANGKA , 3=DATETIME,
 iSearchByPassLike bit)
declare  @ifield varchar(255) ,
 @iname varchar(255),
 @iBolehSearch bit ,
 @iSearchtype int , --= 1= STRING ,2=ANGKA , 3=DATETIME,
 @iSearchByPassLike bit
insert into  @tblMapping 
SELECT
  isnull(tempTable.xItem.value('@ifield', 'varchar(255)'),'') as ifield,
  isnull(tempTable.xItem.value('@iname', 'varchar(max)'),'')  as iname,
  isnull(tempTable.xItem.value('@iBolehSearch', 'bit'),0) as iBolehSearch ,
  isnull(tempTable.xItem.value('@iSearchtype', 'int'),1) as iSearchtype, 
  isnull(tempTable.xItem.value('@iSearchByPassLike', 'varchar(255)'),0) as iSearchByPassLike 
FROM @XMLMAPING.nodes('fl/r')  tempTable(xItem)
  
declare
  @x_rID varchar(255),
  @x_cr bit,
  @x_lr bit,
  @x_ll bit,
  @x_ft varchar(255),
  @x_ft2 varchar(255),
   @x_cn int,
  @RESULT1 varchar(1000)  ,
  @RESULT2 varchar(1000)   
set @rumusWhere=''
set @rumusWhere=@rumusWhere +  @fixWhere

DECLARE xSearch CURSOR FOR   
SELECT
  isnull(tempTable.xItem.value('@rID', 'varchar(255)'),'') ,
  isnull(tempTable.xItem.value('@cr', 'bit'),0) ,
  isnull(tempTable.xItem.value('@lr', 'bit'),0) , 
  isnull(tempTable.xItem.value('@ll', 'bit'),0) ,
  isnull(tempTable.xItem.value('@ft', 'varchar(255)'),'') , 
  isnull(tempTable.xItem.value('@ft2', 'varchar(255)'),'') ,
   isnull(tempTable.xItem.value('@cn', 'int'),1) 
FROM @PARAMETER.nodes('param/search/r')  tempTable(xItem)
OPEN xSearch;
  FETCH NEXT FROM xSearch INTO 
  @x_rID  ,
  @x_cr  ,
  @x_lr  ,
  @x_ll  ,
  @x_ft  ,
  @x_ft2  ,
   @x_cn   ;
   WHILE @@FETCH_STATUS = 0
   BEGIN
    select top 1  
    @ifield= ifield,
    @iname =iname  ,
    @iBolehSearch =iBolehSearch  ,
    @iSearchtype =iSearchtype  , --= 1= STRING ,2=ANGKA , 3=DATETIME,
    @iSearchByPassLike  =iSearchByPassLike
    from @tblMapping where ifield=@x_rID
    
    if @iBolehSearch  =1
    begin
         
         
     EXEC [dbo].[OlahFindViewType2]
       @PATH =@ifield,
       @FIELD_TABLE = @iname,
       @TYPE =@iSearchtype,
       @BYPASS_LIKE = @iSearchByPassLike,
       @x_cr = @x_cr,
       @x_lr = @x_lr,
       @x_ll = @x_ll,
       @x_ft =@x_ft,
       @x_ft2 =@x_ft2,
        @x_cn = @x_cn,
       @RESULT1 = @RESULT1 OUTPUT,
       @RESULT2 = @RESULT2 OUTPUT
     
   
     if LEN(@RESULT1)>0
     begin
      set @rumusWhere=@rumusWhere +  @RESULT1
      insert into @searchX (xid,isi) values (@ifield, ltrim(rtrim(@RESULT2)));
      end
     end
    
    FETCH NEXT FROM xSearch INTO 
  @x_rID  ,
  @x_cr  ,
  @x_lr  ,
  @x_ll  ,
  @x_ft  ,
  @x_ft2  ,
   @x_cn   ;
   END;

  CLOSE xSearch;
  DEALLOCATE xSearch

if nullif(@Sort,'') is null
begin
    select  top 1 @OutSortBy=iname ,@Sort=ifield from @tblMapping
 
end
else 
begin

 set @OutSortBy=(select  top 1 iname from @tblMapping where ifield =@Sort ) 
 if @OutSortBy is null 
 begin

  set @Sort='rID';
  set @OutSortBy=(select  top 1 iname from @tblMapping where ifield =@Sort ) 
  if @OutSortBy is null 
  begin
     select  top 1 @OutSortBy=iname ,@Sort=ifield from @tblMapping
    
  end 

 end 

end


if (@desc=1) set @OutSortBy= @OutSortBy + ' desc ' 
set @search =(select xid as '@id',isi as '@isi' from @searchx for xml path('fl'))
if (LEN(@rumusWhere)>0)   set @rumusWhere= ' where ' + SUBSTRING ( @rumusWhere ,0,LEN( @rumusWhere)-3);
 set nocount off
 END
 
GO

Depends On

1