Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server存储过程插入重复行_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

SQL Server存储过程插入重复行

SQL Server存储过程插入重复行,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一个列为GetDup的表,我希望根据该列的值复制记录。例如,如果GetDup中的值on为1,则复制记录一次。如果列中的值为2,则复制记录两次,依此类推,语句必须位于循环语句中 为此编写存储过程的好方法是什么?请帮忙 输入: +--------+--------------+---------------+ | Getdup | CustomerName | CustomerAdd | +--------+--------------+---------------+ | 1 |

我有一个列为GetDup的表,我希望根据该列的值复制记录。例如,如果GetDup中的值on为1,则复制记录一次。如果列中的值为2,则复制记录两次,依此类推,语句必须位于循环语句中

为此编写存储过程的好方法是什么?请帮忙

输入:

+--------+--------------+---------------+
| Getdup | CustomerName | CustomerAdd   |
+--------+--------------+---------------+
|      1 | John         | 123 SomeWhere |
|      2 | Bob          | 987 SomeWhere |
+--------+--------------+---------------+
我想要的是:

+--------+--------------+---------------+
| Getdup | CustomerName | CustomerAdd   |
+--------+--------------+---------------+
|      1 | John         | 123 SomeWhere |
|      1 | John         | 123 SomeWhere |
|      2 | Bob          | 987 SomeWhere |
|      2 | Bob          | 987 SomeWhere |
|      2 | Bob          | 987 SomeWhere |
+--------+--------------+---------------+

在标准SQL中,可以使用递归CTE:

with recursive cte as (
      select t.dup, . . .
      from t
      union all
      select cte.dup - 1, . . .
      from cte
      where cte.dup > 1
     )
select *
from cte;
当然,并非所有的数据库都支持递归CTE,并且在某些数据库中没有使用递归关键字。

澄清后回答2

号码表来营救

我的例子中的数字表或计数表,如果你想这样称呼它,它是临时的,而且非常小。要使其更大,只需向z添加更多值并添加更多交叉连接。在我看来,数字表和日历表都应该放在每个数据库中。它们非常有用

MS SQL Server 2017架构设置:

问题1:

:

-------------------------------------

原始答案

编辑:进一步澄清问题后,这不会复制行,只会复制列中的数据

像这样的东西可能有用

T-SQL

MySQL

Oracle SQL

PostgreSQL

如果您能提供更多关于您想要什么以及您正在使用什么的详细信息,我们可能会更好地帮助您

注2:根据你的需要,你可能需要做一些数学魔术。上面说,如果GetDup为1,则需要一个副本。如果这意味着您的输出应该是GetDup``GetDup,那么您需要在repeat、replicate或rpad函数中添加一个。ie replicatemycolumn,getdup+1。Oracle SQL将略有不同,因为它使用rpad。

所以,您需要递归解决方案:

with t as (
    select Getdup, CustomerName, CustomerAdd, 0 as id
    from table
    union all
    select Getdup, CustomerName, CustomerAdd, id + 1
    from t
    where id < getdup
)
insert into table (col1, col2, col3)
select Getdup, CustomerName, CustomerAdd
from t
order by getdup
option (maxrecursion 0); 

用您正在使用的数据库标记您的问题。请在表格中提供样本数据和预期结果样本数据已附加。我还将询问您为什么要将重复记录重新插入表中。最终目标是什么?这与o/p有什么关系?@Yogesharma我最初的回答是在OP澄清之前。我已经更新了我的答案,使用计数表来获取行,而不是复制的列。Yogesh。谢谢你的回答,用你的逻辑,我怎样才能实际地将记录插入表中?@jongle。使用插入到。声明。@Jongle。如果要在现有表中插入记录,请以1作为id开始。非常感谢。。插入。。以1开头的id有效。这就是我想要的。曾经有一个次要问题。当前使用的实际表的列太多。是否有办法使用*而不是为每个列指定实际的列名。@Jongle。当然,您可以使用select*。
;WITH z AS (
  SELECT * 
  FROM ( VALUES(0),(0),(0),(0) ) v(x)
)
, numTable AS (
  SELECT num 
  FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY z1.x)-1 num 
    FROM z z1 
    CROSS JOIN z z2 
  ) s1
)
SELECT t1.Getdup, t1.CustomerName, t1.CustomerAdd
FROM mytable t1
INNER JOIN numTable ON t1.getdup >= numTable.num
ORDER BY CustomerName, CustomerAdd
| Getdup | CustomerName |   CustomerAdd |
|--------|--------------|---------------|
|      2 |          Bob | 987 SomeWhere |
|      2 |          Bob | 987 SomeWhere |
|      2 |          Bob | 987 SomeWhere |
|      1 |         John | 123 SomeWhere |
|      1 |         John | 123 SomeWhere |
SELECT replicate(mycolumn,getdup) AS x
FROM mytable
SELECT repeat(mycolumn,getdup) AS x
FROM mytable
SELECT rpad(mycolumn,getdup*length(mycolumn),mycolumn) AS x
FROM mytable
SELECT repeat(mycolumn,getdup+1) AS x
FROM mytable
with t as (
    select Getdup, CustomerName, CustomerAdd, 0 as id
    from table
    union all
    select Getdup, CustomerName, CustomerAdd, id + 1
    from t
    where id < getdup
)
insert into table (col1, col2, col3)
select Getdup, CustomerName, CustomerAdd
from t
order by getdup
option (maxrecursion 0);