USE pubs
GO

CREATE LOGIN JAY WITH PASSWORD = 'StrongPW1'
CREATE USER JAY FOR LOGIN JAY
GO

-- table FOO_TABLE is owned by DBO.
-- using the count_rows_as_me procedure from the previous example
SETUSER JAY
GO
-- this checks permissions if JAY is not DBO
count_rows_as_me 'foo_table'
—
—
SETUSER FRED
GO
CREATE PROCEDURE select_and_count
AS
SELECT * FROM customers
count_rows_as_me 'foo_table'
GO

-- this does two ownership checks
-- even if FRED is DBO
select_and_count
GO