在存储过程中执行 FuncView
我正在尝试修改 SQL 2000 中现有的 SPROC。原始 SPROC 使用 FuncView 作为游标的一部分。 新报告不需要游标,但我不确定如何在游标语句之外执行 FuncView。 下面的第一个例子是原始代码的光标部分。 第二个是我的修改......以及由此产生的错误消息。 请帮忙...
DECLARE @FuncView_PlantConditionAssessmentMetrics VARCHAR(8000)
DECLARE FuncView_PlantConditionAssessmentMetricsCursor CURSOR FOR
Select
'UPDATE #rsTemp
SET TotalComponentsOnTask = rsMetric.TotalComponentsOnTask,
TotalComponentsAssessedMeasured = rsMetric.TotalComponentsAssessedMeasured,
TotalComponentsNotMeasured = rsMetric.TotalComponentsNotMeasured,
TotalComponentsAssessedNotMeasured = rsMetric.TotalComponentsAssessedNotMeasured,
OverdueElevatedItems = rsMetric.OverdueElevatedItems,
OverdueAdvisoryItems = rsMetric.OverdueAdvisoryItems
FROM ['+DBCatalog+'].[dbo].FuncView_PlantConditionAssessmentMetrics ('''+CONVERT(varchar(50), @StartDate, 100)+''','''+ CONVERT(varchar(50), @EndDate, 100)+''') rsMetric
WHERE DBCatalog = '''+DBCatalog+''''
FROM [HostedTangoSettingsSQL].[dbo].[DatabaseTable]
WHERE CustomerID = @CustomerID
AND
HostedDBTypeID IN (SELECT HostedDBTypeID FROM [HostedTangoSettingsSQL].[dbo].defs_HostedDBTypes
WHERE IsActiveDatabase = 1 AND HostedDBTypeID <> 3)
OPEN FuncView_PlantConditionAssessmentMetricsCursor
FETCH NEXT FROM FuncView_PlantConditionAssessmentMetricsCursor INTO @FuncView_PlantConditionAssessmentMetrics
WHILE (@@fetch_status <> -1)
BEGIN
EXEC(@FuncView_PlantConditionAssessmentMetrics)
FETCH NEXT FROM FuncView_PlantConditionAssessmentMetricsCursor INTO @FuncView_PlantConditionAssessmentMetrics
END
CLOSE FuncView_PlantConditionAssessmentMetricsCursor
DEALLOCATE FuncView_PlantConditionAssessmentMetricsCursor
DECLARE @FuncView_PlantConditionAssessmentMetrics VARCHAR(8000)
SELECT FuncView_PlantConditionAssessmentMetrics = @FuncView_PlantConditionAssessmentMetrics
'UPDATE #rsTemp
SET TotalComponentsOnTask = rsMetric.TotalComponentsOnTask,
TotalComponentsAssessedMeasured = rsMetric.TotalComponentsAssessedMeasured,
TotalComponentsNotMeasured = rsMetric.TotalComponentsNotMeasured,
TotalComponentsAssessedNotMeasured = rsMetric.TotalComponentsAssessedNotMeasured,
OverdueElevatedItems = rsMetric.OverdueElevatedItems,
OverdueAdvisoryItems = rsMetric.OverdueAdvisoryItems
FROM ['+@CustomerDesc+'].[dbo].FuncView_PlantConditionAssessmentMetrics ('''+CONVERT(varchar(50), @StartDate, 100)+''','''+ CONVERT(varchar(50), @EndDate, 100)+''') rsMetric
WHERE DBCatalog = '''+@CustomerDesc+''''
Server: Msg 170, Level 15, State 1, Procedure UDR_PlantConditionAssessmentMetrics, Line 54
Line 54: Incorrect syntax near 'UPDATE #rsTemp
SET TotalComponentsOnTask = rsMetric.TotalComponentsOnTask,
TotalComponentsAssessedMeasured = rsMetric.To'.
I am trying to modify an exisiting SPROC in SQL 2000. The original SPROC used a FuncView as part of a a cursor. The new report does not require the cursor, but I am unsure how to execute the FuncView outside of the cursor statement. The first example below is the cursor part of the original code. The second is my modification....with the resulting error message. Please help...
DECLARE @FuncView_PlantConditionAssessmentMetrics VARCHAR(8000)
DECLARE FuncView_PlantConditionAssessmentMetricsCursor CURSOR FOR
Select
'UPDATE #rsTemp
SET TotalComponentsOnTask = rsMetric.TotalComponentsOnTask,
TotalComponentsAssessedMeasured = rsMetric.TotalComponentsAssessedMeasured,
TotalComponentsNotMeasured = rsMetric.TotalComponentsNotMeasured,
TotalComponentsAssessedNotMeasured = rsMetric.TotalComponentsAssessedNotMeasured,
OverdueElevatedItems = rsMetric.OverdueElevatedItems,
OverdueAdvisoryItems = rsMetric.OverdueAdvisoryItems
FROM ['+DBCatalog+'].[dbo].FuncView_PlantConditionAssessmentMetrics ('''+CONVERT(varchar(50), @StartDate, 100)+''','''+ CONVERT(varchar(50), @EndDate, 100)+''') rsMetric
WHERE DBCatalog = '''+DBCatalog+''''
FROM [HostedTangoSettingsSQL].[dbo].[DatabaseTable]
WHERE CustomerID = @CustomerID
AND
HostedDBTypeID IN (SELECT HostedDBTypeID FROM [HostedTangoSettingsSQL].[dbo].defs_HostedDBTypes
WHERE IsActiveDatabase = 1 AND HostedDBTypeID <> 3)
OPEN FuncView_PlantConditionAssessmentMetricsCursor
FETCH NEXT FROM FuncView_PlantConditionAssessmentMetricsCursor INTO @FuncView_PlantConditionAssessmentMetrics
WHILE (@@fetch_status <> -1)
BEGIN
EXEC(@FuncView_PlantConditionAssessmentMetrics)
FETCH NEXT FROM FuncView_PlantConditionAssessmentMetricsCursor INTO @FuncView_PlantConditionAssessmentMetrics
END
CLOSE FuncView_PlantConditionAssessmentMetricsCursor
DEALLOCATE FuncView_PlantConditionAssessmentMetricsCursor
DECLARE @FuncView_PlantConditionAssessmentMetrics VARCHAR(8000)
SELECT FuncView_PlantConditionAssessmentMetrics = @FuncView_PlantConditionAssessmentMetrics
'UPDATE #rsTemp
SET TotalComponentsOnTask = rsMetric.TotalComponentsOnTask,
TotalComponentsAssessedMeasured = rsMetric.TotalComponentsAssessedMeasured,
TotalComponentsNotMeasured = rsMetric.TotalComponentsNotMeasured,
TotalComponentsAssessedNotMeasured = rsMetric.TotalComponentsAssessedNotMeasured,
OverdueElevatedItems = rsMetric.OverdueElevatedItems,
OverdueAdvisoryItems = rsMetric.OverdueAdvisoryItems
FROM ['+@CustomerDesc+'].[dbo].FuncView_PlantConditionAssessmentMetrics ('''+CONVERT(varchar(50), @StartDate, 100)+''','''+ CONVERT(varchar(50), @EndDate, 100)+''') rsMetric
WHERE DBCatalog = '''+@CustomerDesc+''''
Server: Msg 170, Level 15, State 1, Procedure UDR_PlantConditionAssessmentMetrics, Line 54
Line 54: Incorrect syntax near 'UPDATE #rsTemp
SET TotalComponentsOnTask = rsMetric.TotalComponentsOnTask,
TotalComponentsAssessedMeasured = rsMetric.To'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇,游标和动态 SQL; 你正在接触各种坏习惯,不是吗:)这是学习一些东西的好方法,但在维护方面却很糟糕。
看起来你的代码需要这样调整:
为了使其与之前的代码一致。
WOW, cursors AND dynamic SQL; you're getting exposed to all kinds of bad habits, aren't you :) It's a good way to learn something, but it sucks when it comes to maintenance.
It looks like your code needs to be adjusted thusly:
In order to make it consistent with the previous code.