微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql-server – 如何在引用表变量时修复“必须声明标量变量”错误?

我无法弄清楚为什么(或者你可能不能这样做)我得到了超出范围的错误

Must declare the scalar variable “@CompanyGroupSites_Master.

那么我无法在我的Cursor中以这种方式访问​​我的Table变量,或者我一定错过了一些简单的东西,当从光标体内引用时保持该表变量超出范围?

DECLARE @TotalCompaniesToProcess int 
SET @TotalCompaniesToProcess = (select distinct Count(BusinessLine) from vwBuisinessUnit)

IF(@TotalCompaniesToProcess > 0)
BEGIN

    ---------------- ############# SETUP ############# ----------------

    DECLARE @Companies TABLE (Company varchar(30))        
    Insert Into @Companies select distinct Company from Companies

    DECLARE @CompanyGroups_Added TABLE(
                                        CompanyGroupDesc varchar(50),size varchar(50)
                                      )

    DECLARE @CompanyGroupSites_Added TABLE (
                                              CompanyGroupID int,CompanyID varchar(12)                                    
                                           )

    DECLARE @CompanyGroupSites_Master TABLE (
                                              CompanyGroupID int,CompanyID varchar(12)
                                           )
    Insert into @CompanyGroupSites_Master select CompanyGroupID,CompanyID from Sec.CompanyGroupSites                        

    DECLARE @CompanyGroupID int                         
    DECLARE @Company int

    ----------------  END SETUP ----------------

    ---------------- UPDATE THE COMPANIES ----------------

    DECLARE Companies_Cursor CURSOR FOR (select distinct BusinessLine from vwBuisinessUnit)  
    --select distinct BU,BusinessLine from vwBusinessUnit

    Open Companies_Cursor

    Fetch NEXT FROM Companies_Cursor into @Company

    WHILE @@FETCH_STATUS = 0
    BEGIN

        -- is there an existing CompanyGroup for this Organization?  If not create it
        SET @CompanyGroupID = ( select CompanyGroupID from Sec.CompanyGroup 
                                where size = 'Business'
                                and CompanyGroupDesc = @Company)

        IF(@CompanyGroupID < 1) 
        BEGIN
            INSERT INTO @CompanyGroups_Added ([CompanyGroupDesc],[Size])
            VALUES (@Company,'Business')

            SET @CompanyGroupID = @@IDENTITY

            select @CompanyGroupID as CompanyGroupID_Added -- testing
        END

        Select ToDelete.* from (
            select CompanyGroupID,Company 
            from @CompanyGroupSites_Master 
            where @CompanyGroupSites_Master.CompanyGroupID = @CompanyGroupID) as ToDelete

        delete from @CompanyGroupSites_Master where @CompanyGroupSites_Master.CompanyGroupID = @CompanyGroupID

        Fetch NEXT FROM Companies_Cursor into @Company
    END


    CLOSE Companies_Cursor
    DEALLOCATE Companies_Cursor

END

解决方法

这是一个 long standing parser issue.您需要摆脱表前缀或将其包装在方括号中。

delete from @CompanyGroupSites_Master 
where CompanyGroupID = @CompanyGroupID

要么

delete from @CompanyGroupSites_Master 
where [@CompanyGroupSites_Master].CompanyGroupID = @CompanyGroupID

delete from @CompanyGroupSites_Master 
where @CompanyGroupSites_Master.CompanyGroupID = @CompanyGroupID

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