2016-07-14 4 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 FROM other_sales 어디에서 id = '17501'질량 기준으로)는 항상 단일 값을 반환합니까? – 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

모든 select 문이 값을 returnsingle 가정 마지막 열 선언

DECLARE 
     @Report TABLE (

c1 int , 
c2 int , 
c3 int , 
c4 int 

    ) 
+0

그래, 내가 여기에 그것을 추가했지만 죄송합니다 typo 오류가 문제가되지 않습니다 – doe

+0

롤 확인, 안심, 문제는 뭐지? 이 값을 여러 줄이 아닌 한 줄에 표시 하시겠습니까? –

+0

예! 그게, 내가 그것을 라인에 나타나고 싶지만, 지금은 곳곳에 가서 – doe

2

후 쉼표를 제거, 당신은 하나의 문을 시도 할 수 :

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

관련 문제