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 8223311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:39:03+00:00 2026-06-07T14:39:03+00:00

I have a fairly simple database consisting of three (relevant) tables: users, permissions, and

  • 0

I have a fairly simple database consisting of three (relevant) tables: users, permissions, and user permissions. The basic premise is simple: when a user gets created, all the records in the permissions table are automatically added to the user_permissions table with a default value. This is all working fine.

However, as I’m currently in development, I continue to add new permissions, which of course existing users won’t have since those new permissions didn’t exist in the permissions table when they were created. So, I had the brilliant idea to create a little stored procedure to automatically update the user_permissions table with all the permissions not currently existing in the user_permissions table.

So, in short, what I want to do is something like (pseudocode)

For each user without x permission in user_permissions, insert into user_permissions user_id and permission_id

I wasn’t quite sure how to do this from an SQL POV. I played with joins and “not exists” but haven’t really gotten anywhere.

You can play with my schema here: http://sqlfiddle.com/#!3/b0761/3
Thanks in advance for the help!

EDIT: Schema:

CREATE TABLE users (
  user_id      int IDENTITY(1, 1) NOT NULL,
  user_name    varchar(255),
  PRIMARY KEY (user_id));

CREATE TABLE permissions (
  permission_id          int IDENTITY(1, 1) NOT NULL, 
  permission_name        varchar(255) NOT NULL, 
  PRIMARY KEY (permission_id));

CREATE TABLE user_permissions (
  user_id       int NOT NULL, 
  permission_id int NOT NULL, 
  value         tinyint DEFAULT 0 NOT NULL,  
  PRIMARY KEY (user_id, 
  permission_id));

ALTER TABLE user_permissions ADD CONSTRAINT FK_user_pe338140 
FOREIGN KEY (permission_id)
REFERENCES permissions (permission_id);

ALTER TABLE user_permissions ADD CONSTRAINT FK_user_pe405324 
FOREIGN KEY (user_id) REFERENCES users (user_id);

INSERT INTO users(user_name) values('test_username');
INSERT INTO users(user_name) values('test_username2');

INSERT INTO permissions(permission_name) VALUES('permission_1')
INSERT INTO permissions(permission_name) VALUES('permission_2')

INSERT INTO user_permissions(user_id, permission_id, value)
VALUES(1, 1, 1)

INSERT INTO user_permissions(user_id, permission_id, value)
VALUES(2, 1, 1)

EDIT: Query so far

SELECT a.user_id, b.permission_id, 1 as 'value'
FROM USER_PERMISSIONS a right outer join 
PERMISSIONS b on a.permission_id = b.permission_id
  • 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-07T14:39:04+00:00Added an answer on June 7, 2026 at 2:39 pm
    INSERT dbo.user_permissions([user_id], permission_id, [value])
    SELECT u.[user_id], p.permission_id, 1
    FROM dbo.user_permissions AS u 
    CROSS JOIN dbo.[permissions] AS p
    WHERE NOT EXISTS (SELECT 1 FROM dbo.user_permissions 
      WHERE [user_id] = u.[user_id]
      AND permission_id = p.permission_id
    )
    GROUP BY [user_id], p.permission_id;
    

    As an aside, you should avoid names that tend to require delimiters, e.g. user_id and permissions are keywords/reserved words.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple database that I inherited. For the purposes of this
I have a fairly simple MS Access Database that contains some metadata about a
So my database setup is fairly simple. I have a forum_cat table (a forum
I have a fairly simple web application that gets a list of items from
I'm writing an application for android and have a fairly simple SQLite3 database (see
I have a fairly simple call I'm trying to make to a database. I
I have a fairly simple web2py form with a few validators like, IS_FLOAT_IN_RANGE(...) on
I have a fairly simple question, however the answer seems to elude me. If
I have a fairly simple query: SELECT invoice_id WHERE employee_id = 'XXXX' AND customer_id
I have a fairly simple block of code. Sub Run(Name) on error resume next

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.