简体   繁体   English

如何将动态 sql 变量插入临时表

[英]How to insert a dynamic sql variable into a temporary table

So I want to insert the variables email, username, and age from a stored procedure into a temporary table and am running into a few issues.因此,我想将存储过程中的变量 email、用户名和年龄插入到临时表中,并且遇到了一些问题。

Declare @email [varchar](8000),@user_name [varchar](8000), age[int]

select distinct value as value into #temptable from @email,@user_name,@age select * from #temptable select distinct value as value into #temptable from @email,@user_name,@age select * from #temptable

I am getting a syntax error.我收到语法错误。 Does anybody have any advice?有人有什么建议吗?

Generally, I would use this syntax to insert the results of a stored procedure into a table or table variable:通常,我会使用这种语法将存储过程的结果插入到表或表变量中:

INSERT #temptable EXECUTE dbo.MyStoredProcedureName

This assumes that the stored procedure returns the columns in the correct order that the table expects.这假定存储过程以表期望的正确顺序返回列。

For example (table variable example):例如(表变量示例):

declare @temptable table
(
    name varchar(255),
    field varchar(255),
    filename varchar(255),
    filegroup varchar(255),
    size varchar(255),
    maxsize varchar(255),
    growth varchar(255),
    usage varchar(255)
);

INSERT @temptable Exec sp_helpfile;
select * from @temptable ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM