What @@ are for in variables?

  • Hello.

    Looking into the Trigger BOL help I realized into one example that uses @@VarName

    CREATE TRIGGER employee_insupd
    ON employee
    FOR INSERT, UPDATE
    AS
    /* Get the range of level for this job type from the jobs table. */
    DECLARE @@MIN_LVL tinyint,
       @@MAX_LVL tinyint,
       @@EMP_LVL tinyint,
       @@JOB_ID smallint
    SELECT @@MIN_LVl = min_lvl, 
       @@MAX_LV = max_lvl, 
       @@ EMP_LVL = i.job_lvl,
       @@JOB_ID = i.job_id
    FROM employee e, jobs j, inserted i 
    WHERE e.emp_id = i.emp_id AND i.job_id = j.job_id
    IF (@@JOB_ID = 1) and (@@EMP_lVl <> 10) 
    BEGIN
       RAISERROR ('Job id 1 expects the default level of 10.', 16, 1)
       ROLLBACK TRANSACTION
    END
    ELSE
    IF NOT @@ EMP_LVL BETWEEN @@MIN_LVL AND @@MAX_LVL)
    BEGIN
       RAISERROR ('The level for job_id:%d should be between %d and %d.',
          16, 1, @@JOB_ID, @@MIN_LVL, @@MAX_LVL)
       ROLLBACK TRANSACTION
    END

    What are @@ for? What's the difference between using a single @?

    I've benn looking in BOL, but can't find anything helpful

    Thanks

  • @@ is a global value

    @ is a local value exist only on that connection.

     

    mom

  • Thanks, I suspected that.

    And in the example, what are the benefits of using global variables instead of using local ones?

  • Not specifically for this example, but the only benefit I've ever known for a Global Variable is if you have multi processes which need to know what that variable is at that particular given moment, regardless of what processes you have running. It stays up and available to other connections. Since I want full control of what the variable is at all times, then I only use local variables. I suppose if I didn't want to pass a variable value to a nested sproc, then I could use a global, but I'd rather pass the value in the exec statement.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply