2016-10-26 2 views
0

while 루프에서 동적 테이블을 업데이트하고 싶습니다.저장 프로 시저에서 while 루프를 사용하여 테이블을 업데이트하는 방법?

While (@j<[email protected]_getRowCount) 
Begin 

Set @firstcolumn = (Select SplitFirst_tblAR from @result_AR Where rownumber = @j) //String// 
Set @secondcolumn = (Select EMail_tblAR from @result_AR Where rownumber = @j) //String// 
Set @thirdcolumn = (Select SplitFirst_tblKul from @result_AR Where rownumber = @j) //String// 
Set @fourthcolumn = (Select EMail_tblKul from @result_AR Where rownumber = @j) //String// 

insert into @test Values(@tmp_ID, @firstcolumn,@secondcolumn,@thirdcolumn,@fourthcolumn) 

if ((@firstcolumn = @thirdcolumn) AND (@secondcolumn != @fourthcolumn) AND (@firstcolumn != '')) 
begin 

Set @q_updateTable = 'Update '+ quotename(@tablename) +' Set '[email protected]+' = ''' + @fourthcolumn + ''' Where ID = ' + @tmp_ID + '' 
Exec sp_executesql @q_updateTable 

end 

SET @j = @j+1 
End 

내 result_AR 테이블 :

enter image description here

나는 오류가 알고 여기에 :

어디 ID = '+ @tmp_ID 여기

내 코드입니다 + ''

내가 절

같은 경우 '+ @ 럼 이름 +'= '' '+ @secondcolumn +' '' '

코드를 어디에서이 변경3210 올바르게 작동합니다.

ID로 내 where 절을 설정할 수없는 이유는 무엇입니까? 나는 정수로서 ID 값을 얻는다. 오류가 '쿼리가 오류로 완료되었습니다.'입니다. 미리 감사드립니다.

+0

이 변수에 무엇입니까? 그것을 추가 할 수 있습니까? – rbr94

+1

'@tmp_ID '의 데이터 유형은 무엇입니까? –

+1

왜 루프를 이렇게할까요? –

답변

3

id는 정수 값이고 string (varchar)을 연결하기 때문에 where 절에서 Id를 설정할 수 없습니다. 그래서 먼저 (String) varchar로 변환해야하고 where 절을 사용할 수 있습니다.

처럼 :

설정 @q_updateTable = '업데이트'+ QUOTENAME (@tablename) '설정'+ + @ 럼 이름 + '=' ''+ @fourthcolumn + '' '어디 ID ='+ 변환 (VARCHAR @ tmp_ID) + '' Exec에서 sp_executesql을 @q_updateTable "@tmp_ID"의 insted 사용 할

")는 tmp_ID @ (VARCHAR로 변환,"

+0

이 솔루션이 작동하면 투표 해주세요. –

+0

왜 잘못된 이유로 편집을 거부합니까? – rbr94

관련 문제