表字段配置sql语句 使用存储过程执行

1、配置表信息

USE [cishu]
GO

/****** Object:  Table [dbo].[SearchConfig]    Script Date: 03/09/2018 17:39:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[SearchConfig](
    [DisplayIndex] [int] NULL,
    [TypeValue] [nvarchar](100) NULL,
    [TypeName] [nvarchar](100) NULL,
    [ClassName] [nvarchar](100) NULL,
    [sqlString1] [nvarchar](500) NULL,
    [sqlString2] [nvarchar](500) NULL,
    [sqlString3] [nvarchar](500) NULL
) ON [PRIMARY]

GO
View Code

2、执行存储

USE [cishu]
GO

/****** Object:  StoredProcedure [dbo].[MessageReault_SearchFullTextList]    Script Date: 03/09/2018 17:41:20 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




ALTER PROCEDURE [dbo].[MessageReault_SearchFullTextList]
@searchContent NVARCHAR(100) = NULL,
@TypeValue NVARCHAR(100) = NULL,
@PageIndex INT=1,
@PageSize INT=10
AS
BEGIN
SET NOCOUNT ON;

DECLARE @ExecSql NVARCHAR(200)

SELECT TOP 1 @ExecSql=sqlString3 FROM [SearchConfig] WHERE TypeValue=@TypeValue

IF    (@ExecSql IS NULL OR @ExecSql='') BEGIN  RAISERROR ('配置中未找到该查询类型!', 10, 1) END
ELSE 
BEGIN
exec sp_executesql @ExecSql,N'@searchContent nvarchar(100),@TypeValue nvarchar(100),@PageIndex INT,@PageSize INT', 
            @searchContent=@searchContent,@TypeValue=@TypeValue,@PageIndex=@PageIndex,@PageSize=@PageSize
end
--exec(@ExecSql)
SET NOCOUNT OFF;
END


GO
View Code

3、全部类型存储

USE [cishu]
GO

/****** Object:  StoredProcedure [dbo].[MessageReault_SearchFullTextListByAll]    Script Date: 03/09/2018 17:43:54 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO











ALTER PROCEDURE [dbo].[MessageReault_SearchFullTextListByAll]
@searchContent NVARCHAR(100) = NULL,
@TypeValue NVARCHAR(100) = NULL,
@PageIndex INT=1,
@PageSize INT=10
--@AllCount INT=0 OUTPUT
AS
BEGIN
SET NOCOUNT ON;
    DECLARE @minIndex INT
    DECLARE @maxIndex INT
    DECLARE @ExecSql NVARCHAR(200)

    SELECT @minIndex=MIN(a.DisplayIndex),@maxIndex=MAX(a.DisplayIndex) FROM dbo.SearchConfig a
    WHERE TypeValue!='All'
    
    WHILE @minIndex<=@maxIndex
    BEGIN 
        DECLARE @tempAllCount INT
        DECLARE @tempTypeValue NVARCHAR(100)
        SET @tempAllCount=0
        SELECT TOP 1 @ExecSql=sqlString1,@tempTypeValue=TypeValue FROM [SearchConfig] WHERE  TypeValue!='All' AND DisplayIndex=@minIndex
        IF    (@ExecSql IS NULL OR @ExecSql='') 
        BEGIN SET @minIndex=@minIndex+1 CONTINUE  END
        ELSE 
            BEGIN
            CREATE TABLE #tmp(
            AllCount NVARCHAR(100),
            TypeValue NVARCHAR(100)
            )
            INSERT INTO #tmp
            exec sp_executesql @ExecSql,N'@searchContent nvarchar(100),@TypeValue nvarchar(100),@AllCount INT OUTPUT', 
                        @searchContent=@searchContent,@TypeValue=@tempTypeValue,@AllCount=@tempAllCount OUTPUT
            DROP TABLE #tmp
            END
        IF    @tempAllCount>0 
        BEGIN
        SET @minIndex=@minIndex+1
             EXEC MessageReault_SearchFullTextList @searchContent,@tempTypeValue,@PageIndex,@PageSize
             BREAK 
         END
         ELSE
         BEGIN
         SET @minIndex=@minIndex+1
         CONTINUE
         END
      
    END

--exec(@ExecSql)
SET NOCOUNT OFF;
END









GO
View Code

 

posted @ 2018-03-09 17:44  caolingyi  阅读(363)  评论(0编辑  收藏  举报