使用子查询填充 unpivot 列列表
我目前无法访问服务器来测试这一点,但是我将如何去做类似以下的事情,
UNPIVOT (X for XY IN (
SELECT column_name AS [XY]
FROM Information_schema.Columns
WHERE Table_name = XYZ AND column_name LIKE '[0-9][0-9][0-9][0-9]'
)) AS unpivotedData
即是否可以动态填充要取消透视的列列表,而不是静态声明它们
I currently do not have access to a server to test this out on, but how would I go about doing something like the following
UNPIVOT (X for XY IN (
SELECT column_name AS [XY]
FROM Information_schema.Columns
WHERE Table_name = XYZ AND column_name LIKE '[0-9][0-9][0-9][0-9]'
)) AS unpivotedData
i.e. is it possible to dynamically populate a list of columns to unpivot rather than statically declare them
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用动态 sql 来执行此操作。请参考以下SO问题:
T-SQL 透视?可以从行值创建表列
You would need to use dynamic sql to do so. Refer to the following SO question:
T-SQL Pivot? Possibility of creating table columns from row values