有一个存储客户名称的表,我需要复制这些记录并在同一时间添加一个字符。例如
我有:
CNAME
customer1
customer2
customer3
我想回复:
CNAME
customer1
customer1*
customer1#
customer2
customer2*
customer2#
customer3
customer3*
customer3#
有人可以帮帮我吗?
感谢
答案 0 :(得分:4)
<强> SQL2008 + 强>
DECLARE @Customer TABLE (CNAME NVARCHAR(50) NOT NULL);
INSERT @Customer (CNAME)
VALUES ('customer1'), ('customer2'), ('customer3');
SELECT c.CNAME + x.SuffixChar AS NewCNAME
FROM @Customer c
CROSS JOIN (VALUES (''), ('*'), ('#')) x(SuffixChar);
NewCNAME
----------
customer1
customer1*
customer1#
customer2
customer2*
customer2#
customer3
customer3*
customer3#
答案 1 :(得分:3)
您可以将结果合并在一起:
select
cname
from
table1
union all
select
cname + '*'
from
table1
union all
select
cname + '#'
from
table1
order by cname
您可以在this fiddle
中看到它正常工作答案 2 :(得分:2)
我会使用数字表和此查询:
with c as
(
select c.*, rn=row_number()over(order by cname)
from customers c
)
select cname = cname + case n % 3
when 1 then ''
when 2 then '*'
when 0 then '#' END
from numbers n join c
on n between 1 and 3
order by rn
结果:
customer1
customer1*
customer1#
customer2
customer2*
customer2#
customer3
customer3*
customer3#
如何创建数字表:http://sqlperformance.com/2013/01/t-sql-queries/generate-a-set-1
答案 3 :(得分:0)
连接字符串,例如:
select u.login || ' ' || u.password from user u
输出:用户1234
编辑:你的问题更复杂。我认为这只是为了连接字符串。