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
的变量,我只想在其中追加包含各种和/或运算符的查询,我该怎么做?
提前致谢
答案 0 :(得分:0)
我相信您应该将查询准备为字符串,然后使用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