【发布时间】:2012-02-23 09:22:34
【问题描述】:
我只想获取查询的前 x 个数字。 x 将通过参数发送到哪里我该怎么做?
我知道我可以把它当作字符串稍后执行,但我觉得它很麻烦。
这是我的查询,@ArticleNo 是我想要作为 x 的参数。
Create proc [dbo].[GW2_Report_SlowFastMovingArticle]
@ArtKey bigint = null,
@ArtCatKey int= null,
@ArtGroupKey int= null,
@ArtTypeKey int= null,
@MaterialKey int= null,
@ColorKey int= null,
@VendorTypeKey int = null,
@VendorKey bigint = null,
@FromDate datetime = null,
@ToDate datetime = null,
@MovingType int = 0,
@PerformanceType int = 0,
@ArticleNo int = 10,
@OutletKey int = null
AS
BEGIN
SELECT
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName,
Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
into #temp FROM
dbo.Sal_POS INNER JOIN
dbo.Sal_POSDet ON
dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
dbo.ArtWithVendor ON
dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey
WHERE
Sal_POS.IsHold=0 and
Sal_POS.SalesDate between @FromDate and @ToDate and
CASE WHEN @ArtKey is null THEN 1 WHEN ArtWithVendor.ArtKey =@ArtKey THEN 1 ELSE 0 END = 1
and CASE WHEN @ArtCatKey is null THEN 1 WHEN ArtWithVendor.ArtCatKey =@ArtCatKey THEN 1 ELSE 0 END = 1
and CASE WHEN @ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = @ArtGroupKey THEN 1 ELSE 0 END = 1
and CASE WHEN @ArtTypeKey is null THEN 1 WHEN ArtWithVendor.ArtTypeKey = @ArtTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN @MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = @MaterialKey THEN 1 ELSE 0 END = 1
and CASE WHEN @ColorKey is null THEN 1 WHEN ArtWithVendor.ColorKey = @ColorKey THEN 1 ELSE 0 END = 1
and CASE WHEN @VendorKey is null THEN 1 WHEN ArtWithVendor.VendorKey = @VendorKey THEN 1 ELSE 0 END = 1
and CASE WHEN @VendorTypeKey is null THEN 1 WHEN ArtWithVendor.VendorTypeKey = @VendorTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN @OutLetKey is null THEN 1 WHEN Sal_POS.OutLetKey = @OutLetKey THEN 1 ELSE 0 END = 1
Group by
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName
if(@PerformanceType=0 and @MovingType=0)
begin
select * from #temp
order by Pair asc
end
if(@PerformanceType=0 and @MovingType=1)
begin
select * from #temp
order by Pair desc
end
if(@PerformanceType=1 and @MovingType=0)
begin
select * from #temp
order by turnover asc
end
if(@PerformanceType=1 and @MovingType=1)
begin
select * from #temp
order by turnover desc
end
END
【问题讨论】:
-
这适用于哪个版本的 SQL-Server?
-
@Taufiq:您接受的答案是危险。使用 rjdevereux 的答案