简体   繁体   中英

SQL server 2008 - rows are mixed when inserting into another table

I have one store procedure and inside that one i used 4 tables(tb1,tb2,tb3,rs1).each table has only one column(x-varchar type).starting of the procedure I deleted the existing rows in the contents first 3 tables except rs1 then I ll insert some 10 to 15 rows in three tables.. End of procedure I ll insert the 3 tables data into rs1 using 3 insert statements(first rs1,rs2 and rs3). But the issue is when I select the rows from rs1,few ts3 rows are in first two rows of rs1 then it has rs1 data then ts2 value then missing ts3 rows..also this is not happened all the times... I'm not able to find out the reason for this...

Tb1 value
----------
ARUN
Address
State

Tb2 value
----------
Product1
Product2
Product3

Tb3 value
---------
Productname1
Productname2
Productname3

After I'm inserting the data to rs1 like this

Insert into rs1
Select x from ts1

Insert into rs1
Select x from ts2

Insert into rs1
Select x from ts3

Result is coming like below

Productname2
Productname3
Name
Address
State
Product1
Product2
Product3
Productname1

Database tables are not ordered by nature.
You are getting the results in a random since there is nothing to order them by. If you add a column to rs1 that you can order by it, such as a datetime column with default value of GETDATE() , or an identity column, you can use it to get the data in the order you've inserted it to the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM