Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

sql server - How to fix "Must declare the scalar variable" error when referencing table variable?

I can't figure out why (or maybe you just can't do this) I get the out of scope error

Must declare the scalar variable "@CompanyGroupSites_Master.

So is it that I cannot access my Table variable this way inside my Cursor, or I must have missed something simple that's keeping that table variable out of scope when referencing from within the cursor body?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a long standing parser issue. You need to get rid of the table prefix or wrap it in square brackets.

i.e.

delete from @CompanyGroupSites_Master 
where CompanyGroupID = @CompanyGroupID

or

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

Not

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...