无法解决:'Must declare the scalar variable "@Parameter"`'
Unable to resolve: 'Must declare the scalar variable "@Parameter"`'
我需要在存储过程中下面的数据透视表中输入一个参数。但它指出,"Must declare the scalar variable "@pWeek"。"
我试过以各种方式声明它,但我无法弄明白。
当我以我想要的方式获得我想要的数字时,此编码的其他一切都很好。我只需要能够将其放入 SSRS 并能够输入参数。谢谢。
GO
/****** Object: StoredProcedure [dbo].[QB_ACCOUNT_SUMMARY] Script Date: 6/19/2015 12:42:12 PM ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[QB_ACCOUNT_SUMMARY]
@pWeek int
as
SET NOCOUNT ON
declare @AccountRef_Fullname AS NVARCHAR(MAX)
select @AccountRef_Fullname = COALESCE(@AccountRef_Fullname + ',', '') + '['+ AccountRef_Fullname + ']'
from
(
select distinct Accountref_fullname
from (select accountref_fullname from journalcreditlinedetail)JournalCreditLine
union
(select accountref_fullname from journaldebitlinedetail)
union
(select accountref_fullname from txnexpenselinedetail)
union
(select accountref_fullname from depositlinedetail)
union
(select discountaccountref_fullname from [appliedtotxndetail])
) pAccountRef_Full_Name
declare @Sql NVARCHAR(MAX)
set @Sql = N' SELECT [REAL WEEK], [LTWS WEEK], [REAL YEAR], [LTWS YEAR], [LTWS PERIODS], ' + @AccountRef_Fullname
+ 'from
(SELECT * from Account_Summary_View
where Week = @pWeek
)Account_Data '
+' PIVOT ('
+ ' sum(amount) for AccountRef_FullName in ('+ @AccountRef_Fullname +')'
+' ) AS PivotTable '
--+' )Data '
exec (@Sql)
;
改变这个:
where Week = @pWeek
为此:
where Week = ''' + CAST(@pWeek AS varchar(31)) + '''
说明:您正在使用动态 sql;将包含 SQL 命令的字符串放在一起,然后执行它。该动态字符串无法访问在其外部声明的参数和变量。所以它不识别@pWeek 参数。您没有在动态 sql 字符串中声明它。
当你按照我向你展示的方式执行时,你将@pWeek 的值连接到字符串中,这样,例如,如果你将 1
的值传递给@pWeek,那么字符串被执行的将包含 where Week = '1'
,SQL 理解起来没有问题。
您已经在现有代码中使用这一行执行此操作:
AccountRef_FullName in ('+ @AccountRef_Fullname +')'
我需要在存储过程中下面的数据透视表中输入一个参数。但它指出,"Must declare the scalar variable "@pWeek"。"
我试过以各种方式声明它,但我无法弄明白。 当我以我想要的方式获得我想要的数字时,此编码的其他一切都很好。我只需要能够将其放入 SSRS 并能够输入参数。谢谢。
GO
/****** Object: StoredProcedure [dbo].[QB_ACCOUNT_SUMMARY] Script Date: 6/19/2015 12:42:12 PM ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[QB_ACCOUNT_SUMMARY]
@pWeek int
as
SET NOCOUNT ON
declare @AccountRef_Fullname AS NVARCHAR(MAX)
select @AccountRef_Fullname = COALESCE(@AccountRef_Fullname + ',', '') + '['+ AccountRef_Fullname + ']'
from
(
select distinct Accountref_fullname
from (select accountref_fullname from journalcreditlinedetail)JournalCreditLine
union
(select accountref_fullname from journaldebitlinedetail)
union
(select accountref_fullname from txnexpenselinedetail)
union
(select accountref_fullname from depositlinedetail)
union
(select discountaccountref_fullname from [appliedtotxndetail])
) pAccountRef_Full_Name
declare @Sql NVARCHAR(MAX)
set @Sql = N' SELECT [REAL WEEK], [LTWS WEEK], [REAL YEAR], [LTWS YEAR], [LTWS PERIODS], ' + @AccountRef_Fullname
+ 'from
(SELECT * from Account_Summary_View
where Week = @pWeek
)Account_Data '
+' PIVOT ('
+ ' sum(amount) for AccountRef_FullName in ('+ @AccountRef_Fullname +')'
+' ) AS PivotTable '
--+' )Data '
exec (@Sql)
;
改变这个:
where Week = @pWeek
为此:
where Week = ''' + CAST(@pWeek AS varchar(31)) + '''
说明:您正在使用动态 sql;将包含 SQL 命令的字符串放在一起,然后执行它。该动态字符串无法访问在其外部声明的参数和变量。所以它不识别@pWeek 参数。您没有在动态 sql 字符串中声明它。
当你按照我向你展示的方式执行时,你将@pWeek 的值连接到字符串中,这样,例如,如果你将 1
的值传递给@pWeek,那么字符串被执行的将包含 where Week = '1'
,SQL 理解起来没有问题。
您已经在现有代码中使用这一行执行此操作:
AccountRef_FullName in ('+ @AccountRef_Fullname +')'