Question

In: Computer Science

SQL stored procedures Create 2 stored procedures: sp_AddNewUser and sp_AddNewRole. Use the ManyToMany script as your...

SQL stored procedures

Create 2 stored procedures: sp_AddNewUser and sp_AddNewRole. Use the ManyToMany script as your base table structures.

The stored procedure should accept the parameters needed to input the data for each table.

NOTE: You do not need to input the UserID or RoleID. These are surrogate keys and the system automatically inserts them when you insert a row in the tables.  

On execution, the stored procedure should check the database to see if the user exists, if so, return a message that says so. If the user does not exist, it should insert the information for a new user. Then do the same thing (in a different stored procedure) for the Role.

many to many script:

Create database ManyToMany
-- use master
-- drop database ManyToMany
go
use ManyToMany
go

-- Create tables to be used for the discussion
CREATE TABLE [dbo].[tblUsers](
   [Userid] [int] IDENTITY(100,1) NOT NULL PRIMARY KEY,
   [username] [varchar](32) NOT NULL,
   [password] [varchar](32) NOT NULL,
   [FName] [varchar](32) NULL,
   [LName] [varchar](32) NULL,
   [MName] [varchar](32) NULL,
   [eMail] [varchar](128) NOT NULL,
   [Phone] [varchar](16) NULL
)

go
CREATE TABLE [dbo].[tblRoles](
   [RoleID] [int] IDENTITY(100,1) NOT NULL PRIMARY KEY,
   [Role] [varchar](32) NOT NULL
)
go
CREATE TABLE [dbo].[tblUserRoles](
   [RoleID] [int] NOT NULL,
   [UserID] [int] NOT NULL
) ON [PRIMARY]

go

-- Add the foreign keys to the userroles table for data integrity

alter table tblUserRoles add constraint FK_Users Foreign Key (UserID) references tblUsers(UserID)
alter table tblUserRoles add constraint FK_Roles Foreign Key (RoleID) references tblRoles(RoleID)
go

-- insert sample data to the tblRoles Table

insert into tblRoles values ('Employee')
insert into tblRoles values ('Manager')
insert into tblRoles values ('Salesman')
insert into tblRoles values ('Security')

go

-- Insert sample data ti the tblUsers table
insert into tblUsers ([username],[password],[FName],[LName],[eMail],[Phone])
values ('derf', 'password1', 'Fred','Flintstone','[email protected]','555-1234')
insert into tblUsers ([username],[password],[FName],[LName],[eMail],[Phone])
values ('bam', 'bambam', 'Babbam','Rubble','[email protected]','555-5489')
insert into tblUsers ([username],[password],[FName],[LName],[eMail],[Phone])
values ('boss', 'bigboss', 'Sylvester','Slate','[email protected]','555-0258')
go

-- this is a script block used to get valid key values for the relationships
-- between the tables and the rows.

begin
declare @user int
declare @role int
set @user = (select userid from tblUsers where username = 'derf')
set @role = (select RoleID from tblRoles where Role = 'Employee')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @user = (select userid from tblUsers where username = 'bam')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @user = (select userid from tblUsers where username = 'boss')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @user = (select userid from tblUsers where username = 'derf')
set @role = (select RoleID from tblRoles where Role = 'Manager')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @user = (select userid from tblUsers where username = 'boss')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
set @role = (select RoleID from tblRoles where Role = 'Salesman')
insert into tblUserRoles (RoleID,UserID) values(@role,@user)
end
go

-- see the results

select * from tblUsers
select * from tblRoles
select * from tblUserRoles

Solutions

Expert Solution

Please see the below querry:

CREATE PROCEDURE sp_AddNewUser(
@username as varchar(32) ,
@password as varchar(32),
@FName as varchar(32), 
@LName as varchar(32), 
@MName as varchar(32), 
@eMail as varchar(32), 
@Phone as varchar(32)
 )
AS
BEGIN
   IF not EXISTS (SELECT 1 FROM tblUsers WHERE @username = USERNAME)
   BEGIN
                INSERT INTO tblUsers
                values( @username,@password,@FName,@LName,@MName,@eMail,@Phone    )
        END
        ELSE
        BEGIN
                RAISERROR('=============================', 0, 1);
                RAISERROR('USERNAME ALREDY EXISTS', 0, 1);
                RAISERROR('=============================', 0, 1);
        END
