2016-07-14 55 views
0

我有以下代碼將數據插入到列中,但是它沒有加載到正確的列中。您認爲哪些是錯誤的?如何從存儲過程插入到多個表中

ALTER PROCEDURE [dbo].[spTESt] 
AS 

DECLARE @Report TABLE (
    c1 int, c2 int, c3 int, c4 int, c5 int 
) 


INSERT INTO @Report (c1) 
SELECT mass as c1 FROM other_sales 
WHERE id='17501' order by mass 

INSERT INTO @Report (c2) 
SELECT mass as c2 FROM other_sales 
WHERE id='17154' order by mass 


INSERT INTO @Report (c3) 
SELECT mass FROM other_sales 
WHERE id='17156' order by mass 


INSERT INTO @Report (c4) 
SELECT mass FROM other_sales 
WHERE id='17500' order by mass 


INSERT INTO @Report (c5) 
SELECT mass FROM other_sales 
WHERE id='17501' order by mass 

它需要根據其條件進入單獨的列。我應該以不同的結構嗎?

+0

你是什麼意思它是沒有加載到正確的列?做所有這些選擇查詢('SELECT mass mass FROM other_sales WHERE id ='17501'order by mass')總是返回單個值? – Ash

+0

我希望它出現在同一行中 – doe

+0

哪種RDBMS適用於?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –

回答

1
DECLARE 
     @Report TABLE (

c1 int , 
c2 int , 
c3 int , 
c4 int , 
c5 int 

    ) 

DECLARE @_C1 INT 
DECLARE @_C2 INT 
DECLARE @_C3 INT 
DECLARE @_C4 INT 
DECLARE @_C5 INT 

SELECT @_C1 = mass FROM other_sales 
WHERE id='17501' order by mass 

SELECT @_C2 = mass FROM other_sales 
WHERE id='17154' order by mass 

SELECT @_C3 = mass other_sales 
WHERE id='17156' order by mass 

SELECT @_C4 = mass other_sales 
WHERE id='17500' order by mass 

SELECT @_C5 = mass other_sales 
WHERE id='17501' order by mass 

INSERT INTO @Report 
     (c1, c2, c3, c4 , c5) 
VALUES (@_C1, -- c1 - int 
      @_C2, -- c2 - int 
      @_C3, -- c3 - int 
      @_C4, -- c4 - int 
      @_C5 -- c5 - int 
     ) 
+0

這幫助了很多謝謝! – doe

-1

你的最後一列聲明

DECLARE 
     @Report TABLE (

c1 int , 
c2 int , 
c3 int , 
c4 int 

    ) 
+0

是啊對不起錯別字錯誤,當我在這裏添加它,但多數民衆贊成沒有問題 – doe

+0

洛爾好吧,沒有憂慮,但有什麼問題呢?你想要這些值出現在一行上,而不是多行? –

+0

是啊!多數民衆贊成它,我希望它出現在一行,但現在其去所有地方 – doe

2

假設所有select陳述returnsingle值後刪除逗號,你可以嘗試一個聲明如下:

ALTER PROCEDURE [dbo].[spTESt] 
AS 
--...... 
DECLARE @Report TABLE (c1 int, c2 int, c3 int, c4 int, c5 int) 

INSERT INTO @Report (c1, c2, c3, c4, c5) values (
(SELECT mass FROM other_sales WHERE id='17501'), 
(SELECT mass FROM other_sales WHERE id='17154'), 
(SELECT mass FROM other_sales WHERE id='17156'), 
(SELECT mass FROM other_sales WHERE id='17500'), 
(SELECT mass FROM other_sales WHERE id='17501')) 
--.... 
+0

謝謝,這也幫了很多 – doe

相關問題