sql server中的模糊列名

时间:2014-03-06 08:25:28

标签: sql

你好,我在m存储过程中有一个模糊的列名称付款.bid-id有人可以帮忙解决这个问题吗?

  SET NOCOUNT ON;
  SELECT ROW_NUMBER() OVER
  (
  ORDER BY [PaymentID] ASC
  )AS RowNumber
  ,[PaymentID]
  ,[Name]
  ,[WinningPrice]
  ,[PaymentDate]
   ,[Payment.BidID]
 INTO #Results
 FROM  Item INNER JOIN
                         Auction ON Item.ItemID = Auction.ItemID INNER JOIN
                         BID ON Auction.AuctionID = BID.AuctionID INNER JOIN
                         Payment ON BID.BidID = Payment.BidID
                         Where   (BID.Status = 'Paid') AND (BID.BuyerID = @buyer)

  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
End

4 个答案:

答案 0 :(得分:2)

您在查询中使用的列名称可在多个表中使用。

如果没有表格结构,我们无法确定它是哪一个,但可能在您的查询中有一个别名:

,[PaymentID]
,[Name]
,[WinningPrice]
,[PaymentDate]

答案 1 :(得分:1)

试试这个,为不明确的列名创建别名

 SET NOCOUNT ON;
  SELECT ROW_NUMBER() OVER
  (
  ORDER BY [PaymentID] ASC
  )AS RowNumber
  ,[PaymentID]
  ,[Name]
  ,[WinningPrice]
  ,[PaymentDate]
   ,[Payment.BidID] as PBidID
 INTO #Results
 FROM  Item INNER JOIN
                         Auction ON Item.ItemID = Auction.ItemID INNER JOIN
                         BID ON Auction.AuctionID = BID.AuctionID INNER JOIN
                         Payment ON BID.BidID = Payment.BidID
                         Where   (BID.Status = 'Paid') AND (BID.BuyerID = @buyer)

  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
End

答案 2 :(得分:0)

 SET NOCOUNT ON;
  SELECT ROW_NUMBER() OVER
  (
  ORDER BY [PaymentID] ASC
  )AS RowNumber
  ,[PaymentID]
  ,[Name]
  ,[WinningPrice]
  ,[PaymentDate]
   ,[Payment.BidID]
 INTO #Results
 FROM  Item INNER JOIN
                         Auction ON Item.ItemID = Auction.ItemID INNER JOIN
                         BID ON Auction.AuctionID = BID.AuctionID INNER JOIN
                         **Payment P1 ON BID.BidID = P1.BidID**
                         Where   (BID.Status = 'Paid') AND (BID.BuyerID = @buyer)

  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
End

希望这会有所帮助..

突出显示我更改的行

答案 3 :(得分:0)

良好做法是使用以下别名:

SET NOCOUNT ON;
SELECT ROW_NUMBER() OVER
    (
        ORDER BY i.[PaymentID] ASC  --which table it belongs? put correct alias
    )AS RowNumber
    ,i.[PaymentID]                  
    ,i.[Name]
    ,i.[WinningPrice]
    ,i.[PaymentDate]
    ,p.[BidID]
INTO #Results
FROM  Item i
INNER JOIN Auction a 
    ON i.ItemID = a.ItemID 
INNER JOIN  BID b
    ON a.AuctionID = b.AuctionID 
INNER JOIN Payment p 
    ON b.BidID = p.BidID
Where   (b.Status = 'Paid') 
        AND (b.BuyerID = @buyer)

SELECT @RecordCount = COUNT(*)
FROM #Results