无论 SQL Server 中的列名如何,都将整行选择到另一个表中
有没有办法从临时表(表只有一行)中选择行到另一个具有不同名称的列的表中?例如:
TempTable
FirstName LastName Column1 Column2
------------ ------------ ----------- -----------
Joe Smith OKC TX
OtherTable
FirstName LastName Region1 Region2 Region3
------------ ------------ ----------- ----------- ----------
NULL NULL NULL NULL NULL
我需要按照与 TempTable 中的列相同的顺序将数据复制到 OtherTable 中。 TempTable 并不总是相同......因为有时它有 3 列,有时只有 2 列......等等。如果它的列数与 OtherTable 不同,则其余“Region”列应保持为空。
最终结果应该是:
OtherTable
FirstName LastName Region1 Region2 Region3
------------ ------------ ----------- ----------- ----------
Joe Smith OKC TX NULL
另外,TEMPTable 中的列名称永远不会相同...因为一次它会是“Column1”...下一次可能是“XXXXX1” 。这就是为什么我只想复制数据...数据将始终按正确的顺序...
哈哈...这有意义吗?这是针对 SQL Server 2005
Is there a way to select the row from a temp table (table has only one row anyway), into another table that has some columns with differenet names? For example:
TempTable
FirstName LastName Column1 Column2
------------ ------------ ----------- -----------
Joe Smith OKC TX
OtherTable
FirstName LastName Region1 Region2 Region3
------------ ------------ ----------- ----------- ----------
NULL NULL NULL NULL NULL
I need to copy the data, in the same order as the columns from TempTable into OtherTable. TempTable will not always be the same....as in sometimes it will have 3 columns, sometimes just 2...etc. If it does not have the same number of columns as OtherTable, the the remaining "Region" columns should stay null.
The end result should be:
OtherTable
FirstName LastName Region1 Region2 Region3
------------ ------------ ----------- ----------- ----------
Joe Smith OKC TX NULL
PLUS the column names in TEMPTable will NEVER be the same...as in one time it will be "Column1"...the next time it could be "XXXXX1". That's why I just want to copy data only...the data will always be in the correct order...
LOL...does this even make sense? This is for SQL Server 2005
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑........添加动态SQL生成
此代码将生成INSERT语句以从#TEMP插入#TEMP。如果您要从 #temp 转到常规表,您可以对其进行调整以适合您的目的。
旧答案
是的,您可以执行此操作,但您必须为每种类型的
INSERT
编写不同的语句。您必须在两个位置指定列名称 -INSERT INTO
和SELECT
如果源表和目标表中的列数相同,请执行以下
操作要做的就是映射如下:
MyTable.MyColumn01 ->表1.Column1
MyTable.MyColumn02 ->表1.列2
MyTable.MyColumn03 -> Table1.Column3
如果源表的列数较少,则可以使用 NULL 值代替列名
或者,您可以只使用两个列名
如果目标表的列数少于源表,则您可以必须忽略源中的列
EDIT ........ Dynamic SQL Generation added
This code will generate INSERT statements to INSERT from #TEMP into #TEMP. You can tweak it to suit your purpose if you are going from #temp to regular tables.
OLD ANSWER
Yes you can do this but you have to write different statements for each type of
INSERT
. You do have to specify column names in both places - theINSERT INTO
and theSELECT
If you have the same number of columns in your Source and Destination tables, do this
What this will do is map as follows:
MyTable.MyColumn01 -> Table1.Column1
MyTable.MyColumn02 -> Table1.Column2
MyTable.MyColumn03 -> Table1.Column3
If the Source has less columns, you can use a NULL value in place of the column name
OR you can just use two column names
If the destination table has less columns than the source, then you have to ignore columns from the source
您可以使用动态 SQL 做一些事情。
我建议阅读“动态 SQL 的诅咒和祝福 -
如果您不熟悉的话,处理动态表和列名”。
示例如下。您可以改进这一点,以确保源列和目标列具有兼容的类型,或者排除身份列或计算列,但它应该给您一个想法。
You could do something with dynamic SQL.
I recommend reading "The Curse and Blessings of Dynamic SQL -
Dealing with Dynamic Table and Column Names" if this is new to you.
Example follows. You could improve this to be sure that source and destination columns are of compatible types or to exclude identity or computed columns for example but it should give you an idea.
您可以指定目标表的列:
在此示例中,OtherTable.Region3 将最终为 NULL(或者如果它有 DEFAULT 值,它将使用该值)。
INSERT
中的列数必须与SELECT
中的列数匹配。因此,您必须知道 TempTable 中有多少列,并使插入的列列表匹配。但是,如果您只是乱扔
SELECT *
,则无法使用隐式列来实现此目的。回复您的评论:您可以使用
INFORMATION_SCHEMA
查询表中的列数和列名。然后,您将编写应用程序代码来根据此 information_schema 查询的结果创建 SQL
INSERT...SELECT
语句。注意:通过 information_schema 查询临时表需要 特殊处理。
You can specify the columns of the target table:
In this example, OtherTable.Region3 will end up NULL (or if it has a DEFAULT value, it'll use that).
The count of columns in the
INSERT
must match the count of columns in theSELECT
. So you must know how many columns in TempTable and make the list of columns for the insert match.But there's no way to do it with implicit columns, if you're just throwing
SELECT *
around.Re your comment: You can use the
INFORMATION_SCHEMA
to query the count and the names of the columns in the table.Then you would write application code to create your SQL
INSERT...SELECT
statement based on the results from this information_schema query.Note: Querying temp tables through the information_schema requires special handling.