Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one table which has 11 field.field1(id) is primary key identity(1,1).i want to save the value of id to another filed at same time.another filed name is fid int.i am newer.help me

What I have tried:

BEGIN
declare faid int;
insert into member(id,Name,Fname,Gender,Add1,Add2,District,Pincode,Mobile,Email,Occupation,DateofBirth) values(id,name,fname,gender,add1,add2,district,pincode,mobile,email,occupation,dateofbirth);
select faid=MAX(id) from member;
insert into member(fid)values (faid);

END
Posted
Updated 20-Dec-16 0:48am

1 solution

If you are storing same data to multiple columns, then your data will be inconsistent and you will need to normalize the database[^]. There are many solutions, like using Views to show the data to users and there you can use duplicate data. But inside the tables, the data must be atomic and non redundant.

If your tables are not like this, then even after solving this issue you will stumble upon other errors in future. Database anomalies[^] has a bit more information on what problems this type of structure lead you to.

Your table will also have a lot of NULL values, in first INSERT INTO fid will be null, then in second one, all columns other than fid will be null. What are you doing?
 
Share this answer
 
Comments
ramesh arunchalam 20-Dec-16 9:01am    
Thank You For You answer.Is there any option same data to multiple column?.if have,send me syntax please.
Afzaal Ahmad Zeeshan 20-Dec-16 9:20am    
You should not have it in multiple columns. That is the thing.

You can capture the ID later and generate virtual views for it.
ramesh arunchalam 20-Dec-16 9:18am    
Normal my Insert Query like this.BEGIN
insert into member(id,Name,Fname,Gender,Add1,Add2,District,Pincode,Mobile,Email,Occupation,DateofBirth) values(id,name,fname,gender,add1,add2,district,pincode,mobile,email,occupation,dateofbirth);
END
"I want add one field Fid and id field value store to fid filed at same time"
is Possible.?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900