Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure for Insert, Update & Delete

Author  Topic 

vignesht50
Yak Posting Veteran

82 Posts

Posted - 2013-11-26 : 11:58:40
Hi,

How could I possibly write a SP for Insert, Update and Delete for the below tables under this condition. On some conditions they need to be queried, initially for Insert, first the data should be inserted to PROFILES table and then to ROLES table. When inserting, if an entry is new then it should be added to both the tables. Sometimes when I make an entry which is already present in PROFILES table, then in this case the value should be added to the ROLES table. And a delete query to delete rows from both the table. Can anyone suggest me on this?

CREATE TABLE PROFILES(
USER_ID varchar(20) UNIQUE NOT NULL,
Name varchar(40) NULL,
Address varchar(25) NULL
)

insert into PROFILES values ('rryan','Jamie Fox','jfox@live.com')
insert into PROFILES values ('mclark','Michael Clark','mclark@live.com')
insert into PROFILES values ('djones','Dean Jones','djones@live.com')
insert into PROFILES values ('jfox','Jamie Fox','jfox@live.com')
insert into PROFILES values ('drivers','Doc Rivers','drivers@live.com')

CREATE TABLE ROLES(
USER_ID varchar(20) UNIQUE NOT NULL,
Role char(10) NOT NULL,
Applications char (10) NOT NULL
)

insert into ROLES values ('rryan','M','Consultant')
insert into ROLES values ('mclark','AM','Organizer')
insert into ROLES values ('djones','SM','Admin')
insert into ROLES values ('jfox','M','Consultant')
insert into ROLES values ('drivers','AM','Organizer')

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-11-26 : 12:52:35
You will have to first triage the action into 3 - insert/delete/update by testing whether the data that is given to you exists in the table(s) already, and what the requested action is.

As an example, if the request is to insert/update, first you would check whether the row exists in the profiles table. If so you would update it using the user_id as the key. If not, you would insert a new row. You would do a similar check for the Roles table as well. You have not specified whether one user_id can have more than one role. If that is the case, you would need information (i.e. input) on whether the role is to be updated or inserted.

Deleting would be simpler. If you have a foreign key constraint, delete first from the dependent table.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-27 : 04:32:43
quote:
Originally posted by vignesht50

Hi,

How could I possibly write a SP for Insert, Update and Delete for the below tables under this condition. On some conditions they need to be queried, initially for Insert, first the data should be inserted to PROFILES table and then to ROLES table. When inserting, if an entry is new then it should be added to both the tables. Sometimes when I make an entry which is already present in PROFILES table, then in this case the value should be added to the ROLES table. And a delete query to delete rows from both the table. Can anyone suggest me on this?

CREATE TABLE PROFILES(
USER_ID varchar(20) UNIQUE NOT NULL,
Name varchar(40) NULL,
Address varchar(25) NULL
)

insert into PROFILES values ('rryan','Jamie Fox','jfox@live.com')
insert into PROFILES values ('mclark','Michael Clark','mclark@live.com')
insert into PROFILES values ('djones','Dean Jones','djones@live.com')
insert into PROFILES values ('jfox','Jamie Fox','jfox@live.com')
insert into PROFILES values ('drivers','Doc Rivers','drivers@live.com')

CREATE TABLE ROLES(
USER_ID varchar(20) UNIQUE NOT NULL,
Role char(10) NOT NULL,
Applications char (10) NOT NULL
)

insert into ROLES values ('rryan','M','Consultant')
insert into ROLES values ('mclark','AM','Organizer')
insert into ROLES values ('djones','SM','Admin')
insert into ROLES values ('jfox','M','Consultant')
insert into ROLES values ('drivers','AM','Organizer')



best thing would be to create procedures for all these as you're calling this in asp.net
DELETE i've already given you the solution in other thread

insert/update would be like


CREATE PROC AddModifyData
@USER_ID varchar(20),
@Name varchar(40),
@Address varchar(25),
@Role char(10),
@Applications char (10)
AS
IF NOT EXISTS (SELECT 1 FROM PROFILES WHERE USER_ID = @USER_ID)
BEGIN
INSERT PROFILES
(
USER_ID ,
Name,
Address
)
VALUES
(
@USER_ID,
@Name,
@Address
)
END
ELSE
BEGIN
UPDATE PROFILES
SET Name=@Name,
Address = @Address
WHERE USER_ID = @USER_ID
AND (Name <> @Name,
OR Address <> @Address)
END

