Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:30 PM
surya
 
Posts: n/a
Default 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:30 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Insert a record in a particular place

surya (suryaitha@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, esquel@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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:30 PM
David Portas
 
Posts: n/a
Default Re: Insert a record in a particular place

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
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:30 PM
Ralph Ganszky
 
Posts: n/a
Default Re: Insert a record in a particular place


"David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message
news:1148720603.539232.327650@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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 07:30 PM
David Portas
 
Posts: n/a
Default Re: Insert a record in a particular place

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
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 07:30 PM
Eric J. Holtman
 
Posts: n/a
Default Re: Insert a record in a particular place

"Ralph Ganszky" <rg@web.de> wrote in news:e59adc$kug$01$1@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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 07:30 PM
Ralph Ganszky
 
Posts: n/a
Default Re: Insert a record in a particular place


"David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message
news:1148732229.972980.233770@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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 07:30 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Insert a record in a particular place

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, esquel@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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 07:30 PM
David Portas
 
Posts: n/a
Default Re: Insert a record in a particular place

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
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-29-2008, 07:31 PM
Hugo Kornelis
 
Posts: n/a
Default Re: Insert a record in a particular place

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:33 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589