473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Insert a record in a particular place

i have a table name is HH table
it has two columns 'hhno' and hhname'
HH tabele
hhno hhname
100 suresh
101 baba
103 ram
i want to insert a one record(102 , chandra) in HH table between
(101,baba) and( 103 ,ram).
how can i insert them please help ,me thanks

May 27 '06 #1
10 3302
surya (su*******@gmail.com) writes:
i have a table name is HH table
it has two columns 'hhno' and hhname'
HH tabele
hhno hhname
100 suresh
101 baba
103 ram
i want to insert a one record(102 , chandra) in HH table between
(101,baba) and( 103 ,ram).
how can i insert them please help ,me thanks


Just as you would insert any other row:

INSERT HH(hhno, hhnmae)
VALUES (102, chandra)

Then again, one can say that you can't because "between" does not make
any sense in this context. Tables are unordered sets of rows.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 27 '06 #2
surya wrote:
i have a table name is HH table
it has two columns 'hhno' and hhname'
HH tabele
hhno hhname
100 suresh
101 baba
103 ram
i want to insert a one record(102 , chandra) in HH table between
(101,baba) and( 103 ,ram).
how can i insert them please help ,me thanks


Like this:

INSERT INTO hhtable (hhno, hhname)
(102 , 'chandra');

SELECT hhno, hhname
FROM hhtable
ORDER BY hhno ;

A table doesn't have any order. Only the results of a SELECT statement
can be sorted.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

May 27 '06 #3

"David Portas" <RE****************************@acm.org> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
surya wrote:
i have a table name is HH table
it has two columns 'hhno' and hhname'
HH tabele
hhno hhname
100 suresh
101 baba
103 ram
i want to insert a one record(102 , chandra) in HH table between
(101,baba) and( 103 ,ram).
how can i insert them please help ,me thanks


Like this:

INSERT INTO hhtable (hhno, hhname)
(102 , 'chandra');

SELECT hhno, hhname
FROM hhtable
ORDER BY hhno ;

A table doesn't have any order. Only the results of a SELECT statement
can be sorted.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--


I think you can have an ordered table in SQL Server if you are using a
clustering index.

Regards
Ralph
May 27 '06 #4
Ralph Ganszky wrote:

I think you can have an ordered table in SQL Server if you are using a
clustering index.

Regards
Ralph


You cannot. That is to say, a table with a clustered index is still not
logically ordered.

The OP asked how to insert a row between other rows. Although a
clustered index might achieve something like that at the page level a
clustered index doesn't really answer the original question - there is
no way to INSERT rows between each other. The only reliable way to get
ordered results from a table is to use ORDER BY when you query the
table.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

May 27 '06 #5
"Ralph Ganszky" <rg@web.de> wrote in news:e5*************@news.t-
online.com:

I think you can have an ordered table in SQL Server if you are using a
clustering index.


You think wrong. If you don't use an ORDER BY clause,
the returned order is undefined.

Sure, if you have a clustered index, AND you're only
selecting from one table, AND you're not using a where
clause that causes the optimizer to choose differently,
AND the phase of the moon is just right, THEN you **MIGHT**
get an ordered set.

In fact, that's pretty much guaranteed to work, right up
until you're demo'ing your project for the clients.

May 27 '06 #6

"David Portas" <RE****************************@acm.org> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
Ralph Ganszky wrote:

I think you can have an ordered table in SQL Server if you are using a
clustering index.

Regards
Ralph


You cannot. That is to say, a table with a clustered index is still not
logically ordered.

The OP asked how to insert a row between other rows. Although a
clustered index might achieve something like that at the page level a
clustered index doesn't really answer the original question - there is
no way to INSERT rows between each other. The only reliable way to get
ordered results from a table is to use ORDER BY when you query the
table.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--


How do you know that the OP asked exactly what you expect he have asked? Are
you sure that he asked what you think/want he has asked? What I meant is
exactly what he asked from my point of view. He asked if it is possible to
insert in between two other records. A clustered index does exactly this on
row level. It has on the other side nothing to do with the order in the
result set as you mentioned. But the answer is true any way for the physical
representation on the disk. I aggree that in relational logic there is no
order in a set, but SQL implementation of the relational model are allmost
allways no strict implementations and have some features which are against
the relational model from Cod. Especially MS SQL Server have many features
which are not in the sense of Cod, e.g. the FROM clause in the UPDATE
statement.

I think the OP can now choose from the replies which answer answers his
question.

Regards Ralph
May 28 '06 #7
Ralph Ganszky (rg@web.de) writes:
How do you know that the OP asked exactly what you expect he have asked?
Are you sure that he asked what you think/want he has asked?


Taken to the letter, suryv'a question did not make sense.

My guess that is that what he was really asking about was the Open Table
feature, where he wanted to right-click and select Insert as in Excel,
and a row in the middle.

But that's the way the story goes. If you are short on details in your
questions, the answers may not apply to your real problem.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 28 '06 #8
Ralph Ganszky wrote:

How do you know that the OP asked exactly what you expect he have asked? Are
you sure that he asked what you think/want he has asked? What I meant is
exactly what he asked from my point of view. He asked if it is possible to
insert in between two other records. A clustered index does exactly this on
row level. It has on the other side nothing to do with the order in the
result set as you mentioned. But the answer is true any way for the physical
representation on the disk.


Only partly right. A clustered index will give rows a positional order
that can be reconstructed internally in the clustered index. However,
that order is normally inaccessible to the user. Your statement that
this is a physical order on disk is misleading. There need not be any
such physical ordering and even if it exists initially there is no
guarantee it will be maintained by a clustered index.

