简体   繁体   English

如何将存储过程的结果插入到新表中?

[英]How can I insert result from stored procedure to a new table?

I have a stored procedure that returns a SELECT (can't convert it into a view because it does a little bit more than a SELECT ). 我有一个存储过程返回一个SELECT (无法将其转换为视图,因为它比SELECT还要多)。 I want to store the results into a new non temporary table. 我想将结果存储到新的非临时表中。

So far this is what I've tried 到目前为止,这是我尝试过的

select * 
into newTable 
from (StoredProcedure) t

But it throws an error: 但这会引发错误:

Incorrect syntax near ')'. ')'附近的语法不正确。

EDIT: My SP is 编辑:我的SP是

CREATE PROCEDURE Soporte.ManejoSPs
@NombreSP nvarchar (200)
AS

declare @qry nvarchar (500)

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_NAME = @NombreSP + 'Table')
BEGIN

    set @qry = 'select top 0 into ' + @NombreSP  + 'Table from (@NombreSP)     t'
    print @qry
    exec(@qry)
END


declare @Ultima datetime
declare @Tabla nvarchar(200)
declare @Minutos int

Select @Minutos = Minutos, @Tabla = NombreTabla, @Ultima = UltimaVez from     Soporte.ManejoSP where NombreSP = @NombreSP

If (datediff(mi, @Ultima, getdate()) > @Minutos)
BEGIN

    set @qry = 'INSERT INTO ' + @Tabla + ' exec ' + @NombreSP
    exec(@qry)
END

set @qry = 'Select * from ' + @Tabla
exec(@qry)

Note : The following will only work up until SQL Server 2008 R2. 注意 :以下内容仅适用于SQL Server 2008 R2。 For SQL Server 2012+ this will not work (requires WITH RESULT SETS , see here for some details on its specification). 对于SQL Server 2012+,这将不起作用(需要WITH RESULT SETS ,有关其规范的一些详细信息,请参见此处 )。


If your stored procedure is called t (in database db_name and schema schema_name ) and the table newTable doesn't exist yet: 如果您的存储过程名为t (在数据库db_name和schema schema_name ),并且表newTable还不存在:

SELECT
  *
INTO
  newTable
FROM
  OPENROWSET (
    'SQLNCLI',
    'Server=localhost;Trusted_Connection=yes;',
    'SET FMTONLY OFF; EXEC [db_name].[schema_name].t;'
  );

If the server is a named instance, you need to supply the proper Server parameter ( ServerName\\Instance ). 如果服务器是命名实例,则需要提供适当的Server参数( ServerName\\Instance )。

For this to work, you need the execute following first to allow Ad Hoc Distributed Queries . 为此,您需要首先执行以下命令以允许Ad Hoc Distributed Queries You only need to execute this once. 您只需要执行一次。

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO

If the newTable table already exists: 如果newTable表已经存在:

INSERT INTO newTable
EXEC t

暂无
暂无

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

相关问题 如何将存储过程的结果插入到新表中 - How can I insert the results of a stored procedure into a new table 如何创建一个存储过程,该存储过程从表中获取值并将其插入新表中 - How can I create a stored procedure which takes values from a table and insert them into new table 如何将 sql 服务器存储过程数据插入到新表中? - how can insert sql server stored procedure data to the new table? 如何将查询的不同结果从一个表连接到存储过程中的另一个表 - How can I join different result of a query from a table to another table in a stored procedure 如何将 SQL Server 存储过程的结果集从一台服务器导出到另一台服务器上的表中? - How can i export the result set of an SQL Server stored procedure from one server into a table on another server? 为什么我不能在我的存储过程deleteByMonth中插入我的表变量,尽管查询产生结果? - Why can't I insert into my table variable in my stored procedure deleteByMonth, altough the query yields a result? 从存储过程插入临时表,该存储过程返回多个结果集 - Insert Into temp table from a stored procedure that returns multiple result sets 如何将存储过程和varchar的结果插入到临时表中? - How to insert result of a stored procedure and a varchar into a temp table? 如何使用插入创建存储过程? - How can I create a stored procedure with an insert? 如何将存储过程的结果插入具有额外可空列的表中 - How to insert result of stored procedure into a table with extra nullable columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM