또 하나의 유니버설 페이지 저장 프로세스, 테이블 별명 지원, 다중 테이블 연합 조회 SQL 문장
15942 단어 ql 문장
select '' as CheckBox, A.TargetID, A.TargetPeriod, Convert(varchar(10),
B.BeginDate, 120) as BeginDate,
Convert(varchar(10), B.EndDate, 120) as EndDate, C.SalesCode,
C.SalesName, D.CatalogCode, D.CatalogName,
E.OrgID, E.OrgName, F.OrgID as BranchOrgID, F.OrgCode as
BranchOrgCode, F.OrgName as BranchOrgName,
A.Amount, '' as DetailButton
from ChlSalesTarget as A
left outer join ChlSalesTargetPeriod as B on A.TargetPeriod=B.TargetPeriod
left outer join ChlSales as C on A.Sales=C.SalesCode
left outer join ChlItemCatalog as D on A.ItemCatalog=D.CatalogCode
left outer join ChlOrg as E on A.OrgID=E.OrgID
left outer join ChlOrg as F on C.BranchOrgID=F.OrgID
where A.TargetPeriod >='200607' and A.TargetPeriod <='200608' and F.OrgCode
like '%123%' and E.OrgCode like '%123%'
order by A.TargetPeriod desc,C.SalesName,D.CatalogName
위의 SQL 안에는 일부 특수한 상황이 있다. 예를 들어Convert 함수를 사용했고 메인 키가 없고 여러 개의 테이블 연결이 있으며 테이블 별명, 필드 별명 등이 있다. 이런 상황은 처리하기가 비교적 까다로울 수 있다. 물론 그 중의'as Check Box'는 내 시스템의 특례 상황으로 처리하는 데 사용된다.
제가 이곳에서 자체 개발한 유니버설 페이지 저장 프로세스를 제공하는데 어떤 좋은 건의와 의견이 있는지 여러분의 가르침을 아끼지 마십시오.코드는 다음과 같습니다.
----Sp_Paging
/**//*
:
:
@PK varchar(50),
, , , ,
@Fields varchar(500), ( :ID,Code,Name)
@Tables varchar(1000), (Org)
@Where varchar(500), (Code like '100')
@OrderBy varchar(100), ( , :ID,Code desc,Name desc)
@PageIndex int, , 1 , 0。
@PageSize int,
:Hollis Yao
:2006-08-06
:
*/
CREATE PROCEDURE [dbo].[Sp_Paging]
@PK varchar(50)='',
@Fields varchar(500),
@Tables varchar(1000),
@Where varchar(500)='',
@OrderBy varchar(100),
@PageIndex int,
@PageSize int
AS
-- , SQL
set @Fields = replace(@Fields, '''', '''''')
-- SQL, , 4k
declare @SQL1 varchar(4000)
declare @SQL2 varchar(4000)
set @SQL1 = ''
set @SQL2 = ''
if @Where is not null and len(ltrim(rtrim(@Where))) > 0
set @Where = ' where ' + @Where
else
set @Where = ' where 1=1'
set @SQL1 = @SQL1 + ' declare @TotalCount int' -- ,
set @SQL1 = @SQL1 + ' declare @PageCount int' -- ,
set @SQL1 = @SQL1 + ' declare @PageIndex int' -- ,
set @SQL1 = @SQL1 + ' declare @StartRow int' -- ,
set @SQL1 = @SQL1 + ' select @TotalCount=count(*) from ' + @Tables + @Where --
set @SQL1 = @SQL1 + ' if @PageCount <= 0 begin' -- 0,
set @SQL1 = @SQL1 + ' select ' + @Fields + ' from ' + @Tables + ' where 1<>1'
set @SQL1 = @SQL1 + ' select 0 as PageIndex,0 as PageCount,'
+ convert(varchar, @PageSize) + ' as PageSize,0 as TotalCount'
set @SQL1 = @SQL1 + ' return end'
set @SQL1 = @SQL1 + ' set @PageCount=(@TotalCount+' + convert(varchar, @PageSize)
+ '-1)/' + convert(varchar, @PageSize) --
set @SQL1 = @SQL1 + ' set @PageIndex=' + convert(varchar, @PageIndex)
--
set @SQL1 = @SQL1 + ' if @PageIndex<0 set @PageIndex=1'
set @SQL1 = @SQL1 + ' if @PageIndex>@PageCount and @PageCount>0
set @PageIndex=@PageCount'
set @SQL1 = @SQL1 + ' set @StartRow=(@PageIndex-1)*' + convert(varchar, @PageSize)
+ '+1'
if (charindex(',', @OrderBy)=0 and charindex(@PK, @OrderBy)>0)
begin
--****************************************************************************
--**************** ********************************************
--****************************************************************************
declare @SortDirection varchar(10) -- ,>=: ,<=:
set @SortDirection = '>='
if charindex('desc', @OrderBy) > 0
set @SortDirection = '<='
set @SQL2 = @SQL2 + ' declare @Sort varchar(100)'
-- ,
set @SQL2 = @SQL2 + ' set rowcount @StartRow'
--
set @SQL2 = @SQL2 + ' select @Sort=' + @PK + ' from '
+ @Tables + @Where + ' order by ' + @OrderBy --
set @SQL2 = @SQL2 + ' set rowcount ' + convert(varchar, @PageSize)
--
set @Where = @Where + ' and ' + @PK + @SortDirection + '@Sort'
set @SQL2 = @SQL2 + ' select ' + @Fields + ' from ' + @Tables
+ @Where + ' order by ' + @OrderBy
--
end
else
begin
set @SQL2 = @SQL2 + ' declare @EndRow int'
set @SQL2 = @SQL2 + ' set @EndRow=@PageIndex*' + convert(varchar, @PageSize)
set @SQL2 = @SQL2 + ' set rowcount @EndRow'
set @SQL2 = @SQL2 + ' declare @PKBegin int' -- ,
set @SQL2 = @SQL2 + ' declare @PKEnd int' -- ,
set @SQL2 = @SQL2 + ' set @PKBegin=@StartRow'
set @SQL2 = @SQL2 + ' set @PKEnd=@EndRow'
--****************************************************************************
--************ , ******************
--****************************************************************************
declare @TempFields varchar(500)
set @TempFields=@Fields
set @TempFields = replace(@TempFields, ''''' as CheckBox', '')
set @TempFields = replace(@TempFields, ''''' as DetailButton', '')
set @TempFields = replace(@TempFields, ''''' as Radio', '')
set @TempFields = LTRIM(RTRIM(@TempFields))
if left(@TempFields,1)=',' --
set @TempFields = substring(@TempFields, 2, len(@TempFields))
if right(@TempFields,1)=',' --
set @TempFields = substring(@TempFields, 1, len(@TempFields)-1)
set @SQL2 = @SQL2 + ' select identity(int,1,1) as PK,' + @TempFields
+ ' into #tb from ' + @Tables + @Where + ' order by ' + @OrderBy
--****************************************************************************
--******** , , *********
--****************************************************************************
declare @TotalFields varchar(500)
declare @tmp varchar(50)
declare @i int
declare @j int
declare @iLeft int --
declare @iRight int --
set @i = 0
set @j = 0
set @iLeft = 0
set @iRight = 0
set @tmp = ''
set @TotalFields = ''
while (len(@Fields)>0)
begin
set @i = charindex(',', @Fields)
-- www.xker.com( )
if (@i=0)
begin
-- ,
set @tmp = @Fields
end
else
begin
set @tmp = substring(@Fields, 1, @i)
end
set @j = charindex('.', @tmp)
if (@j>0)
set @tmp = substring(@tmp, @j+1, len(@tmp))
--******* , *********
-- , Convert(varchar(10), B.EndDate, 120) as EndDate
while (charindex('(', @tmp) > 0)
begin
set @iLeft = @iLeft + 1
set @tmp = substring(@tmp, charindex('(', @tmp)+1, Len(@tmp))
end
while (charindex(')', @tmp) > 0)
begin
set @iRight = @iRight + 1
set @tmp = substring(@tmp, charindex(')', @tmp)+1, Len(@tmp))
end
-- ,
if (@iLeft = @iRight)
begin
set @iLeft = 0
set @iRight = 0
-- :CheckBox、DetailButton、Radio
if (charindex('CheckBox', @tmp) = 0 and charindex
('DetailButton', @tmp) = 0 and charindex('Radio', @tmp) = 0)
begin
--
if (charindex('as', @tmp) > 0)-- , 'as'
begin
set @tmp = substring(@tmp, charindex('as', @tmp)+2, len(@tmp))
end
else
begin
if (charindex(' ', @tmp) > 0)-- , (" ")
begin
while(charindex(' ', @tmp) > 0)
begin
set @tmp = substring(@tmp, charindex(' ', @tmp)+1, len(@tmp))
end
end
end
end
set @TotalFields = @TotalFields + @tmp
end
if (@i=0)
set @Fields = ''
else
set @Fields = substring(@Fields, @i+1, len(@Fields))
end
--print @TotalFields
set @SQL2 = @SQL2 + ' select ' + @TotalFields + '
from #tb where PK between @PKBegin and @PKEnd order by PK'
--
set @SQL2 = @SQL2 + ' drop table #tb'
end
-- “PageIndex( )、PageCount( )、PageSize( )、TotalCount( )”
set @SQL2 = @SQL2 + ' select @PageIndex as PageIndex,@PageCount as PageCount,'
+ convert(varchar, @PageSize) + ' as PageSize,@TotalCount as TotalCount'
--print @SQL1 + @SQL2
exec(@SQL1 + @SQL2)
이 공통 페이지 나누기 저장 프로세스를 사용하는 경우 호출 방법은 다음과 같습니다.
공통 페이지 나누기 저장 프로세스를 사용하여 페이지 나누기
/**//*
: ,
:
@UserType int,
@OrgID varchar(500),
@TargetPeriodBegin nvarchar(50),
@TargetPeriodEnd nvarchar(50),
@BranchOrgCode nvarchar(50),
@BranchOrgName nvarchar(50),
@OrgCode nvarchar(50),
@OrgName nvarchar(50),
@SalesCode nvarchar(50),
@SalesName nvarchar(50),
@CatalogCode nvarchar(50),
@CatalogName nvarchar(50),
@PageIndex int, , 1 , 0。
@PageSize int,
:Hollis Yao
:2006-08-11
:
============================================================
*/
CREATE PROCEDURE [dbo].[GetSalesTargetList]
@UserType int,
@OrgID nvarchar(500),
@TargetPeriodBegin nvarchar(50),
@TargetPeriodEnd nvarchar(50),
@BranchOrgCode nvarchar(50),
@BranchOrgName nvarchar(50),
@OrgCode nvarchar(50),
@OrgName nvarchar(50),
@SalesCode nvarchar(50),
@SalesName nvarchar(50),
@CatalogCode nvarchar(50),
@CatalogName nvarchar(50),
@PageIndex int,
@PageSize int
AS
declare @Condition nvarchar(2000)
set @Condition = ''
if (@UserType<>1)
set @Condition = @Condition + ' and A.OrgID in (' + @OrgID + ')'
if (len(@TargetPeriodBegin)>0)
set @Condition = @Condition + ' and A.TargetPeriod >=''' + @TargetPeriodBegin + ''''
if (len(@TargetPeriodEnd)>0)
set @Condition = @Condition + ' and A.TargetPeriod <=''' + @TargetPeriodEnd + ''''
if (len(@BranchOrgCode)>0)
set @Condition = @Condition + ' and F.OrgCode like ''%' + @BranchOrgCode + '%'''
if (len(@BranchOrgName)>0)
set @Condition = @Condition + ' and F.OrgName like ''%' + @BranchOrgName + '%'''
if (len(@OrgCode)>0)
set @Condition = @Condition + ' and E.OrgCode like ''%' + @OrgCode + '%'''
if (len(@OrgName)>0)
set @Condition = @Condition + ' and E.OrgName like ''%' + @OrgName + '%'''
if (len(@SalesCode)>0)
set @Condition = @Condition + ' and C.SalesCode like ''%' + @SalesCode + '%'''
if (len(@SalesName)>0)
set @Condition = @Condition + ' and C.SalesName like ''%' + @SalesName + '%'''
if (len(@CatalogCode)>0)
set @Condition = @Condition + ' and D.CatalogCode like ''%' + @CatalogCode + '%'''
if (len(@CatalogName)>0)
set @Condition = @Condition + ' and D.CatalogName like ''%' + @CatalogName + '%'''
if (len(@Condition)>0)
set @Condition = substring(@Condition,5,len(@Condition))
--print @Condition
exec sp_Paging
N'',N''' as CheckBox, A.TargetID, A.TargetPeriod, Convert(varchar(10),
B.BeginDate, 120) as BeginDate, Convert(varchar(10), B.EndDate, 120) as EndDate,
C.SalesCode, C.SalesName, D.CatalogCode, D.CatalogName, E.OrgID, E.OrgName,
F.OrgID as BranchOrgID, F.OrgCode as BranchOrgCode, F.OrgName as BranchOrgName,
A.Amount, '' as DetailButton',
N'ChlSalesTarget as A
left outer join ChlSalesTargetPeriod as B on A.TargetPeriod=B.TargetPeriod
left outer join ChlSales as C on A.Sales=C.SalesCode
left outer join ChlItemCatalog as D on A.ItemCatalog=D.CatalogCode
left outer join ChlOrg as E on A.OrgID=E.OrgID
left outer join ChlOrg as F on C.BranchOrgID=F.OrgID',
@Condition,
N'A.TargetPeriod desc,C.SalesName,D.CatalogName',
@PageIndex, @PageSize
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
유용한 SQL 문(중복 레코드 삭제, 로그 축소)텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.