插入一个存储过程到一个temptable,temptablereturns为空
Inserting a stored procedure into a temp table, temp table returns empty
由于我无法控制的原因,我必须一次查询一个设施 ID 的某些程序,这意味着我需要遍历所有设施并注入临时 table 才能利用高效数据。
我已经多次使用这个循环,从来没有遇到过问题。由于这个特定查询的某种原因,temp table 返回空,我不知道为什么。
存储过程自行完美执行,整个查询运行没有抛出任何错误。但是,当我 select * #HistoricalRentalActivity 我得到一个空白 table (但正确 headers)
IF OBJECT_ID('tempdb..#HistoricalRentalActivity') IS NOT NULL
DROP TABLE #HistoricalRentalActivity
IF OBJECT_ID('tempdb..#rowsiteHistoricalRentalActivity') IS NOT NULL
DROP TABLE #rowsiteHistoricalRentalActivity
SELECT
ROW_NUMBER() OVER (ORDER BY siteid) AS rownum, siteid
INTO #rowsiteHistoricalRentalActivity
FROM dbo.sites
CREATE TABLE #HistoricalRentalActivity
(
SiteID INT,
iSortOrder INT,
sDescActivity VARCHAR (200),
dcM1 MONEY,
dcM2 MONEY,
dcM3 MONEY,
dcM4 MONEY,
dcM5 MONEY,
dcM6 MONEY,
dcM7 MONEY,
dcM8 MONEY,
dcM9 MONEY,
dcM10 MONEY,
dcM11 MONEY,
dcM12 MONEY,
dcM13 MONEY,
dcM14 MONEY,
dc3MonthAvg MONEY
)
DECLARE @enddate datetime
SET @enddate = DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
DECLARE @maxrow INT
SET @maxrow = (SELECT COUNT(siteid) FROM #HistoricalRentalActivity)
DECLARE @row INT
SET @row = 1
WHILE(@row <= @maxrow)
BEGIN
DECLARE @siteidrow INT
SET @siteidrow = (SELECT siteid FROM #rowsiteHistoricalRentalActivity WHERE rownum = @row)
INSERT INTO #HistoricalRentalActivity (SiteID, iSortOrder, sDescActivity,
dcM1, dcM2, dcM3, dcM4, dcM5, dcM6, dcM7,
dcM8, dcM9, dcM10, dcM11, dcM12, dcM13, dcM14,
dc3MonthAvg)
EXEC dbo.spRptMgmtHistorytb
@SiteID = @siteidrow,
@dEnd = @enddate
SET @row = @row + 1
END
我很茫然,希望其他人有想法?
maxRow
需要从 #rowSiteHistoricalRentalActivity
而不是您刚刚创建的 table 获取计数。
由于我无法控制的原因,我必须一次查询一个设施 ID 的某些程序,这意味着我需要遍历所有设施并注入临时 table 才能利用高效数据。
我已经多次使用这个循环,从来没有遇到过问题。由于这个特定查询的某种原因,temp table 返回空,我不知道为什么。
存储过程自行完美执行,整个查询运行没有抛出任何错误。但是,当我 select * #HistoricalRentalActivity 我得到一个空白 table (但正确 headers)
IF OBJECT_ID('tempdb..#HistoricalRentalActivity') IS NOT NULL
DROP TABLE #HistoricalRentalActivity
IF OBJECT_ID('tempdb..#rowsiteHistoricalRentalActivity') IS NOT NULL
DROP TABLE #rowsiteHistoricalRentalActivity
SELECT
ROW_NUMBER() OVER (ORDER BY siteid) AS rownum, siteid
INTO #rowsiteHistoricalRentalActivity
FROM dbo.sites
CREATE TABLE #HistoricalRentalActivity
(
SiteID INT,
iSortOrder INT,
sDescActivity VARCHAR (200),
dcM1 MONEY,
dcM2 MONEY,
dcM3 MONEY,
dcM4 MONEY,
dcM5 MONEY,
dcM6 MONEY,
dcM7 MONEY,
dcM8 MONEY,
dcM9 MONEY,
dcM10 MONEY,
dcM11 MONEY,
dcM12 MONEY,
dcM13 MONEY,
dcM14 MONEY,
dc3MonthAvg MONEY
)
DECLARE @enddate datetime
SET @enddate = DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
DECLARE @maxrow INT
SET @maxrow = (SELECT COUNT(siteid) FROM #HistoricalRentalActivity)
DECLARE @row INT
SET @row = 1
WHILE(@row <= @maxrow)
BEGIN
DECLARE @siteidrow INT
SET @siteidrow = (SELECT siteid FROM #rowsiteHistoricalRentalActivity WHERE rownum = @row)
INSERT INTO #HistoricalRentalActivity (SiteID, iSortOrder, sDescActivity,
dcM1, dcM2, dcM3, dcM4, dcM5, dcM6, dcM7,
dcM8, dcM9, dcM10, dcM11, dcM12, dcM13, dcM14,
dc3MonthAvg)
EXEC dbo.spRptMgmtHistorytb
@SiteID = @siteidrow,
@dEnd = @enddate
SET @row = @row + 1
END
我很茫然,希望其他人有想法?
maxRow
需要从 #rowSiteHistoricalRentalActivity
而不是您刚刚创建的 table 获取计数。