Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to return records in squence of inner join table? Expand / Collapse
Author
Message
Posted Friday, September 10, 2010 5:53 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 14, 2010 6:26 AM
Points: 3, 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
Post #983676
Posted Friday, September 10, 2010 6:15 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 9:22 AM
Points: 40,632, Visits: 37,094
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 2008, MVP
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

Post #983690
Posted Friday, September 10, 2010 6:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
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
Post #983692
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse