SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure for Insert, Update & Delete
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

vignesht50
Yak Posting Veteran

77 Posts

Posted - 11/26/2013 :  11:58:40  Show Profile  Reply with Quote
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')

Edited by - vignesht50 on 11/26/2013 12:10:10

James K
Flowing Fount of Yak Knowledge

3593 Posts

Posted - 11/26/2013 :  12:52:35  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 11/27/2013 :  04:32:43  Show Profile  Reply with Quote
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

Edited by - visakh16 on 11/27/2013 04:33:31
Go to Top of Page

vignesht50
Yak Posting Veteran

77 Posts

Posted - 11/28/2013 :  02:02:05  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 11/28/2013 :  06:04:31  Show Profile  Reply with Quote
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

77 Posts

Posted - 11/28/2013 :  07:34:15  Show Profile  Reply with Quote
No, I just want to get clarified.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000