에러메세지는,
Unable to create constraint. See previous errors.
제약에관한 에러인거 같은데 create table로 임시테이블을 만들때 발생합니다.
같은 환경의 다른 DB에서는 에러가 안나는데 새로바꾸는 DB에서만 에러가 납니다. 검색해보아도 이런에러는 없더군요.
환경은 MSSQL 6.5입니다.
아래는 전체 SQL문입니다.
---------------------------------------------------------------------
declare @sf_no numeric(10)
,@j_sno numeric(07)
,@c_sno numeric(07)
,@io_date smalldatetime
,@io_qty numeric(14,2)
,@row_cnt numeric(03) -- 변경날짜 갯수
,@old_sf_no numeric(10) -- 이 세개의 변수는
,@old_c_sno numeric(10) -- 전과 변경량이 같
,@old_qty numeric(10,2) -- 으면 기록 no
,@temp_date smalldatetime -- 커서내에 쓰는 변수
,@io_date1 smalldatetime -- 변경날짜
,@io_date2 smalldatetime
,@io_date3 smalldatetime
,@io_date4 smalldatetime
,@io_date5 smalldatetime
,@io_date6 smalldatetime
,@io_date7 smalldatetime
,@io_date8 smalldatetime
,@io_date9 smalldatetime
,@io_date10 smalldatetime
,@firstdate smalldatetime
,@io_Fdate smalldatetime
,@io_Tdate smalldatetime
,@year numeric(4)
,@month numeric(2)
,@sa_sno numeric(7)
,@Fsf_no numeric(10)
,@Tsf_no numeric(10)
select @io_Fdate = '2003-04-01'
select @io_Tdate = '2003-04-30'
select @year = 2003
select @month = 5
select @sa_sno = 1
select @Fsf_no = 2003050000
select @Tsf_no = 2003059999
select @firstdate =(select convert(smalldatetime,
convert(char(4),@year)+"-"+
convert(char(2),@month)+"-01"))
/*** 변경날짜 구함 ***/
declare CUR_DATE insensitive cursor for
select distinct HH.IO_DATE
from JJ_SALES_PLAN PP
,JJ_SALES_PLAN_QTY_HIST HH
where PP.SA_SNO = @sa_sno
and PP.SP_YY = @year
and PP.SP_MM = @month
and PP.SF_NO BETWEEN @Fsf_no AND @Tsf_no
and PP.SP_SNO = HH.SP_SNO
select @row_cnt = 0
/*** 자료 fetch ***/
open CUR_DATE
while (@@cursor_rows <> 0) begin
fetch next from CUR_DATE into @temp_date
if @@fetch_status = -1 break
if @@fetch_status = -2 continue
select @row_cnt = @row_cnt + 1
if @row_cnt > 10
select @row_cnt = 10
if @row_cnt = 1
select @io_date1 = @temp_date
if @row_cnt = 2
select @io_date2 = @temp_date
if @row_cnt = 3
select @io_date3 = @temp_date
if @row_cnt = 4
select @io_date4 = @temp_date
if @row_cnt = 5
select @io_date5 = @temp_date
if @row_cnt = 6
select @io_date6 = @temp_date
if @row_cnt = 7
select @io_date7 = @temp_date
if @row_cnt = 8
select @io_date8 = @temp_date
if @row_cnt = 9
select @io_date9 = @temp_date
if @row_cnt = 10
select @io_date10 = @temp_date
end -- while
close CUR_DATE
deallocate CUR_DATE
/*** 임시 테이블 생성 ***/
create table #TP_TABLE
( SF_NO numeric(10)
,J_SNO numeric(07)
,C_SNO numeric(07)
,IO_QTY1 numeric(10,2) NULL default 0
,IO_QTY2 numeric(10,2) NULL default 0
,IO_QTY3 numeric(10,2) NULL default 0
,IO_QTY4 numeric(10,2) NULL default 0
,IO_QTY5 numeric(10,2) NULL default 0
,IO_QTY6 numeric(10,2) NULL default 0
,IO_QTY7 numeric(10,2) NULL default 0
,IO_QTY8 numeric(10,2) NULL default 0
,IO_QTY9 numeric(10,2) NULL default 0
,IO_QTY10 numeric(10,2) NULL default 0
)
SELECT SP.SP_SNO,SP.SF_NO,SP.J_SNO,SP.C_SNO
,(SP.IO_QTY_INPUT + SP.IO_QTY_TRANS - SP.NY_QTY) AS QTY
INTO #T
FROM JJ_SALES_PLAN SP
WHERE SP.SA_SNO = @sa_sno
AND SP.SP_YY = @year
AND SP.SP_MM = @month
AND SP.SF_NO BETWEEN @Fsf_no AND @Tsf_no
AND SP.SP_SNO IN(SELECT SP_SNO FROM JJ_SALES_PLAN_HIST
WHERE IO_DATE BETWEEN @io_Fdate AND @io_Tdate
AND SP_FIRST IN(1,2)
)
declare CUR_DATA_SA insensitive cursor for
select PP.SF_NO, PP.J_SNO, PP.C_SNO
,HH.IO_DATE
,PP.QTY+HH.TOT_QTY_GA
from #T PP
,JJ_SALES_PLAN_QTY_HIST HH
where PP.SP_SNO = HH.SP_SNO
order by PP.SF_NO, PP.C_SNO, HH.IO_DATE
select @old_sf_no = 0
select @old_c_sno = 0
select @old_qty = 0
/*** 자료 fetch ***/
open CUR_DATA_SA
while (@@cursor_rows <> 0) begin
fetch next from CUR_DATA_SA
into @sf_no ,@j_sno ,@c_sno ,@io_date ,@io_qty
if @@fetch_status = -1 break
if @@fetch_status = -2 continue
if (@sf_no <> @old_sf_no) or (@c_sno <> @old_c_sno)
begin
select @old_sf_no = @sf_no
select @old_c_sno = @c_sno
select @old_qty = 0
end
if not exists (select SF_NO from #TP_TABLE
where SF_NO = @sf_no
and J_SNO = @j_sno
and C_SNO = @c_sno)
insert into #TP_TABLE (SF_NO,J_SNO,C_SNO)
values (@sf_no ,@j_sno ,@c_sno)
if (@io_date1 = @io_date) and (@io_qty <> @old_qty)
update #TP_TABLE
set IO_QTY1 = @io_qty
where SF_NO = @sf_no
and J_SNO = @j_sno
and C_SNO = @c_sno
if (@io_date2 = @io_date) and (@io_qty <> @old_qty)
update #TP_TABLE
set IO_QTY2 = @io_qty
where SF_NO = @sf_no
and J_SNO = @j_sno
and C_SNO = @c_sno
if (@io_date3 = @io_date) and (@io_qty <> @old_qty)
update #TP_TABLE
set IO_QTY3 = @io_qty
where SF_NO = @sf_no
and J_SNO = @j_sno
and C_SNO = @c_sno
if (@io_date4 = @io_date) and (@io_qty <> @old_qty)
update #TP_TABLE
set IO_QTY4 = @io_qty
where SF_NO = @sf_no
and J_SNO = @j_sno
and C_SNO = @c_sno
if (@io_date5 = @io_date) and (@io_qty <> @old_qty)
update #TP_TABLE
&nb