在存储过程中执行 FuncView

发布于 2024-07-26 18:37:30 字数 2963 浏览 1 评论 0原文

我正在尝试修改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛起拖把扫天下 2024-08-02 18:37:30

哇,游标和动态 SQL; 你正在接触各种坏习惯,不是吗:)这是学习一些东西的好方法,但在维护方面却很糟糕。

看起来你的代码需要这样调整:

SELECT  @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 + ''''

为了使其与之前的代码一致。

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:

SELECT  @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 + ''''

In order to make it consistent with the previous code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文