sqlserver 存储过程 临时表无法排序,求各位大神解释
USE [HLT_HIEP_V2]
GO
/****** Object: StoredProcedure [dbo].[master_querySupplierCertificate] Script Date: 06/08/2013 10:54:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[master_querySupplierCertificate]
@OrganizationDomain NVARCHAR(20),
@PartnerId nvarchar(20),
@CertificateId nvarchar(20),
@CertificateName nvarchar(80),
@CertificateType int,
@StartValidDate datetime,
@EndValidDate datetime,
@CertificatePhoto nvarchar(128),
@Markers int,
@StartCreateDate datetime,
@EndCreateDate datetime,
@PageIndex int,
@PageSize int,
@SortField nvarchar(20),
@SortMethod nvarchar(20)
as
begin
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
DECLARE @TotalRecords int
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageSize - 1 + @PageLowerBound
--设置排序字段和规则
IF(@SortField IS NULL OR @SortMethod IS NULL OR @SortField='' OR @SortMethod='')
BEGIN
SET @SortField = 'PartnerId'
SET @SortMethod = 'DESC'
END
--拼SQL 按条件排序查询
DECLARE @SqlStr nvarchar(1000)
SET @SqlStr=''
DECLARE @OrganizationProviderId uniqueidentifier
IF(@OrganizationDomain IS NOT NULL AND @OrganizationDomain<>'' AND @OrganizationDomain<>'%' AND @OrganizationDomain<>'%%')
BEGIN
SELECT @OrganizationProviderId=OrganizationProviderId FROM system_Organizations WHERE OrganizationDomain = @OrganizationDomain
if(@OrganizationProviderId is null) return -1
END
IF(@OrganizationProviderId IS NOT NULL)
SET @SqlStr=@SqlStr+'OrganizationProviderId = '''+CONVERT(nvarchar(50), @OrganizationProviderId, 50) + ''' AND '
IF(@PartnerId IS NOT NULL AND @PartnerId<>'' AND @PartnerId<>'%' AND @PartnerId<>'%%')
SET @SqlStr+='PartnerId like ''%'+@PartnerId + '%'' AND '
IF(@CertificateId IS NOT NULL AND @CertificateId<>'' AND @CertificateId<>'%' AND @CertificateId<>'%%')
SET @SqlStr+='CertificateId like ''%'+@CertificateId + '%'' AND '
IF(@CertificateName IS NOT NULL AND @CertificateName<>'' AND @CertificateName<>'%' AND @CertificateName<>'%%')
SET @SqlStr+='CertificateName like ''%'+@CertificateName + '%'' AND '
IF(@CertificateType IS NOT NULL )
SET @SqlStr+='CertificateType = '''+@CertificateType + ''' AND '
IF(@StartValidDate IS NOT NULL)
SET @SqlStr += 'StartValidDate >= '''+CONVERT(varchar(50), @StartValidDate, 21) + ''' AND '
IF(@EndValidDate IS NOT NULL)
SET @SqlStr+='EndValidDate <= '''+CONVERT(varchar(50), @EndValidDate, 21) + ''' AND '
IF(@CertificatePhoto IS NOT NULL AND @CertificatePhoto<>'' AND @CertificatePhoto<>'%' AND @CertificatePhoto<>'%%')
SET @SqlStr+='CertificatePhoto like ''%'+@CertificatePhoto + '%'' AND '
IF(@Markers IS NOT NULL AND @Markers<>'' AND @Markers<>'%' AND @Markers<>'%%')
SET @SqlStr+='Markers = '''+@Markers + ''' AND '
IF(@StartCreateDate IS NOT NULL)
SET @SqlStr += 'CreateDate >= '''+CONVERT(varchar(50), @StartCreateDate, 21) + ''' AND '
IF(@EndCreateDate IS NOT NULL)
SET @SqlStr+='CreateDate <= '''+CONVERT(varchar(50), @EndCreateDate, 21) + ''' AND '
IF(RIGHT(@SqlStr,4)='AND ') SET @SqlStr = LEFT(@SqlStr,LEN(@SqlStr)-4)
IF(@SqlStr<>'') SET @SqlStr=' WHERE '+@SqlStr
SET @SqlStr= 'SELECT OrganizationProviderId,PartnerId,CertificateId FROM master_SupplierCertificate '+@SqlStr
SET @SqlStr= @SqlStr + ' ORDER BY '+ @SortField + ' ' + @SortMethod
CREATE TABLE #sortInformation
(
IndexId int IDENTITY (0, 1) NOT NULL,
[OrganizationProviderId] uniqueidentifier NOT NULL,
PartnerId nvarchar(20),
CertificateId nvarchar(20)
)
INSERT INTO #sortInformation([OrganizationProviderId],PartnerId,CertificateId)
EXEC(@SqlStr)
SET @TotalRecords = @@ROWCOUNT
SELECT o.OrganizationProviderId
,o.[PartnerId]
,o.[CertificateId]
,[CertificateName]
,[CertificateType]
,[StartValidDate]
,[EndValidDate]
,[CertificatePhoto]
,[Remark]
,[Markers]
FROM [HLT_HIEP_V2].[dbo].[master_SupplierCertificate] AS o, #sortInformation AS s
WHEREo.OrganizationProviderId=s.OrganizationProviderId
and o.PartnerId = s.PartnerId
and o.CertificateId = s.CertificateId
AND s.IndexId
BETWEEN convert(varchar(10), @PageLowerBound)
and convert(varchar(10), @PageUpperBound)
order by @SortField + ' ' + @SortMethod
RETURN @TotalRecords
end
[解决办法]
select @SortField + ' ' + @SortMethod,看下什么值。
[解决办法]