将存储过程结果集放入表中 - 除了 INSERT INTO...EXEC 之外还有其他快捷方式吗?
我正在创建一个临时表,它是存储过程结果集的结果。现在我正在使用create table
语句,然后是insert into....exec
。
它完成了工作,但我很好奇是否还有其他方法可以解决这个问题?我希望可以运行 select into
,让存储过程的结果集充当 select 语句的角色,这样我就不必事先编写 create 语句(这样如果存储过程的列发生变化,不需要任何修改。)
如果有其他方法可以更好地满足我的需求,我很乐意听到它们。非常感谢。
I'm creating a temp table that's the result of a stored procedure's result set. Right now I'm using the create table
statement, followed by insert into....exec
.
It gets the job done but I was curious if there are other ways of going about this? I wish it were possible to run a select into
, with the stored procedure's result set serving the role of the select statement, so that I wouldn't have to write a create statement beforehand (so that if the stored proc's columns change, there wouldn't be any modifications needed.)
If there are other ways to go about this that might fit my needs better I'd love to hear about them. Thanks very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种方法:表变量和CTE。每个都有好处,您的场景往往让我相信,如果源数据很大,临时表是最佳选择。如果不是,我会倾向于 CTE。
There are a couple of ways: Table variables and CTEs. There are a benefits to each and your scenario tends to lead me to believe that a Temp Table is the best option if the source data is substantial. If it's not, I would lean towards a CTE.
一个技巧:
存储过程不是表或视图,因此您不能期望像表或视图那样的记录集。
您对“如果存储过程列发生变化”的评论忽略了这个事实:它不应该改变
One trick:
Stored procs are not tables or views so you can't expect a recordset like you would a table or view.
You comment about "if the stored proc columns change" is overlooking this fact: it shouldn't change