Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all..
i had two columns like
s.no name
1 adi
2 ajay
3 balu
4 bhanu
5 deepak
6 jhon
7 striker
in which i need to add one person 'anu' names are in alphabetical order 'anu' will be added in between ajey and balu 'anu' number will be 3 and remaining all update numbers will increment with 1 so i need to update >3 s.no values
after adding the series will be
1 adi
2 ajay
3 anu
4 balu
5 bhanu
6 deepak
7 jhon
8 striker
so here i need update query i did't tried bcoz i dont know
please can any one help me
thanks in advance
Posted
Updated 25-Apr-11 19:34pm
v2
Comments
Ankur\m/ 26-Apr-11 1:37am    
You can do it easily in the front-end. Get the values into a datatable ordered by name. Add the new value to datatable. Sort the table by name. Reassign SlNo in a for loop. Bulk update the table using DataAdapter update method.
tulasiram3975 26-Apr-11 2:06am    
Can You Give Me A sample

This might help you.
SQL
declare @intflag int
declare @intstring varchar(40)
declare @intstringb4 varchar(40)
set @intflag=2
set @intstring = (select [name] from table where sno=2)
While (@intflag <=(select max(sno) from table))
begin
print @intflag
set @intstringb4 = (select [name] from table where sno=@intflag+1)
update table set [name]=@intstring where sno=@intflag+1
set @intstring = @intstringb4
set @intflag = @intflag + 1
update table set [name]='anu' where id=@intflag
end
 
Share this answer
 
Comments
tulasiram3975 26-Apr-11 2:04am    
I need to do in code behind
m@dhu 26-Apr-11 3:06am    
Then just do as Ankur has said.
tulasiram3975 26-Apr-11 3:26am    
Am Feel Fear To Do can You Give Me A Sample Fro That
thank you
SQL
create procedure [dbo].[updatest]
as
begin

update st set sno=sno+1 where sno>=3
insert into st values(3,'anu')
select * from st order by sno
end
 
Share this answer
 
Comments
tulasiram3975 26-Apr-11 2:56am    
Thanks For Repley I dont Want To Use Stored Procedure
i Want To Do In Code Behind Only
You could try writing a stored procedure for this - a single query may not be sufficient to do all this processing.
 
Share this answer
 
Wow. What a requirement!

Anyways, I got a couple of questions for you. If you answer this I might be able to provide you a simpler approach :)

Questions
1. Does this table has any relationship to any other table by the ID?
2. Is the "S.No" an auto increment field? or do you set it manually?
2. Why do you need the Names and the "S.No"s to be sorted to match each other? Just out of curiosity, I want to know :)


Based on your answers :)

What you need to do is very simple. Let's say the new name is "Jennifer" and the s.no is "4" and the name of your table is "some_table"

1. Run this SQL to update the values
SQL
UPDATE some_table SET s.no = s.no + 1 WHERE s.no >= 4


This will result something similar to
s.no name
1 adi
2 ajay
3 balu
5 bhanu
6 deepak
7 jhon
8 striker

2. Run this SQL to insert your value
SQL
INSERT INTO some_table (s.no, name) VALUES (4, 'Jennifer');


This will result to
s.no name
1 adi
2 ajay
3 balu
4 Jennifer
5 bhanu
6 deepak
7 jhon
8 striker

Hope this helps you :) Regards
 
Share this answer
 
v2
Comments
tulasiram3975 26-Apr-11 5:33am    
1a:Yes But Not With sno its sid (sid is auto increment)
2a:No Not auto increment Set Manually
3a:Because names must be in alphabetical order so sno also in that order only
thank you in advance
CodeHawkz 26-Apr-11 6:02am    
Thank you for your answers, I update my solution based on your answers :)

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