SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to return records in squence of inner join table?


how to return records in squence of inner join table?

Author
Message
jdp12383
jdp12383
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 34
Hi,

I have test database with following script. I am trying to explain my problem with this sample db script. I am creating a temp. table with the ordered column from other table and then using that table to join the other table. If you notice the output of the below select query, the returned rows from first table are in the sequence of insertion not in the sequence of the temp. table.

Is there any other way to retrieve rows in the sequence of temp. (joined) table?

CREATE TABLE [dbo].[Table_2](
[c1] [int] NULL,
[c2] [nvarchar](50) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Table_2] ([c1], [c2]) VALUES (1, N'z')
INSERT [dbo].[Table_2] ([c1], [c2]) VALUES (2, N'y')
INSERT [dbo].[Table_2] ([c1], [c2]) VALUES (3, N'x')
INSERT [dbo].[Table_2] ([c1], [c2]) VALUES (4, N'a')
INSERT [dbo].[Table_2] ([c1], [c2]) VALUES (5, N'b')
INSERT [dbo].[Table_2] ([c1], [c2]) VALUES (6, N'c')

CREATE TABLE [dbo].[Table_1](
[c1] [int] NULL,
[c2] [nvarchar](50) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Table_1] ([c1], [c2]) VALUES (3, N'x')
INSERT [dbo].[Table_1] ([c1], [c2]) VALUES (2, N'y')
INSERT [dbo].[Table_1] ([c1], [c2]) VALUES (1, N'z')
INSERT [dbo].[Table_1] ([c1], [c2]) VALUES (6, N'c')
INSERT [dbo].[Table_1] ([c1], [c2]) VALUES (5, N'b')
INSERT [dbo].[Table_1] ([c1], [c2]) VALUES (4, N'a')

----------------------------------------------------

IF OBJECT_ID('TempDB..#tblTemp','U') IS NOT NULL
DROP TABLE #tblTemp

CREATE TABLE #tblTemp (Id int);

INSERT INTO #tblTemp SELECT c1 FROM Table_2 ORDER BY c2

SELECT *
FROM Table_1
INNER JOIN #tblTemp ON Id=Table_1.c1

DROP TABLE #tblTemp



Regards,
Jigs
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88919 Visits: 45284
If you want to retrieve rows in a particular order, you must put an ORDER BY on the statement that retrieves the data with the appropriate columns listed

An order by on the insert only affects identity columns (if present). It does not guarantee the order that data will be returned from that table

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16540 Visits: 19557
There is no guarantee that rows will be returned in any particular unless ORDER BY is specified.
There is also no guarantee that rows will be inserted in a particular order, even if the inserts are sourced as SELECT...ORDER BY...

http://sqlblogcasts.com/blogs/sqlandthelike/archive/2010/06/27/sql-101-without-order-by-order-is-not-guaranteed.aspx

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search