简体   繁体   English

如何将该选择语句转换为SQL Server中的插入表?

[英]How can I convert this select statement into an insert table in SQL Server?

I've found answers online, but none with specifically what I am doing. 我在网上找到了答案,但是没有一个明确的答案。 I couldn't get anything to work. 我什么都没工作。

I have a select that randomly selects records and I just want to be able to have it insert into a table instead. 我有一个选择,它可以随机选择记录,而我只是希望能够将其插入表中。

My SQL is 我的SQL是

with data as (
select *, row_number() over (partition by DIVISION order by DIVISION) as rn
    from WORK
)
select *
from data
where rn <= @randomNumber or (rn - @randomNumber) % 18 = 1 AND DIVISION != 4;

I know I am not supposed to do the partition by DIVISION order by DIVISION but that's a separate issue I believe. 我知道我不应该按DIVISION顺序进行分区,但是我认为这是一个单独的问题。

I just need to be able to insert this data into another table WORK_CLEAN 我只需要能够将此数据插入另一个表WORK_CLEAN

You just need to add an insert statement. 您只需要添加一条插入语句。

with data as (
select *, row_number() over (partition by DIVISION order by DIVISION) as rn
    from WORK
)
insert yourTable ([ColumnsHere])
select *
from data
where rn <= @randomNumber or (rn - @randomNumber) % 18 = 1 AND DIVISION != 4;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM