• A table variable, like any other variable, is only in scope until the end of the batch. If you run the table variable declare, then as another batch run the update, the update will fail. Same as if you first ran a declare of any other variable, then ran later a query using that variable.

    You can't run this:

    DECLARE @i INT

    then later highlight and run this and expect it to work

    SET @i = 0

    Table variables have the same scoping rules as any other variable.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass