Добавление хранимой процедуры с запросом

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, я просто хочу добавить в нее запрос, который содержит различные и и / или операторы, как я могу это сделать?

заранее спасибо

asp.net,stored-procedures,sql-server-2012,

0

Ответов: 1


0 принят

Я считаю, что вы должны подготовить запрос как строку, а затем использовать динамический 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
asp.net, хранимая-процедура, SQL-сервер 2012,
Похожие вопросы
Яндекс.Метрика