SQL 把公式字段变为值存入值字段

alter PROCEDURE [dbo].[stock050] 
AS
declare @billno varchar(10), @max_num int
select distinct sn, billno=convert(varchar(20),null) into #t_dbf10 from zz_temp221222
select top 1 @billno = billno from t_stoc_stocktak where year_no='2022' and factory='1' order by billno desc

--最大盤點票號 + 1
set @max_num=cast(right(@billno,5) as int)
update #t_dbf10 set billno = '1R' + right(cast(101000+@max_num + sn as char(6)),5) 

--select 'w10', * from #t_dbf10

update zz_temp221222 set billno=b.billno from zz_temp221222 a join #t_dbf10 b on a.sn=b.sn
--select * from zz_temp221222

select id_num=identity(int), year_no='2022', factory='1', billno, item_code, short_desc = (select short_desc from t_inma0010 where item_code=a.item_code), checkqty=qty, qty, unit, warehouse='B', locate 
, det = case when right(rtrim(det),1)='*' then left(det, DATALENGTH(rtrim(det)) - 1) else det end , com_no='1', handled='xxx'
, aa11 = convert(dec(18,4),null)
into #t_dbf20 from zz_temp221222 a

declare @s1 varchar(1000), @i int, @id_num int, @det varchar(100)
declare cur1 scroll cursor 
for select id_num,det from #t_dbf20
open cur1
set @i=1
while @i=1
    begin
        fetch next from cur1 into @id_num,@det
        if @@FETCH_STATUS <> 0
            break

        select @s1 = 'update #t_dbf20 set aa11 =' + @det + ' where id_num='+ convert(varchar(10),@id_num)
        
		execute(@s1)

    end
close cur1
deallocate cur1

--set @s1 = 'update #t_dbf20 set aa11 =' + '8*5'
--execute(@s1)

--select @s1

select 'calc-error', * from #t_dbf20 where checkqty<>aa11

posted on 2022-12-22 15:26  manber  阅读(69)  评论(0)    收藏  举报

导航