基于现有的数据库表创建@TableVariable? [英] Create @TableVariable based on an existing database table?

查看:29
本文介绍了基于现有的数据库表创建@TableVariable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在存储过程中使用表变量,但这里有一个问题.我的表非常大,声明表变量也需要很长的代码来编写和调试.

I want to use table variables in stored procedures but here is an issue. My tables are very large and declaring a table variable need a long code to write and debug as well.

请给我一些快速声明表变量的方法,是否可以基于现有表创建表变量?

Kindly advice me some way to declare table variables quickly, is it possible to create table variable based on an existing table ?

或者请分享任何技巧以创建用于创建表变量的代码.

Or please share any tip to create code for creating table variable.

谢谢

推荐答案

如本SO问题 你不能选择表变量.

As discussed in this SO Question you can't select into a table variable.

当您说大"时,如果您的意思是很多列,那么对您来说最好的方法可能是将该表编写为 create 并保存定义并在您的 Declare 语句中使用它.

When you say "large", if you mean a lot of columns, the best approach for you would probably be to script that table as create and save the definition and use that in your Declare statement.

如果您的意思是表变量中的行数很大,您可能需要考虑使用临时表,然后您可以执行 SELECT INTO 语句来创建它基于原始.

If you mean large as far as the number of rows you'll have in the table variable, you may want to consider using a temporary table which you could then do a SELECT INTO statement to create it based off of the original.

SELECT * INTO #tmpTable FROM srcTable

这篇关于基于现有的数据库表创建@TableVariable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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