我正在尝试将 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/