内部加入 order by 和 where 子句
Inner Join with order by and where clase
我创建了一个用于填充下拉列表的存储过程。但是 order by 子句不适用于我的程序。
ALTER PROCEDURE proc
-- Add the parameters for the stored procedure here
@compID bigint,
@after datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
CREATE TABLE #tmpAuc ( ProjectID BIGINT, Title VARCHAR(256))
INSERT INTO #tmpAuc
SELECT SA.ID ProjectID, SA.Title
FROM [dbo].[Sessions] S
INNER JOIN Auc SA ON S.AucID = SA.ID
WHERE S.Session < 3 AND SA.Status > 0 AND SA.CompanyID = @companyID AND S.LiveBeginDate > @after
ORDER BY LiveBeginDate
SELECT DISTINCT * FROM #tmpAuc
END
我想按 LiveBehinDate 降序排列
在临时 table 和临时 table 结果中包含 LiveBeginDate
ORDER BY LiveBeginDate
CREATE TABLE #tmpAuctions (ProjectID BIGINT, Title VARCHAR(256), LiveBeginDate DATETIME)
INSERT INTO #tmpAuctions (ProjectID, Title, LiveBeginDate)
SELECT SA.ID AS ProjectID, SA.Title, S.LiveBeginDate
FROM [dbo].[Sessions] S
INNER JOIN [Spectrum_Auctions].[dbo].[Auctions] SA ON S.AuctionID = SA.ID
WHERE S.SessionState < 3 AND SA.Status > 0 AND SA.CompanyID = @companyID AND S.LiveBeginDate > @after
SELECT DISTINCT *
FROM #tmpAuctions
ORDER BY LiveBeginDate
或避免使用 temp table 并直接在过程中使用 SELECT 和 JOIN:
SELECT SA.ID AS ProjectID, SA.Title
FROM [dbo].[Sessions] S
INNER JOIN [Spectrum_Auctions].[dbo].[Auctions] SA ON S.AuctionID = SA.ID
WHERE S.SessionState < 3 AND SA.Status > 0 AND SA.CompanyID = @companyID AND S.LiveBeginDate > @after
ORDER BY S.LiveBeginDate
那是因为您的数据在写入驻留在 temp dB 中的 temp table 时是有序的。永远不能保证从 temp dB 读取是有序的。因此,当您 select 从您的温度 table 中加星时,这就是您得到的。
摆脱临时 table 并直接执行 select。这也将更快、更有效。如果您的过程变得更复杂,请使用 CTE 而不是临时 tables,因为它们更容易概念化并且 运行 在所有情况下都快得多。
我创建了一个用于填充下拉列表的存储过程。但是 order by 子句不适用于我的程序。
ALTER PROCEDURE proc
-- Add the parameters for the stored procedure here
@compID bigint,
@after datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
CREATE TABLE #tmpAuc ( ProjectID BIGINT, Title VARCHAR(256))
INSERT INTO #tmpAuc
SELECT SA.ID ProjectID, SA.Title
FROM [dbo].[Sessions] S
INNER JOIN Auc SA ON S.AucID = SA.ID
WHERE S.Session < 3 AND SA.Status > 0 AND SA.CompanyID = @companyID AND S.LiveBeginDate > @after
ORDER BY LiveBeginDate
SELECT DISTINCT * FROM #tmpAuc
END
我想按 LiveBehinDate 降序排列
在临时 table 和临时 table 结果中包含 LiveBeginDate
ORDER BY LiveBeginDate
CREATE TABLE #tmpAuctions (ProjectID BIGINT, Title VARCHAR(256), LiveBeginDate DATETIME)
INSERT INTO #tmpAuctions (ProjectID, Title, LiveBeginDate)
SELECT SA.ID AS ProjectID, SA.Title, S.LiveBeginDate
FROM [dbo].[Sessions] S
INNER JOIN [Spectrum_Auctions].[dbo].[Auctions] SA ON S.AuctionID = SA.ID
WHERE S.SessionState < 3 AND SA.Status > 0 AND SA.CompanyID = @companyID AND S.LiveBeginDate > @after
SELECT DISTINCT *
FROM #tmpAuctions
ORDER BY LiveBeginDate
或避免使用 temp table 并直接在过程中使用 SELECT 和 JOIN:
SELECT SA.ID AS ProjectID, SA.Title
FROM [dbo].[Sessions] S
INNER JOIN [Spectrum_Auctions].[dbo].[Auctions] SA ON S.AuctionID = SA.ID
WHERE S.SessionState < 3 AND SA.Status > 0 AND SA.CompanyID = @companyID AND S.LiveBeginDate > @after
ORDER BY S.LiveBeginDate
那是因为您的数据在写入驻留在 temp dB 中的 temp table 时是有序的。永远不能保证从 temp dB 读取是有序的。因此,当您 select 从您的温度 table 中加星时,这就是您得到的。 摆脱临时 table 并直接执行 select。这也将更快、更有效。如果您的过程变得更复杂,请使用 CTE 而不是临时 tables,因为它们更容易概念化并且 运行 在所有情况下都快得多。