在SQL中将临时表解析为另一个表,保留列名

发布于 2025-01-08 04:47:17 字数 537 浏览 3 评论 0原文

我有一个临时表,看起来像这样。

| USER_NO | MY_CODE1      | MY_CODE2      |  
| UserNo1 | UserNo1_Code1 | UserNo1_Code2 |  
| UserNo2 | UserNo2_Code1 | UserNo2_Code2 |

其中第一行是列名称,第二行和第三行是值。我需要将这些记录附加到另一个表中。

| CusRef  | CodeName | CodeValue     |  
| UserNo1 | MY_CODE1 | UserNo1_Code1 |  
| UserNo2 | MY_CODE2 | UserNo1_Code2 |  
| UserNo2 | MY_CODE1 | UserNo2_Code1 |  
| UserNo2 | MY_CODE2 | UserNo2_Code2 | 

其他表中还有许多其他字段,我需要从其他表中引用这些字段,但这是另一个问题。

我可以使用什么 SQL 来使这第一部分工作?

I have a temporary table which looks something like this.

| USER_NO | MY_CODE1      | MY_CODE2      |  
| UserNo1 | UserNo1_Code1 | UserNo1_Code2 |  
| UserNo2 | UserNo2_Code1 | UserNo2_Code2 |

Where the first line is column names, the 2nd and 3rd are values. I need to append these records into another table.

| CusRef  | CodeName | CodeValue     |  
| UserNo1 | MY_CODE1 | UserNo1_Code1 |  
| UserNo2 | MY_CODE2 | UserNo1_Code2 |  
| UserNo2 | MY_CODE1 | UserNo2_Code1 |  
| UserNo2 | MY_CODE2 | UserNo2_Code2 | 

There a number of other fields in that other table which I need to reference from other table but that is another issue.

What SQL could I use to make this first bit work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2025-01-15 04:47:17

通过UNPIVOT可以获得您想要的结果

SELECT USER_NO As CusRef,
       CodeName,
       CodeValue     
FROM #YourTempTable
UNPIVOT (CodeValue FOR CodeName IN (MY_CODE1, MY_CODE2)) AS U

Your desired results can be obtained with UNPIVOT

SELECT USER_NO As CusRef,
       CodeName,
       CodeValue     
FROM #YourTempTable
UNPIVOT (CodeValue FOR CodeName IN (MY_CODE1, MY_CODE2)) AS U
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文