2015-10-08 6 views
5

Ho una stored procedure che recupera tre colonne da più tabelle. Voglio ottenere i risultati in una tabella multivalore definita dall'utente e passare la variabile a un'altra procedura per eseguire operazioni sui dati variabili. Tuttavia non sta funzionando. Ho il codice seguente. Qualche idea sul perché non funziona?Come assegnare dati a un tipo di tabella definito dall'utente da Seleziona query in SQL Server 2014

--This is the initial stored procedure 
Create Procedure spSelectData 
AS 
BEGIN 
    Select 
     Userid, first_date, last_update 
    From Users 
END 

--This is to create the table type. 
Create type Task1TableType AS TABLE 
(
    Userid nvarchar(20), 
    First_date datetime, 
    Last_update datetime 
) 

--Declare a table of type 
DECLARE @firstStep AS Task1TableType 
(
    Userid nvarchar(20), 
    First_date datetime, 
    Last_update datetime 
) 

Insert @firstStep EXEC spSelectData 

Select * from @firstStep 

-- This is the procedure 1 
CREATE PROC spTest1 
    @TTType Task1TableType READONLY 
AS 
BEGIN 
    Select * from @TTType 
END 
+0

Che cosa non funziona, sono gli errori? – Iztoksson

+0

Mostra la tua istruzione 'EXECUTE spTest1'. –

risposta

4

Il problema è qui:

DECLARE @firstStep AS Task1TableType 
(
    Userid nvarchar(20), 
    First_date datetime, 
    Last_update datetime 
) 


Insert @firstStep 
EXEC spSelectData; 

dovrebbe essere:

DECLARE @firstStep AS Task1TableType; 

Insert INTO @firstStep 
EXEC spSelectData; 

EXEC spTest1 
    @firstStep; 

Non v'è alcuna necessità di definire colonne in cui è definito il tipo e INSERT richiedono INTO clausola. Dopo che cambia il tuo codice funziona.

SqlFiddleDemo

Problemi correlati