END
CREATE PROCEDURE sp_AddNewRole(
@Role as varchar(32)
 )
AS


BEGIN
   IF NOT EXISTS (SELECT 1 FROM tblRoles WHERE Role = @Role)
   BEGIN
                INSERT INTO tblRoles
        values( @Role   )
        END
        ELSE
        BEGIN
        RAISERROR('=============================', 0, 1);
        RAISERROR('USER ROLE ALREDY EXISTS', 0, 1);
        RAISERROR('=============================', 0, 1);
        END
END

Related Solutions

Step 2: Create Stored Procedures to Add/Update/Delete an entity table Create a script to create a...
Step 2: Create Stored Procedures to Add/Update/Delete an entity table Create a script to create a table named ProjectBilling which will have the following columns: • projectBillID char(6) : A 6 character unique identifier (numbers and letters) • TransAmount decimal(16,9) : The amount of the transaction • TransDesc varchar(255): A description of the transaction • TransDate datetime: The date of the transaction • projectID char(4):The Id of the project • accountMgr char(8):The employee who manages the bill ledger Include this...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3...
Create one sql script file to complete the following. You cannot run separate SQL statements for...
Create one sql script file to complete the following. You cannot run separate SQL statements for the homework. You will also need to place a semicolon after each SQL statement, a requirement for SQL files containing multiple SQL statements Lesson 3 Write a query to display the current date. Label the column DATE. Display the last name of all employees who have an A and an E in their last name. For each employee, display the employee number, last_name, salary,...
Write a SQL script to add another table to your database. Include these fields in your...
Write a SQL script to add another table to your database. Include these fields in your Product table: Field Name Description Data Type Sample Value ProductID Product ID integer 5 ProductName Product Name varchar(50) candle Description Product Description varchar(255) Bee’s wax candle picUrl Filename of the product’s picture varchar(50) candle.gif Price Product Price decimal 10.99           ProductID should be the Primary Key. It is an auto-increment field.           The Price field stores prices to 7 significant digits and to 2...
create procedures you can use as a bookkeeper as part of your duties related to payroll....
create procedures you can use as a bookkeeper as part of your duties related to payroll. include the following points: Enter employee pay records. Process payroll. Reconcile payroll. Update payroll records. Payroll enquiries. End of quarter. End of financial year. Back-up. Complete payroll component of BAS.
Use CYGWIN TERMINAL to create this script. COPY AND PASTE the screenshot. Script 1: 1.   Perform...
Use CYGWIN TERMINAL to create this script. COPY AND PASTE the screenshot. Script 1: 1.   Perform a ls -al on the files within the user's home directory and save it to a file called ls.dat within your ~ directory 2.   Save the tree information for the /home directory in a file called tree.dat within your ~directory 3.   Create a new directory in your home directory called "backups" 4.   Move the files you just created to the new directory 5.   Rename...
Consider these two Oracle Stored Procedures / Functions: Assume that both have been successfully compiled. CREATE...
Consider these two Oracle Stored Procedures / Functions: Assume that both have been successfully compiled. CREATE OR REPLACE FUNCTION CALC(pamt number) RETURN number AS vTot number; BEGIN     vTot := pamt + pamt ;      Return vTot; END; CREATE OR REPLACE PROCEDURE SHOW(pstr VARCHAR2) AS BEGIN     dbms_output.put_line(pstr); END; Write an anonymous block that executes the function named CALC and then the procedure called SHOW. The output generated by SHOW must be: The total is 20
Use a single SQL statement to create a relational table and to load into the table...
Use a single SQL statement to create a relational table and to load into the table department name, subject code, year of running and session of running that offered by the departments. Note that a running subject offered by a department means a lecturer of the department has been assigned to teach the subject. Next, enforce the appropriate consistency constraints on the new table.    When ready use SELECT statement to list the contents of the relational table created and...
1. How to create a group of users using a PowerShell script. 2. how to create...
1. How to create a group of users using a PowerShell script. 2. how to create a file consisting a group of 3 new user name’s and passwords, 1 to a line in power shell. 3. How to write a script in power shell to delete specific group of users. Prove that it works.
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as if it were being added to MySQL (please give explanations for each line of SQL code and a copy of the code as it would be entered into the query by itself: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT