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.
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. |
|
|
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.netDELETE i've already given you the solution in other threadinsert/update would be likeCREATE PROC AddModifyData@USER_ID varchar(20),@Name varchar(40),@Address varchar(25),@Role char(10),@Applications char (10)ASIF NOT EXISTS (SELECT 1 FROM PROFILES WHERE USER_ID = @USER_ID)BEGININSERT PROFILES(USER_ID ,Name,Address )VALUES(@USER_ID,@Name,@Address )ENDELSEBEGINUPDATE PROFILESSET Name=@Name,Address = @AddressWHERE USER_ID = @USER_IDAND (Name <> @Name,OR Address <> @Address)ENDIF NOT EXISTS (SELECT 1 FROM ROLES WHERE USER_ID = @USER_ID)BEGININSERT ROLES(USER_ID ,Role,Applications )VALUES(@USER_ID,@Role,@Applications )ENDELSEBEGIN UPDATE ROLESSET Role=@Role,Applications = @Applications WHERE USER_ID = @USER_IDAND (Role <> @RoleOR Applications <> @Applications )ENDGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
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.netDELETE i've already given you the solution in other threadinsert/update would be likeCREATE PROC AddModifyData@USER_ID varchar(20),@Name varchar(40),@Address varchar(25),@Role char(10),@Applications char (10)ASIF NOT EXISTS (SELECT 1 FROM PROFILES WHERE USER_ID = @USER_ID)BEGININSERT PROFILES(USER_ID ,Name,Address )VALUES(@USER_ID,@Name,@Address )ENDELSEBEGINUPDATE PROFILESSET Name=@Name,Address = @AddressWHERE USER_ID = @USER_IDAND (Name <> @Name,OR Address <> @Address)ENDIF NOT EXISTS (SELECT 1 FROM ROLES WHERE USER_ID = @USER_ID)BEGININSERT ROLES(USER_ID ,Role,Applications )VALUES(@USER_ID,@Role,@Applications )ENDELSEBEGIN UPDATE ROLESSET Role=@Role,Applications = @Applications WHERE USER_ID = @USER_IDAND (Role <> @RoleOR Applications <> @Applications )ENDGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://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 tOUTPUT DELETED.ID INTo @DELETED_IDsFROM PROFILES tWHERE ... your any conditions hereDELETE tFROM ROLES tINNER JOIN @DELETED_IDs dON d.ID = t.ID |
|
|
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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
vignesht50
Yak Posting Veteran
82 Posts |
Posted - 2013-11-28 : 07:34:15
|
No, I just want to get clarified. |
|
|
|
|
|
|
|