To refer to this in the way you did (an "ordered table") without any
qualification or explanation is to invite confusion. A table is a
logical model concept and definitively does not have a logical ordering
- not even given the faults and features of SQL and T-SQL. It's
difficult to see how a clustered index answers the original question
(if the OP wasn't aware of clustered indexes then how could he be
asking a question about their internal ordering?) but even if it did, I
wanted to clear up any confusion about the phrase "ordered table".

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

May 28 '06 #9
On 28 May 2006 03:12:03 -0700, David Portas wrote:
Ralph Ganszky wrote:

How do you know that the OP asked exactly what you expect he have asked? Are
you sure that he asked what you think/want he has asked? What I meant is
exactly what he asked from my point of view. He asked if it is possible to
insert in between two other records. A clustered index does exactly this on
row level. It has on the other side nothing to do with the order in the
result set as you mentioned. But the answer is true any way for the physical
representation on the disk.


Only partly right. A clustered index will give rows a positional order
that can be reconstructed internally in the clustered index. However,
that order is normally inaccessible to the user. Your statement that
this is a physical order on disk is misleading. There need not be any
such physical ordering and even if it exists initially there is no
guarantee it will be maintained by a clustered index.


Hi David,

Exactly. I checked this using the following script:

USE TestDB90
-- Create table
CREATE TABLE HH
(hhno int NOT NULL PRIMARY KEY CLUSTERED,
hhname varchar(40) NOT NULL)
go
-- Insert initial population
INSERT INTO HH (hhno, hhname)
SELECT 100, 'suresh'
UNION ALL
SELECT 101, 'baba'
UNION ALL
SELECT 103, 'ram'
go
-- Attempt to add row "in between" other rows
INSERT INTO HH (hhno, hhname)
SELECT 102, 'chandra'
go
-- Get DBCC PAGE output to query window instead of system log
DBCC TRACEON(3604)
go
-- Find page in DB file where table data is stored
SELECT first FROM sysindexes WHERE id = OBJECT_ID('HH')
go
-- Check page (calculate page number from previous query output
DBCC PAGE(TestDB90, 1, 208, 2)
go

And here are some snippets from the output:

609EC060: 30000800 64000000 0200fc01 00150073 †0...d..........s
609EC070: 75726573 68300008 00650000 000200fc †uresh0...e......
609EC080: 01001300 62616261 30000800 67000000 †....baba0...g...
609EC090: 0200fc01 00120072 616d3000 08006600 †.......ram0...f.
609EC0A0: 00000200 fc010016 00636861 6e647261 †.........chandra
609EC0B0: 00000000 00000000 00000000 00000000 †................
(...)
609EDFF0: 01000000 381d0000 88009a00 75006000 †....8.......u.`.

OFFSET TABLE:

Row - Offset
3 (0x3) - 136 (0x88)
2 (0x2) - 154 (0x9a)
1 (0x1) - 117 (0x75)
0 (0x0) - 96 (0x60)

As you can see, the physical order matches (this case, coincidentally)
the order of insertion. The "logical" ordering according to the
clustered index is enforced in the offset table (which is encoded in the
last few bytes of the pages - you'll recognise the binary values of the
offsets in the last line of the dump).

--
Hugo Kornelis, SQL Server MVP
May 28 '06 #10
SQL Server does not have a functionality to insert a record in a particular
place in a table.

If you are talking about a clustered index then
1. You are not talking about the functionality 'table' but about an
implementation detail.
2. The function of a clustered index is to speed up queries, not to insert
in a specific position.

It makes no sense to insert a record in a particular place. It makes sense
to:
1. get records in a order by using order by
2. speed up querys by using a clustered index.

It is wrong to expect sorted records in the result set of a query without
using a order by, even if there is a clustered index. Functional spoken, the
concept table (also in SQL Server) has no order.

Peter

"Ralph Ganszky" <rg@web.de> wrote in message
news:e5*************@news.t-online.com...

"David Portas" <RE****************************@acm.org> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
surya wrote:
i have a table name is HH table
it has two columns 'hhno' and hhname'
HH tabele
hhno hhname
100 suresh
101 baba
103 ram
i want to insert a one record(102 , chandra) in HH table between
(101,baba) and( 103 ,ram).
how can i insert them please help ,me thanks


Like this:

INSERT INTO hhtable (hhno, hhname)
(102 , 'chandra');

SELECT hhno, hhname
FROM hhtable
ORDER BY hhno ;

A table doesn't have any order. Only the results of a SELECT statement
can be sorted.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--


I think you can have an ordered table in SQL Server if you are using a
clustering index.

Regards
Ralph

Jun 5 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
1
by: Thomas Bartkus | last post by:
The meaning of REPLACE INTO is clear to me. IF the new record presents new key values, then it is inserted as a new record. IF the new record has key values that match a pre-existing record, then...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
4
by: Tim Slattery | last post by:
It would be convenient for my app to store the stuff I'm generating in a std::list. I'd like to remember the location of a particular place in the list - sort of like sticking my finger into it -...
4
by: Simon Gare | last post by:
Hi all, need to retrieve a record from a table (tblBookingForm) in one database and insert it into a table (tblNetServ) in another database on the same server, leaving the original record in...
5
by: djsdaddy | last post by:
Good Day All, I have some EEO data in an old dBase4 database that I have converted to an Access table. Since dBase was not a relational database, I didn't create any key fields. I linked all of the...
3
by: lionelm2007 | last post by:
Hi there, I have a form called Production Run and has a field to select a product. I would like to grab the recipe for a particular product from product recipe table, do some calculations on the...
5
by: =?Utf-8?B?bXBhaW5l?= | last post by:
Hello, I am completely lost as to why I can't update a DropDownList inside a DetailsView after I perform an insert into an object datasource. I tried to simply it down to the core demostration:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.