如何使用While循环在Sql中仅将表的两列插入临时表? [英] How Can I Insert Only Two Columns Of A Table Into A Temporary Table In Sql Using While Loop?

查看:102
本文介绍了如何使用While循环在Sql中仅将表的两列插入临时表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含四列的表,我需要将此表的两列插入临时表。我怎么能做到这一点。我真的需要帮助

解决方案

正确的方法就像是



< pre lang =sql> INSERT INTO DestinationTable(Field1,Field2)
SELECT Column1,Column2
FROM SourceTable;


尝试使用 insert into..select..from。

示例:

  CREATE   TABLE  #Names 

Name1 VARCHAR 100
Name2 VARCHAR 100


INSERT INTO #Names
选择 Name1,Name2 来自姓名





DEMO [ ^ ]



如果你需要迭代表行。检查以下答案

有没有办法在不使用游标的情况下在TSQL中循环表变量? [ ^ ]

在SQL Server 2008中迭代行 [ ^ ]


I have a table having four columns in it ,i need to insert two columns of this table to a temporary table. How can i accomplish this.I realy need a help

解决方案

The correct way is like

INSERT INTO DestinationTable  (Field1, Field2)
SELECT Column1, Column2
FROM SourceTable;


try with insert into..select..from.
sample :

CREATE TABLE  #Names
  (
    Name1 VARCHAR(100)
    Name2 VARCHAR(100)
  )

INSERT INTO #Names
select  Name1,Name2 from Names



DEMO[^]

if you need to iterate over rows of table. check below answers
Is there a way to loop through a table variable in TSQL without using a cursor?[^]
Iterate through rows in SQL Server 2008[^]


这篇关于如何使用While循环在Sql中仅将表的两列插入临时表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