473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

T-SQL how to quickly make the rows in sequence?

22
The example table looks like

Table T1
---------------
Name Income
---------------
James 44000
Frank 140000
Robert 60000
---------------

I wish to have another table, which looks like

Table T2
---------------
Name Income
---------------
James 44000
Robert 60000
Frank 140000
---------------

Of course I can use a query that has the wanted order.
--------------------
Expand|Select|Wrap|Line Numbers
  1. SELECT Name, Income FROM T1 ORDER BY Income
--------------------
However, I have to bcp out this query to disk and bcp it into Table T2. I think this might be low efficiency.

I can also use a cursor type of T-SQL (the T-SQL is just to show a concept and may not be working)
--------------------------------------
Expand|Select|Wrap|Line Numbers
  1. WHILE(SELECT COUNT(*) FROM T1 > 0) 
  2.   BEGIN
  3.     INSERT INTO T2 (NAME, INCOME)SELECT NAME, MIN(INCOME)FROM T1
  4.     DELETE FROM T1 WHERE INCOME = (SELECT MIN(INCOME) FROM T1)
  5.   END 
-------------------------------------
Is there a better way to do this?

Thanks
Nov 10 '12 #1
5 2058
zmbd
5,501 Expert Mod 4TB
It would be helpful to understand why you need a second table that duplicates the information in the first table.

I strongly advise against doing this under normal circumstances.

HOWEVER, IF you absolutly have to have this second table, then I suggest a make-table query.. http://msdn.microsoft.com/en-us/libr...v=sql.90).aspx
Nov 10 '12 #2
cuqsy0
22
@zmbd
Thanks for the hint. I have tried to use "select into order by" but the produced table still in the same order as the original table.

The following example illustrate this:


Expand|Select|Wrap|Line Numbers
  1. DROP TABLE #test1
  2. DROP TABLE #test2
  3. CREATE TABLE #test1(N INT)
  4.  
  5. INSERT INTO #test1(N) VALUES (22), (11)
  6. SELECT * FROM #test1
  7.  
  8. SELECT * INTO #test2 FROM #test1 ORDER BY N
  9.  
  10. SELECT * FROM #test2
Nov 11 '12 #3
Rabbit
12,516 Expert Mod 8TB
Put a clustered index on the table.

I also do not understand the need for this table.
Nov 11 '12 #4
cuqsy0
22
The need for this table is to compare two tables. For example, I have table A and B, like this:

Table A
----------------
Col1 Col2 Col3
3 7 8
1 5 6
1 2 5

Table B
----------------
Col7 Col8 Col9
2 7 8
5 1 6
5 2 1

I need to identify the rows with exactly the same numbers between two tables, although the numbers might be in different columns. In the above example, the rows with the same numbers of the two tables are :
1 5 6

and

1 2 5

Please tell me if there is a better way to do this?
Nov 13 '12 #5
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code.

I don't see the connection between what you want to do and your original post. The data structure is different, and there's no need for the numbers to be in order.
Nov 13 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
1
by: Steve | last post by:
Hi; I have a table with a TEXT datatype. Its a comment field. Right now the users who put in singlequotes are killing the web front end. The programmer responsible is fixing this issue but...
18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
2
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would...
1
by: Ernesto | last post by:
Using the "Pull New Subscription" wizrd in the enterprise manager, I can create a subscription without a problem. Howwever using TSQL I cannot do the same thing: Using this sequence:...
16
by: David Lozzi | last post by:
Hello, I have some code that adds a new user. The new user has a checkboxlist of items which they can be associated with. I would like to send this list of items to TSQL along with the new user...
10
by: Steve Atkins | last post by:
I have a large table (potentially tens or hundreds of millions of rows) and I need to extract some number of these rows, defined by an integer primary key. So, the obvious answer is select *...
4
by: roman.ali | last post by:
Hi, I am using a .dqy file to import some rows from a sql server 2003 database to microsoft excel. Unfortunately, I have am importing more rows than excel can handle. Is there a way around...
1
by: kbromer | last post by:
I'm in the process of trying to teach myself T-SQL out of a WROX book, and am having a problem wrapping my head around this example, I understand the concept of Primary Keys through using MS Access,...
1
by: jabeek | last post by:
I had to write a valid TSQL query that does all the following in a transaction and will rollback if any system error is thrown. • Create the following tables Customer, Order and Address where...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.