且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我想按名称顺序在SQL中的表中插入数据

更新时间:2023-01-29 09:22:18

如果我创建表并尝试使用代码:

If I create your tables and try your code:
USE [Testing]
GO

/****** Object:  Table [dbo].[FRTU_AVAILABILITY_CONFIG]    Script Date: 13/05/2019 11:32:20 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[FRTU_AVAILABILITY_CONFIG](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[SubstationName] [nvarchar](max) NOT NULL,
	[Other] [int] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO




USE [Testing]
GO

/****** Object:  Table [dbo].[RTU_AVAIL_DIALY]    Script Date: 13/05/2019 11:32:42 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[RTU_AVAIL_DIALY](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[RTUName] [nvarchar](max) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO




SELECT TOP 1000 [ID]
      ,[SubstationName]
      ,[Other]
  FROM [Testing].[dbo].[FRTU_AVAILABILITY_CONFIG]




ID	SubstationName	Other
1	DDD	6
2	EEE	5
3	AAA	9
4	BBB	8
5	CCC	7




SELECT TOP 1000 [ID]
      ,[RTUName]
  FROM [Testing].[dbo].[RTU_AVAIL_DIALY]




ID	RTUName
1	AAA
2	BBB
3	CCC
4	DDD
5	EEE

对我来说很好看。

查看输入和输出数据...

It looks fine to me.
Look at your input and output data ...


对在数据库表中维护排序顺序首先是一个问题:排序是一种仅在向最终用户提供数据时才有用的操作。数据库引擎知道如何有效地存储其数据,其标准可能与人类用户的标准不同。



你应该不再担心订单了其中要存储记录:)

亲切。
The desire to maintain a sorted order in a database table is an issue in the first place: sorting is an operation which is really helpful only when presenting data to the end user. The database engine knows how to store its data efficiently, and its criterions for that may not be the same as those of human users.

You should stop worrying about the order in which records are to be stored :)
Kindly.


选择< col1>,< col2>,< col3> dense_rank()over(由< col2>排序)为< tmp_table>
来自< old_table>的




插入< new_table>

select< col1>,< col2>,< col3>来自< tmp_table>



使用dense_rank订购列。
select <col1>,<col2>,<col3> dense_rank() over(order by <col2>) as d
into <tmp_table>
from <old_table>

insert into <new_table>
select <col1>,<col2>,<col3> from <tmp_table>

using dense_rank to order the column.