2014-12-12 10 views
6

Ho tre tabelle con dati diversi e devo inserire in una tabella TEMP e restituire quella tabella in StoredProcedure.Come inserire più istruzioni select in una tabella temporanea

ho cercato come:

-- To get last 10 Days Letters count 
SELECT col1,col2,1 AS Type, LettersCount 
INTO #temp FROM tblData 

-- To get last 4 weeks Letters count 
SELECT col1,col2,2 AS Type, LettersCount 
INTO #temp FROM tblData 

-- To get month wise Letters count 
SELECT col1,col2,3 AS Type, LettersCount 
INTO #temp FROM tblData 

Visualizzazione errore come

Msg 2714, Level 16, State 1, Line 16 
There is already an object named '#temp ' in the database. 
Msg 102, Level 15, State 1, Line 24 
Incorrect syntax near 'T'. 
Msg 2714, Level 16, State 1, Line 32 
There is already an object named '#temp ' in the database. 

risposta

18

è possibile controllare già esiste o NOT

IF OBJECT_ID ('tempdb..#TempLetters') is not null 
drop table #TempLetters 


SELECT col1,col2,1 AS Type, LettersCount 
INTO #TempLetters FROM tblData 

-- To get last 4 weeks Letters count 
INSERT INTO #TempLetters 
SELECT col1,col2,2 AS Type, LettersCount 
FROM tblData 

-- To get month wise Letters count 
INSERT INTO #TempLetters 
SELECT col1,col2,3 AS Type, LettersCount 
FROM tblData 
+0

Grazie. Funziona bene. –

+0

@Dineshalla Il vostro benvenuto –

1

L'errore si verifica perché la prima selezionare nella dichiarazione crea la tabella e il secondo e terzo cerca di ricreare di nuovo.

cambiare la seconda e la terza query in:

insert into #temp 
select.. 
4

La dichiarazione SELECT INTO può essere utilizzato anche per creare una nuova tabella vuota utilizzando lo schema di un altro select * into tablename from .. qui tablename tavolo non dovrebbe esistere.

Cambia vostro inserto in questo modo:

SELECT col1, 
     col2, 
     1 AS Type, 
     LettersCount 
INTO #temp 
FROM tblData 

-- To get last 4 weeks Letters count 
INSERT INTO #temp 
SELECT col1,col2,2 AS Type,LettersCount 
FROM tblData 

-- To get month wise Letters count 
INSERT INTO #temp 
SELECT col1,col2,3 AS Type,LettersCount 
FROM tblData 
4

Creare la tabella temporanea, una volta, quindi inserire in esso per gli altri due istruzioni SELECT:

SELECT col1, col2, 1 AS Type, LettersCount 
    INTO #temp 
    FROM tblData; 

INSERT INTO #temp 
    SELECT col1, col2, 2 AS Type, LettersCount 
     FROM tblData; 

INSERT INTO #temp 
    SELECT col1, col2, 3 AS Type, LettersCount 
     FROM tblData; 
0

Perché non scrivere una sola istruzione di inserimento e unire le tabelle prima di inserire

with A as 
(
    -- To get last 10 Days Letters count 
    SELECT col1,col2,1 AS Type, LettersCount 
    FROM tblData 
    union all 
    -- To get last 4 weeks Letters count 
    SELECT col1,col2,2 AS Type, LettersCount 
    FROM tblData 
    union all 
    -- To get month wise Letters count 
    SELECT col1,col2,3 AS Type, LettersCount 
    FROM tblData 
) 
select col1, col2, Type, LettersCount 
INTO #temp 
FROM A 

Questo ti aiuterà ad aggiungere più tabelle nel selezionare facilmente se avete bisogno, come non avrete bisogno di tutte le dichiarazioni più insert per loro

+0

In ogni istruzione select ho usato group by e order by. Funzionerà. –

+0

Visualizzazione dell'errore come messaggio 1033, livello 15, stato 1, riga 14 La clausola ORDER BY non è valida nelle viste, funzioni inline, tabelle derivate, sottoquery e espressioni di tabella comuni, a meno che non sia specificato anche TOP o FOR XML. –

+0

ordina non funziona, raggruppa per volontà, tuttavia se vuoi eseguire un ordine prima di inserire, puoi farlo dopo la riga 'Da A' – Hitesh

Problemi correlati