IF NOT EXISTS (SELECT 1 FROM ROLES WHERE USER_ID = @USER_ID)
BEGIN
INSERT ROLES
(
USER_ID ,
Role,
Applications
)
VALUES
(
@USER_ID,
@Role,
@Applications
)
END
ELSE
BEGIN
UPDATE ROLES
SET Role=@Role,
Applications = @Applications
WHERE USER_ID = @USER_ID
AND (Role <> @Role
OR Applications <> @Applications )

END
GO


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

vignesht50
Yak Posting Veteran

82 Posts

Posted - 2013-11-28 : 02:02:05
quote:
Originally posted by visakh16

quote:
Originally posted by vignesht50

Hi,

How could I possibly write a SP for Insert, Update and Delete for the below tables under this condition. On some conditions they need to be queried, initially for Insert, first the data should be inserted to PROFILES table and then to ROLES table. When inserting, if an entry is new then it should be added to both the tables. Sometimes when I make an entry which is already present in PROFILES table, then in this case the value should be added to the ROLES table. And a delete query to delete rows from both the table. Can anyone suggest me on this?

CREATE TABLE PROFILES(
USER_ID varchar(20) UNIQUE NOT NULL,
Name varchar(40) NULL,
Address varchar(25) NULL
)

insert into PROFILES values ('rryan','Jamie Fox','jfox@live.com')
insert into PROFILES values ('mclark','Michael Clark','mclark@live.com')
insert into PROFILES values ('djones','Dean Jones','djones@live.com')
insert into PROFILES values ('jfox','Jamie Fox','jfox@live.com')
insert into PROFILES values ('drivers','Doc Rivers','drivers@live.com')

CREATE TABLE ROLES(
USER_ID varchar(20) UNIQUE NOT NULL,
Role char(10) NOT NULL,
Applications char (10) NOT NULL
)

insert into ROLES values ('rryan','M','Consultant')
insert into ROLES values ('mclark','AM','Organizer')
insert into ROLES values ('djones','SM','Admin')
insert into ROLES values ('jfox','M','Consultant')
insert into ROLES values ('drivers','AM','Organizer')



best thing would be to create procedures for all these as you're calling this in asp.net
DELETE i've already given you the solution in other thread

insert/update would be like


CREATE PROC AddModifyData
@USER_ID varchar(20),
@Name varchar(40),
@Address varchar(25),
@Role char(10),
@Applications char (10)
AS
IF NOT EXISTS (SELECT 1 FROM PROFILES WHERE USER_ID = @USER_ID)
BEGIN
INSERT PROFILES
(
USER_ID ,
Name,
Address
)
VALUES
(
@USER_ID,
@Name,
@Address
)
END
ELSE
BEGIN
UPDATE PROFILES
SET Name=@Name,
Address = @Address
WHERE USER_ID = @USER_ID
AND (Name <> @Name,
OR Address <> @Address)
END

IF NOT EXISTS (SELECT 1 FROM ROLES WHERE USER_ID = @USER_ID)
BEGIN
INSERT ROLES
(
USER_ID ,
Role,
Applications
)
VALUES
(
@USER_ID,
@Role,
@Applications
)
END
ELSE
BEGIN
UPDATE ROLES
SET Role=@Role,
Applications = @Applications
WHERE USER_ID = @USER_ID
AND (Role <> @Role
OR Applications <> @Applications )

END
GO


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




Hi Visakh,
How can I merge this delete query to the SP?
declare @DELETED_IDs table
(
ID varchar(20)
)

DELETE t
OUTPUT DELETED.ID INTo @DELETED_IDs
FROM PROFILES t
WHERE ... your any conditions here

DELETE t
FROM ROLES t
INNER JOIN @DELETED_IDs d
ON d.ID = t.ID


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-28 : 06:04:31
same as how i gave you for insert/update. just enclose the delet statements within CREATE PROC. Do you mean you want someone to write the full query for you?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

vignesht50
Yak Posting Veteran

82 Posts

Posted - 2013-11-28 : 07:34:15
No, I just want to get clarified.
Go to Top of Page
   

- Advertisement -