Stored procedures 和(dbo.getdecommissiondate(a.uid)>@StartDate) 按a.uid分组 设置@months-=3 结束' EXEC(@Query) 您在哪里声明您的@帐户和@查询变量?看起来其中一个可能是intNo,两个都是varcha

Stored procedures 和(dbo.getdecommissiondate(a.uid)>@StartDate) 按a.uid分组 设置@months-=3 结束' EXEC(@Query) 您在哪里声明您的@帐户和@查询变量?看起来其中一个可能是intNo,两个都是varcha,stored-procedures,reporting-services,Stored Procedures,Reporting Services,和(dbo.getdecommissiondate(a.uid)>@StartDate) 按a.uid分组 设置@months-=3 结束' EXEC(@Query) 您在哪里声明您的@帐户和@查询变量?看起来其中一个可能是intNo,两个都是varchars。我已经编辑了代码。谢谢您在哪里声明您的@帐户和@查询变量?看起来其中一个可能是intNo,两个都是varchars。我已经编辑了代码。谢谢你,我不得不稍微修改一下你的代码(一次声明两次),还有两个拼写错误,但它给了我我需要的解决方案。非常

和(dbo.getdecommissiondate(a.uid)>@StartDate) 按a.uid分组 设置@months-=3 结束' EXEC(@Query)
您在哪里声明您的
@帐户
@查询
变量?看起来其中一个可能是intNo,两个都是varchars。我已经编辑了代码。谢谢您在哪里声明您的
@帐户
@查询
变量?看起来其中一个可能是intNo,两个都是varchars。我已经编辑了代码。谢谢你,我不得不稍微修改一下你的代码(一次声明两次),还有两个拼写错误,但它给了我我需要的解决方案。非常感谢!谢谢,我不得不稍微修改一下你的代码(一次声明修改两次)和两个拼写错误,但它给了我所需要的解决方案。非常感谢!
CREATE TABLE #tempScaffStandingByTime
(
    TotalStanding INT,
    MonthsAgo INTEGER
) 

DECLARE @StartDate DATETIME = null
DECLARE @months INTEGER = 12

use [Safetrak-BradyTechUK]
WHILE @months >= 0
BEGIN
   SET @StartDate = DATEADD(mm, -12 + @months, DATEADD(mm, 0, DATEADD(mm, 
   DATEDIFF(mm,0,GETDATE()-1), 1)))
   INSERT INTO #tempScaffStandingByTime
   select TOP 1 COUNT(*) OVER () AS TotalRecords, @months
   from asset a
   join wshhistory h on h.assetid = a.uid
   where a.deleted = 0 and h.assetstate <> 6 
   AND (dbo.getdecommissiondate(a.uid) > @StartDate)
   group by a.uid
   SET @months -= 3
END

SELECT * FROM #tempScaffStandingByTime

DROP TABLE #tempScaffStandingByTime
        DECLARE @Query  varchar (8000)
              , @Account varchar (100) = 'BradyTechUK'

        SET @Account = 'USE [Safetrak-' + @Account + ']'

/************************************************************************************/

/********** Create Table to hold data ***********************************************/

            CREATE TABLE #tempScaffStandingByTime
            (
                TotalStanding INT,
                MonthsAgo INTEGER
            ) 

/************************************************************************************/

/********** Populate temp table with data *******************************************/

    DECLARE @StartDate DATETIME = null
    DECLARE @months INTEGER = 12

    SET @Query= +@Account+ ' 
            WHILE '+@months+' >= 0
                BEGIN
                    SET '+@StartDate+' = DATEADD(mm, -12 + ('+@months+', DATEADD(mm, 0, DATEADD(mm, DATEDIFF(mm,0,GETDATE()-1), 1)))
                    INSERT INTO #tempScaffStandingByTime
                    select TOP 1 COUNT(*) OVER () AS TotalRecords, '+@months+'
                    from asset a
                    join wshhistory h on h.assetid = a.uid
                    where a.deleted = 0 and h.assetstate <> 6 
                    AND (dbo.getdecommissiondate(a.uid) > '+@StartDate+')
                    group by a.uid
                    SET '+@months+' -= 3
                END'
    EXEC (@Query)

/************************************************************************************/

/********** Select Statement to return data to sp ***********************************/

    Select  TotalStanding
            , MonthsAgo
        FROM #tempScaffStandingByTime

/************************************************************************************/

 DROP TABLE  #tempScaffStandingByTime
    SET @Query= @Account + '
    DECLARE @StartDate DATETIME = null
    DECLARE @months VARCHAR(2) = ''12''

        WHILE @months >= 0
            BEGIN
                SET @StartDate = DATEADD(mm, -12 + @months, DATEADD(mm, 0, DATEADD(mm, DATEDIFF(mm,0,GETDATE()-1), 1)))
                INSERT INTO #tempScaffStandingByTime
                select TOP 1 COUNT(*) OVER () AS TotalRecords, @months, @StartDate
                from asset a
                join wshhistory h on h.assetid = a.uid
                where a.deleted = 0 and h.assetstate <> 6 
                AND (dbo.getdecommissiondate(a.uid) > @StartDate)
                group by a.uid
                SET @months -= 3
            END'    
    EXEC (@Query)