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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql code

Author  Topic 

riya reddy
Starting Member

7 Posts

Posted - 2011-05-31 : 15:10:20
I needed the code to insert a row between two rows

the table has two columns with 10 rows
if the new data is to be inserted in row 5 the old data of row 5 should move to row 6 and data of row 6 to row 7 and so on

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-05-31 : 15:14:23
There really isn't a concept of between rows in SQL Server. Do you have a clustered index on this table? Please provide the DDL for your table (the create table statement) , as well as some sample data so we can see exactly what you're trying to accomplish.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

riya reddy
Starting Member

7 Posts

Posted - 2011-05-31 : 16:02:31
quote:
Originally posted by jimf

There really isn't a concept of between rows in SQL Server. Do you have a clustered index on this table? Please provide the DDL for your table (the create table statement) , as well as some sample data so we can see exactly what you're trying to accomplish.

Jim

Everyday I learn something that somebody else already knew



The table has two columns serial number and value
the table has 16 rows
new data is to be inserted in row 12 under the column name
when the value is inserted the original data of row 12 should move down to row 13 and the data of 13th row should move to 14th row.
finally the table has 17 rows and 17th row has the value of 16th row
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2011-05-31 : 19:36:53
Again, there is no concept in a table of order. You define the order of the set of data when you query the table.

How are you defining each row and assigning a value of 1 through nn?
Go to Top of Page

Ghanta
Yak Posting Veteran

96 Posts

Posted - 2011-06-01 : 03:08:10
I guess you can create another integer column and use that to order the data in table to another table. If you are using identiy column.. you can drop the identity and re-seed as per the need.. also provide what jim said and we should be able to help you more.
Go to Top of Page

riya reddy
Starting Member

7 Posts

Posted - 2011-06-01 : 10:28:26
quote:
Originally posted by jeffw8713

Again, there is no concept in a table of order. You define the order of the set of data when you query the table.

How are you defining each row and assigning a value of 1 through nn?



When I insert a row I get an error message "Primary key violation". How do I eliminate it? Please illustrate with example
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-06-01 : 10:33:18
You can't. That's what we're trying to tell you. And you still haven't provided us with the info we need to help you at all.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

riya reddy
Starting Member

7 Posts

Posted - 2011-06-01 : 11:21:03
quote:
Originally posted by jimf

You can't. That's what we're trying to tell you. And you still haven't provided us with the info we need to help you at all.

Jim

Everyday I learn something that somebody else already knew



Can I have a valid email address where I can send the table with create table statement
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-06-01 : 11:32:04
No. Post it here, change names of any columns that might compromise confidentiality/security.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-06-01 : 12:46:48
You can't insert "between" two primary keys. You'll have to either change the table structure or identify some way of knowing that the new value inserted is actually "between" two others.


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -