Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8678643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:46:30+00:00 2026-06-12T20:46:30+00:00

I need a little help with a query I am writing. I have broken

  • 0

I need a little help with a query I am writing. I have broken it down to its most simple form.
An example of my table structure is shown below (I have altered it for clarity):

Users
    UserId int -- PK Identity
    Username    varchar(30)

DirectReports
    DirectReportId  int -- PK Identity
    UserId int -- FK to Users.UserId
    ManagerId -- FK to Users.UserId

Documents
    DocumentId int -- PK Identity
    DocumentOwner int -- FK to Users.UserId
    DocumentName varchar(30)
    CreatedBy int -- FK to Users.UserId
    CreatedDate datetime

What I need to do, is allow users who have created a document to be able to see their own document.
So I use:

CREATE PROCEDURE GetUsersDocuments
    @UserId         INT = null
AS
BEGIN

    SET NOCOUNT ON;

    Select * from Documents D
    Where D.CreatedBy = ISNULL(@UserId, D.CreatedBy)
END

Which returns all documents created by a specific user.

However the business rules of the model, also dictate that another user may create a document on behalf of a user. So a user needs visibility of all records created by themselves, and all documents which they have ownership of:

CREATE PROCEDURE GetUsersDocuments
    @UserId         INT = null
AS
BEGIN

    SET NOCOUNT ON;

    Select * from Documents D
    Where D.CreatedBy = ISNULL(@UserId, D.CreatedBy)
    OR D.DocumentOwner = ISNULL(@UserId, D.DocumentOwner)
END

All works well. However, I have just been informed, that all direct reports of a user should have visibility of both documents created by a User, and documents a User has ownership of.

Given the example table structure I have defined, how would I express this in terms of a query?

Many Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:46:31+00:00Added an answer on June 12, 2026 at 8:46 pm
    Select * from Documents D 
    Where D.CreatedBy = ISNULL(@UserId, D.CreatedBy) 
        OR D.DocumentOwner = ISNULL(@UserId, D.DocumentOwner)  
        OR D.DocumentOwner = ( select ManagerID from DirectReports where @UserId = UserID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a little help brainstorming on a query. I have a sales table
I need a little help writing a query. I have this data... vDir iNumber
Need a little help with a SQL / ActiveRecord query. Let's say I have
i need some help with this sql query heres my mysql table structure: DOMAINS
i need a little help with a AR query. This is how my models
I have figured out most of my SQL query, but I seem to need
Just writing a little function here and need some optimisation help! All requests redirect
I need a little help with my PHP. Basically I have a field a
I need a little help for one sql query; here is my basic user
Ok, I need a little help. I have these two PHP classes class menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.