【发布时间】:2020-06-16 18:15:10
【问题描述】:
我有几个插入查询:
insert into table2 (age, name)
select '0-19', 'abc'
where not exists (select 1 from table2 where age is null and name is null);
insert into table2 (age, name)
select '20-29', 'zxy'
where not exists (select 1 from table2 where age not in ( '0-19') and name is null);
insert into table2 (age, name)
select '30-39', 'egt'
where not exists (select 1 from table2 where age not in ( '0-19', '20-29') and name is null);
insert into table2 (age, name)
select '40-49', 'aaa'
where not exists (select 1 from table2 where age not in ( '0-19', '20-29', '30-39') and name is null);
insert into table2 (age, name)
select '50-59', 'rtg'
where not exists (select 1 from table2 where age not in ( '0-19', '20-29', '30-39', '40-49') and name is null);
insert into table2 (age, name)
select '60+', 'ghg'
where not exists (select 1 from table2 where age not in ( '0-19', '20-29', '30-39', '40-49', '50-59') and name is null);
如果满足相关条件,我想插入数据。你可以看到他们分开查询。将这些查询重写为一个查询。谢谢
无论nvarchar 或integer,我都可以尝试与任何可以修改这些查询的人进行测试。
【问题讨论】:
-
请解释您要做什么。这些查询对我来说真的没有意义。
-
如果满足条件我想插入数据。你可以看到他们单独的查询。将这些查询重写为一个查询。
-
抱歉,存储整数值(
age列的整数值)不是一个好主意。 -
所有 nvarchar 不是整数
-
"all nvarchar not integer" 更糟糕的是,@qphan。 9 岁的人不 比
75的人大,但是,'9'大于'75'(或者在你的情况下是N'9'和N'75'分别)。然后我们会发现,在存储非 ANSI 字符时存储nvarchar也是一种快速存储。
标签: sql sql-server