sql - 将 ASCII(子)集传输到表

标签 sql sql-server database relational-database ascii

我正在尝试将 ASCII 字符的子集传输到表中,但我一直收到错误消息,提示我在 SSMS 中复制值。

这是我的表格代码:

create table xyz(
  aChar char not null,
  primary key(aChar)
);

这是用来填充表格的:

declare @xChars int = 250
declare @iterations int = 0
while @iterations < @xChars
begin
insert into xyz values (char(@iterations))
set @iterations += 1
end

希望你们中的一个能帮我解决这个问题。

最佳答案

问题是不区分大小写的排序规则。 'a''A' 是同一件事。因此,使用区分大小写的排序规则:

create table xyz (
  aChar char collate SQL_Latin1_General_CP1_CS_AS not null,
  primary key(aChar)
);

你可以用一条语句来做到这一点:

with nums as (
      select 1 as n
      union all
      select n + 1
      from nums
      where n + 1 < 250
     )
insert into xyz (aChar)
    select char(nums.n)
    from nums
options (maxrecursion 0);

Here是一个 SQL fiddle 。

您还可以使用计算列来执行此操作:

create table xyz(
  aChar_num smallint not null,
  aChar as (char(aChar_num)),
  primary key(aChar_num)
);

with nums as (
      select 1 as n
      union all
      select n + 1
      from nums
      where n + 1 < 250
     )
insert into xyz (aChar_num)
    select nums.n
    from nums
    option (maxrecursion 0);

如图this SQL fiddle 。

关于sql - 将 ASCII(子)集传输到表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47739304/

相关文章:

sql-server - PDO_ODBC和unixODBC无法使用非默认端口连接MSSQL

具有数据库可执行文件的 JavaFx 应用程序

database - 从同时包含数字列表的文件中获取唯一数字

SQL + 具有不同行集的内部选择

SQL Server 查找包含的组

sql - SQL Server 2005 中的分层查询

sql - 递归地从同一个表父子表中获取最后一条记录

php - 如何通过 SSL 连接到 Amazon RDS?

mysql - SQL计数和求和问题

sql - Perl DBI 常量 - 如何访问?