I have the following parameters and requirements and I have to write sql script, having issues doing it.
I need assistance as I am new to t-sql
We need to create a generic sql script that will accept the following parameters:
- @v_Username
- @v_Encryptedpassword
- @v_Firstname
- @v_Lastname
- @v_Branch
- @i_Rolekey
Step 1: Add the users row
Check the users table to see if the username already exists
-if yes then get the users.id and store it in @i_userid
-if no, then
-set @i_userid = max users.id and add +1 to it to get the new users.id
-insert the following
INSERTINTO users(id,username,password,first_name,last_name,active,deleted,created_by,dev_access)
values(@i_userid, @v_Username, @v_Encryptedpassword,@v_firstname,@v_lastname,1,0,1,0)
Step 2: Set the branch information
-Set the @i_branchid = bu_entries.id where bu_entries description = @v_Branch
-Set the @v_branchcode = bu_entries.code where bu_entries description = @v_Branch
Check the users_entries table to see if a row already exists for users_entries.user_id = @i_userid andusers_entries.entry_id = @i_branchid
IF no then Insert the following
INSERTINTO users_entries (user_id,entry_id,role_id,sort_order,created_Date)
values (@i_userid,@i_branchid,@i_rolekey,1,curdate())
Now do the dar_branch table
- Set the @i_maxdarbranchkey = max dar_branch.id +1
Check to see if the row already exists for dar_branch.userid = @i_userid and dar_branch.branchid = @v_branchcode
If no then insert the row
insertinto dar_branch (id,userid,branchid)
values (@i_maxdarbranchkey, @i_userid, @v_branchcode,3)
Step 3: Insert the acl_priviliges rows to allow access to the dar pages by cloning from an existing user. We will need to do a loop to increment the acl_priviliges.id for each insert.
Check first to see if the user exists where acl_privliges.user_id = @i_userid
If no, then we need to insert all the rows
Insert into acl_priviliges (id, resource_id,role_id,user_id,template_id, rule)
Select @i_maxid,resource_id,role_id,@i_userid,template_id,rule from acl_privilges where user_id= 32052
I have done the following but have alot of syntax errors
Declare @i_userid int,
@v_Username varchar(50),
@v_Encryptedpassword char(20),
@v_Firstname varchar(50),
@v_Lastname varchar(50),
@v_Branch int,
@i_Rolekey int
select * from users
where username=@v_Username and
first_name=@v_Firstname and
last_name=@v_Lastname
begin
if exists (select @v_Username from users)
set @i_userid=id
else
set @i_userid = max id+1
end
begin
insert into users
(id,
username,
password,
first_name,
ast_name,
active,
deleted,
created_by,
dev_access)
values
(
@i_userid,
@v_Username,
@v_Encryptedpassword,
@v_firstname,
@v_lastname,
1,
0,
1,
0)
end
-------step 2-------------------------------------------------------
begin
declare
@i_branchid int,
@v_branchcode char(16)
select * from bu_entries
--select @i_branchid=bu_entries.id,@v_branchcode=code from bu_entries
begin
Set @i_branchid = bu_entries.id where bu_entries.description = @v_Branch
Set @v_branchcode = bu_entries.code where bu_entries.description = @v_Branch
end
begin
if exists ( select * from bu_entries where users_entries.user_id = @i_userid and users_entries.entry_id = @i_branchid)
else
begin
INSERT INTO users_entries
(user_id,
entry_id,
role_id,
sort_order,
created_Date)
values
(
@i_userid,
@i_branchid,
@i_rolekey,
1,
getdate())
end
begin
declare
@i_maxdarbranchkey int
set @i_maxdarbranchkey = max dar_branch.id +1
begin
if exists
(select * from dar_branch WHERE dar_branch.id = @i_userid and dar_branch.branchid = @v_branchcode)
else
BEGIN
insert into dar_branch
(id,
userid,
branchid)
values
(
@i_maxdarbranchkey,
@i_userid,
@v_branchcode,
3)
end
-------------step 3-------------------------------------------
declare
@i_maxid int
begin
if exists (select acl_priviliges.user_id=@user_id)
else
Insert into acl_priviliges
(id,
resource_id,
role_id,
user_id,
template_id,
rule)
Select
@i_maxid,
resource_id,
@i_rolekey,
@i_userid,
template_id,
rule
from
acl_privileges
where user_id= 32052
end