【问题标题】:Append stored procedure with query使用查询附加存储过程
【发布时间】:2016-10-12 20:54:10
【问题描述】:
ALTER PROCEDURE [dbo].[Testing]
@PageIndex INT = 1
  ,@PageSize INT = 10
  ,@type nvarchar(max)
  ,@city nvarchar(max)
  ,@query nvarchar(max)
  ,@RecordCount INT OUTPUT
  AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @mainQuery nvarchar(MAX),
        @paramDeclaration nvarchar(500);
SET @paramDeclaration = ' @PageIndex INT = 1
                         ,@PageSize INT = 10
                         ,@type nvarchar(max)
                         ,@city nvarchar(max)
                         ,@RecordCount INT';
SET @mainQuery = '

  ;WITH DistinctMails AS
(
select  Unit_Table.Unit_title, Vendor_Base_Price.Base_Price, Vendor_Base_Price.showprice, Category_Table.Title, Vendor_Registration.Business_Name, 
                     Vendor_PrimaryInfo.Street_Address, Vendor_PrimaryInfo.Locality, Vendor_PrimaryInfo.Nearest_Landmark, Vendor_PrimaryInfo.City, Vendor_PrimaryInfo.State, 
                     Vendor_PrimaryInfo.Country, Vendor_PrimaryInfo.PostalCode, Vendor_PrimaryInfo.Latitude, Vendor_PrimaryInfo.Longitude, Vendor_PrimaryInfo.ImageUrl, 
                     Vendor_PrimaryInfo.ContactNo, Vendor_PrimaryInfo.Email,Vendor_PrimaryInfo.Vendor_ID,Vendor_Value_Table.Feature_ID,Vendor_Value_Table.Value_Text

 ,
    ROW_NUMBER() OVER(PARTITION BY Vendor_PrimaryInfo.Vendor_ID ORDER BY Vendor_PrimaryInfo.Vendor_ID) AS RowNum

  FROM  Unit_Table INNER JOIN
                     Vendor_Base_Price ON Unit_Table.Unit_ID = Vendor_Base_Price.Unit_ID INNER JOIN
                     Vendor_PrimaryInfo ON Vendor_Base_Price.Vendor_ID = Vendor_PrimaryInfo.Vendor_ID INNER JOIN
                     Vendor_Registration ON Vendor_Base_Price.Vendor_ID = Vendor_Registration.Vendor_ID AND 
                     Vendor_PrimaryInfo.Vendor_ID = Vendor_Registration.Vendor_ID INNER JOIN
                     Category_Table ON Vendor_Registration.Category_ID = Category_Table.Category_ID
                     LEFT JOIN
                     Vendor_Value_Table ON Vendor_Registration.Vendor_ID = Vendor_Value_Table.Vendor_ID LEFT JOIN
                     Feature_Table ON Vendor_Value_Table.Feature_ID = Feature_Table.Feature_ID
                 where Vendor_Registration.Category_ID=@type and Vendor_PrimaryInfo.City=@city'
                 SET @mainQuery = @mainQuery + @query +'

)
SELECT * into #Results  
FROM DistinctMails
WHERE RowNum = 1
  SELECT @RecordCount = COUNT(*)
  FROM #Results

  SELECT * FROM #Results
  WHERE RowNum BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

  DROP TABLE #Results'
  exec sp_executesql @mainQuery, @paramDeclaration, @PageIndex = @PageIndex, @PageSize = @PageSize, @type = @type,
                                                  @city = @city, @RecordCount = @RecordCount
  END

**不能将 RecordCount 作为输出 ** 请给我一些意见我被困在这里 提前致谢

如您所见,我有一个名为@query 的变量,我只想在其中附加查询,其中包含各种and and or 运算符,我该怎么做?

提前致谢

【问题讨论】:

    标签: asp.net stored-procedures sql-server-2012


    【解决方案1】:

    我相信您应该将查询准备为字符串,然后使用dynamic sql 执行它。

    sp_executesql [ @stmt = ] statement
    [ 
      { , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' } 
         { , [ @param1 = ] 'value1' [ ,...n ] }
    ]
    

    在我的 SQL Server 2012 上测试的示例在旧版本中可能会有所不同。

    DECLARE @query NVARCHAR(MAX),
            @conditions NVARCHAR(MAX),
            @combinedQuery NVARCHAR(MAX),
            @testValue NVARCHAR(100),
            @testValue2 Int,
            @ParmDefinition NVARCHAR(500);
    SET @ParmDefinition = N'@parameter NVARCHAR(100), @testValue2 Int OUTPUT';
    SET @testValue = 'HelloWorld'
    SET @query = 'SELECT * FROM dbo.Localizations'
    SET @conditions = ' WHERE Value =@parameter SET @testValue2 = 33'
    SET @combinedQuery = @query + @conditions
    
    exec sp_executesql @combinedQuery,@ParmDefinition, @parameter = @testValue,   @testValue2 = @testValue2 OUTPUT
    SELECT  @testValue2
    

    你的程序将被转换成类似的东西:

    ALTER PROCEDURE GetVendorsPageWise
           @PageIndex INT = 1
          ,@PageSize INT = 10
          ,@type nvarchar(max)
          ,@city nvarchar(max)
          ,@query nvarchar(max)
          ,@RecordCount INT OUTPUT
    AS
    BEGIN
        SET NOCOUNT ON;
        DECLARE @mainQuery nvarchar(MAX),
                @paramDeclaration nvarchar(500);
        SET @paramDeclaration = ' @PageIndex INT = 1
                                 ,@PageSize INT = 10
                                 ,@type nvarchar(max)
                                 ,@city nvarchar(max)
                                 ,@RecordCount INT OUTPUT';
        SET @mainQuery = 
        'SELECT 
            ROW_NUMBER() OVER (ORDER BY Vendor_PrimaryInfo.Vendor_ID asc) AS RowNumber,
            Unit_Table.Unit_title, Vendor_Base_Price.Base_Price,        
            Vendor_Base_Price.showprice, Category_Table.Title,    
            Vendor_Registration.Business_Name, 
            Vendor_PrimaryInfo.Street_Address, Vendor_PrimaryInfo.Locality, 
            Vendor_PrimaryInfo.Nearest_Landmark, Vendor_PrimaryInfo.City, 
            Vendor_PrimaryInfo.State, Vendor_PrimaryInfo.Country, 
            Vendor_PrimaryInfo.PostalCode, Vendor_PrimaryInfo.Latitude, 
            Vendor_PrimaryInfo.Longitude, Vendor_PrimaryInfo.ImageUrl, 
            Vendor_PrimaryInfo.ContactNo, Vendor_PrimaryInfo.Email, 
            Vendor_PrimaryInfo.Vendor_ID, Vendor_Value_Table.Feature_ID,
            Vendor_Value_Table.Value_Text
        INTO #Results
        FROM  
            Unit_Table 
        INNER JOIN
            Vendor_Base_Price ON Unit_Table.Unit_ID = Vendor_Base_Price.Unit_ID 
        INNER JOIN
            Vendor_PrimaryInfo ON Vendor_Base_Price.Vendor_ID = Vendor_PrimaryInfo.Vendor_ID 
        INNER JOIN
            Vendor_Registration ON Vendor_Base_Price.Vendor_ID = Vendor_Registration.Vendor_ID 
                                AND Vendor_PrimaryInfo.Vendor_ID = Vendor_Registration.Vendor_ID 
        INNER JOIN
            Category_Table ON Vendor_Registration.Category_ID = Category_Table.Category_ID
        LEFT JOIN
            Vendor_Value_Table ON Vendor_Registration.Vendor_ID = Vendor_Value_Table.Vendor_ID 
        LEFT JOIN
            Feature_Table ON Vendor_Value_Table.Feature_ID = Feature_Table.Feature_ID
        WHERE
            Vendor_Registration.Category_ID = @type 
            AND Vendor_PrimaryInfo.City = @city '
    
        SET @mainQuery = @mainQuery + @query +
        'ORDER BY 
            Vendor_PrimaryInfo.Vendor_ID
    
        SELECT @RecordCount = COUNT(*)
        FROM #Results
    
        SELECT * 
        FROM #Results
        WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
    
        DROP TABLE #Results'
    
        exec sp_executesql @mainQuery, @paramDeclaration, @PageIndex = @PageIndex, @PageSize = @PageSize, @type = @type,
                                                          @city = @city, @RecordCount = @RecordCount OUTPUT
    END
    

    【讨论】:

    • 您应该将存储过程中的所有代码转换为字符串,附加查询条件并执行它。在我的答案中添加了示例。希望这会有所帮助。
    • 正如您在我的过程中看到的那样,它的中间不是最后一行,所以我如何使用 SET combinedQuery = query + conditions
    • 如果您附上我的程序将非常感谢
    • 非常感谢您的帮助,但在我编辑修改后的代码的地方卡住了一些,也请建议
    • 更新示例,添加输出参数。当您提出新问题时,您不应该更新您的问题,您应该创建新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多