我有这个调用其他过程的存储过程(这会返回一些数据行)。
在第一个过程中,我需要将所有这些行插入到永久表中并返回该表的内容。我有这个,但有一个临时表。
如何插入永久表中然后将其返回?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[paBltBuscarBoletasASA] @id_Asa int
-- Add the parameters for the stored procedure here
AS
DECLARE @Query int
CREATE TABLE #tablaTemporal (Numero_Pregunta varchar, Numero_Boleta int, Cultivo varchar, Numero_Siembra int, Detalle_Error varchar)
DECLARE miCursor CURSOR FOR
SELECT
localizacion.c_Fk_IdBoleta
FROM
Blt_Boleta as boleta, Fnc_Localizacion as localizacion
WHERE
boleta.c_Pk_IdBoleta = localizacion.c_Fk_IdBoleta AND
localizacion.si_CodAsa = @id_Asa
OPEN miCursor
FETCH NEXT FROM miCursor INTO @Query
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #tablaTemporal(Numero_Pregunta, Numero_Boleta, Cultivo, Numero_Siembra, Detalle_Error) exec dbo.paBltMarcarErroresBoleta @Query
FETCH NEXT FROM miCursor INTO @Query
END
CLOSE miCursor
DEALLOCATE miCursor
答案 0 :(得分:1)
您可以在存储过程的底部执行此操作,将临时表复制到永久表中:
-- To insert into a permanent table
INSERT INTO MyPermanentTable (Numero_Pregunta, Numero_Boleta, Cultivo,
Numero_Siembra, Detalle_Error)
SELECT Numero_Pregunta, Numero_Boleta, Cultivo, Numero_Siembra, Detalle_Error
FROM #tablaTemporal
-- To get the result set that was added back to the caller
SELECT Numero_Pregunta, Numero_Boleta, Cultivo, Numero_Siembra, Detalle_Error
FROM #tablaTemporal
答案 1 :(得分:0)
如果要将存储过程的结果存储到永久数据库表中,则可以执行以下操作:
INSERT INTO MyPermenantTable Execute [dbo].[paBltBuscarBoletasASA] parameter1, parameter2
以上查询对我有用...希望它也适合你。