Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
i have data like this
VB
19  ,   'Global'
156111  ,   'Calvin'
153211  ,   'James A. Garfield '
152111  ,   'James Buchanan'
159211  ,   'Lyndon B. Johnson'
150411  ,   'Madison'
151211  ,   'William Henry Harrison'
154111  ,   'William McKinley'

when i insert into table variable its displaying in following format but i want display how i inserted in table variable
VB
156111  ,   'Calvin'
19  ,   'Global'
153211  ,   'James A. Garfield'
152111  ,   'James Buchanan'
159211  ,   'Lyndon B. Johnson'
150411  ,   'Madison'
151211  ,   'William Henry Harrison'
154111  ,   'William McKinley'
Posted
Updated 12-Nov-13 8:27am
v2
Comments
prakash.chakrala 12-Nov-13 8:24am    
bulk only

Try creating an identity column in your table variable see http://msdn.microsoft.com/en-us/library/ms186775.aspx[^].
This will automatically generate a unique id as each row is inserted, then when you select from the table var ORDER the results by that id
 
Share this answer
 
The SQL standard requires no guarantee about the order data will be returned in unless it is explicitly ordered using "order by". Some vendors' provide implementations that (most of the time) will return data in the order it was entered. However you cannot and should not rely on this.

You need to provide the table (or table variable) holding the data with a field that can be used to specify order.

The simplest way of achieving this In T-SQL is to provide an Identity column which will automatically increment every time a row is added. See the link in the solution posted by Chill60 above for more information.

SQL
SET NOCOUNT ON

DECLARE @Jim TABLE
(
  OrderBy int identity(0,1),
  KeyField int,
  ValueField varchar(50)
)

INSERT INTO @JIM (KeyField, ValueField) VALUES (19 , 'Global')
INSERT INTO @JIM (KeyField, ValueField) VALUES (156111 , 'Calvin')
INSERT INTO @JIM (KeyField, ValueField) VALUES (153211 , 'James A. Garfield ')
INSERT INTO @JIM (KeyField, ValueField) VALUES (152111 , 'James Buchanan')
INSERT INTO @JIM (KeyField, ValueField) VALUES (159211 , 'Lyndon B. Johnson')
INSERT INTO @JIM (KeyField, ValueField) VALUES (150411 , 'Madison')
INSERT INTO @JIM (KeyField, ValueField) VALUES (151211 , 'William Henry Harrison')
INSERT INTO @JIM (KeyField, ValueField) VALUES (154111 , 'William McKinley')

select * from @jim order by orderby asc


Will get you:
OrderBy     KeyField    ValueField
----------- ----------- --------------------------------------------------
0           19          Global
1           156111      Calvin
2           153211      James A. Garfield 
3           152111      James Buchanan
4           159211      Lyndon B. Johnson
5           150411      Madison
6           151211      William Henry Harrison
7           154111      William McKinley
 
Share this answer
 
v2
SQL
DECLARE @Jim TABLE
(
  KeyField int,
  ValueField varchar(50)
)

INSERT INTO @Jim
Select 19 ,   'Global'
UNION
Select 156111,   'Calvin'
UNION
Select 153211  ,   'James A. Garfield '
UNION
Select 152111  ,   'James Buchanan'
UNION
Select 159211  ,   'Lyndon B. Johnson'
UNION
Select 150411  ,   'Madison'
UNION
Select 151211  ,   'William Henry Harrison'
UNION
Select 154111  ,   'William McKinley'

Select * FROM @Jim
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900