Last post Mar 12, 2012 11:00 AM by Lannie
Mar 10, 2012 05:53 AM|tnitin55|LINK
I have created a table with attributes like s_no , user_name , Phone_number , user_ID ........ and insertion divided into many parts so first query i used is insert and then update where user_ID matches.
and i use query to insert like (insert into table (user_name , Phone_number , user_ID where user_ID =textbox1.text)
and for the rest of the attributes update where user_ID = @user_ID
something like that but the problem is if user again use the same user_ID to edit something in database there is again a new row created with the same user_ID and for this scenario user_ID must be unique and if any user again use the same user_ID then the new
data will be update in the same user_ID instead of new row created. I know there must be query like
if this then
but the thing is i do'nt have any idea about it to use insert and update at the same place.
i want to use procedure for solving this problem.
Any help will be appropriated.
Mar 10, 2012 06:01 AM|sharanamma|LINK
Please add primary key constraint to user_ID coloumn and
write store procedure like,
create proc InsertOrUpdate
update tablename set (parameters to columns) where user_ID=@user_ID
insert into tablename
Hope this will helps to you,
Mar 10, 2012 05:29 PM|Lannie|LINK
Use MERGE Clause
If the user_id already exists, it updates your columns of choice
If it is a new row based on userid, it inserts new row
Mar 12, 2012 07:56 AM|tnitin55|LINK
Thanks for the reply but can you give any example or any link to use merge clause.
Mar 12, 2012 11:00 AM|Lannie|LINK