| Author |
Topic |
|
pap
Starting Member
13 Posts |
Posted - 2004-01-22 : 09:00:58
|
| Hello,Can you help me with this:How can I execute a formula in a sp.The code should be like this:declare @text varchar(5000)declare @size_PO varchar(7), @qtd_PO varchar(7), @i intset @i=8while @i<34beginif (@i<10)beginset @size_PO='@Col00' + convert(varchar(1), @i)set @qtd_PO='@Col0' + convert(varchar(2), @i+25)endelsebeginset @size_PO='@Col0' + convert(varchar(2), @i)set @qtd_PO='@Col0' + convert(varchar(2), @i+25)endset @text='INSERT into sffscxx0 values(@gssto, @Col007, @Col003,'+@size_PO+', @Col002,'+@qtd_PO+', 0, @Col006, convert(datetime, getdate(),112),'+'''SERVER'''+')'execute @text -- or something similarset @i=@i+1endis this possible to do ???Thanks |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-01-22 : 09:08:39
|
| Forget about T-SQL -- can you explain to use what you are trying to do? it look slike you have two input parameters and you want to insert rows into a table in some manner via some looping, but I don't quite get it. Just explain in regular old english what you are trying to do and what your needs are, and we'll help you get there.- Jeff |
 |
|
|
pap
Starting Member
13 Posts |
Posted - 2004-01-22 : 09:38:43
|
| Thank you Jeff I will try.Let me explain:I have a table with 25 fields of the same time "shoe size" (@Col008 to @Col032) and another 25 fields "quantity" (@Col033 to @Col057). Here is an examplesize quantity40 141 543 12... ...What I need is to pick all the sizes and quantities and put them in another table "Stock control online" based in some conditions, Sales order, production order, etc.So, my intention is to, with a while loop insert all the data into the other table "stock control online"If I do this step by step, i.e. one insert for each size, quantity (without the loop) it works, but the code will be repeated 25 times (I think that this is bad programming) so I try to do a while loop with the repeated code and change only the fields with this concatenated formulaset @size_PO='@Col00' + convert(varchar(1), @i)set @qtd_PO='@Col0' + convert(varchar(2), @i+25)But for some reason that I can't solve in the insert the system is puting the name of the variable instead of the value of the variableLike thisINSERT into sffscxx0 values(..., ..., ...,@Col008, ...,@Col033, ..., ..., ...,'SERVER')When what I need is thisINSERT into sffscxx0 values(..., ..., ...,'40', ...,1, ..., ..., ...,'SERVER')Hope that I could make me undestoodThanks a lot |
 |
|
|
Nazim
A custom title
1408 Posts |
Posted - 2004-01-22 : 10:05:01
|
| why do require seperate variable's for each loop.Can you post the whole Sp Code . -------------------------What lies behind you and what lies ahead of you are small matters compared to what lies within you.-Ralph Waldo Emerson |
 |
|
|
pap
Starting Member
13 Posts |
Posted - 2004-01-22 : 10:17:50
|
| Here it is. Thank you for your help !CREATE PROCEDURE import_sfFPOxx0_manual @ponbr varchar(9) outputasdeclare @res int, @cursor int, @variable varchar(5), @status varchar(2), @status1 int, @i intdeclare @hub varchar(10), @col02 varchar(20), @cartoon varchar(20), @data varchar(8), @time varchar(8), @ship varchar(10)declare @Col001 varchar(9), @Col002 varchar(10), @Col003 varchar(13), @Col004 int, @Col005 varchar(40), @Col006 varchar(3), @Col007 varchar(3)declare @Col008 varchar(4), @Col009 varchar(4), @Col010 varchar(4), @Col011 varchar(4), @Col012 varchar(4), @Col013 varchar(4), @Col014 varchar(4)declare @Col015 varchar(4), @Col016 varchar(4), @Col017 varchar(4), @Col018 varchar(4), @Col019 varchar(4), @Col020 varchar(4), @Col021 varchar(4)declare @Col022 varchar(4), @Col023 varchar(4), @Col024 varchar(4), @Col025 varchar(4), @Col026 varchar(4), @Col027 varchar(4), @Col028 varchar(4)declare @Col029 varchar(4), @Col030 varchar(4), @Col031 varchar(4), @Col032 varchar(4), @Col033 numeric(5,1), @Col034 numeric(5,1), @Col035 numeric(5,1)declare @Col036 numeric(5,1), @Col037 numeric(5,1), @Col038 numeric(5,1), @Col039 numeric(5,1), @Col040 numeric(5,1), @Col041 numeric(5,1), @Col042 numeric(5,1)declare @Col043 numeric(5,1), @Col044 numeric(5,1), @Col045 numeric(5,1), @Col046 numeric(5,1), @Col047 numeric(5,1), @Col048 numeric(5,1), @Col049 numeric(5,1), @Col050 numeric(5,1)declare @Col051 numeric(5,1), @Col052 numeric(5,1), @Col053 numeric(5,1), @Col054 numeric(5,1), @Col055 numeric(5,1), @Col056 numeric(5,1), @Col057 numeric(5,1), @Col058 varchar(1)declare @FSZ001 varchar(10), @FSZ002 int, @FSZ003 int, @FSZ004 varchar(4), @FSZ005 intdeclare @size_SO varchar(2), @qtd_SO numeric(5,1), @valSO varchar(2), @counterSO intdeclare @size_BZ varchar(2), @qtd_BZ numeric(5,1), @valBC varchar(2), @counterBC intdeclare @size_PO varchar(7), @qtd_PO varchar(7)declare @soodn varchar(10), @soitm numeric(6), @sopda varchar(1), @sornb varchar(1), @sodde varchar(1), @soman varchar(13), @socat varchar(3)declare @gssto varchar(3)declare TPO cursor for select rtrim(ponbr), rtrim(poodn), RTRIM(poman), RTRIM(pomac), RTRIM(pomad), RTRIM(pounm), RTRIM(pocat), RTRIM(pos01), RTRIM(pos02), RTRIM(pos03), RTRIM(pos04),RTRIM(pos05), RTRIM(pos06), RTRIM(pos07), RTRIM(pos08), RTRIM(pos09), RTRIM(pos10), RTRIM(pos11), RTRIM(pos12), RTRIM(pos13), RTRIM(pos14), RTRIM(pos15), RTRIM(pos16), RTRIM(pos17), RTRIM(pos18), RTRIM(pos19), RTRIM(pos20), RTRIM(pos21), RTRIM(pos22), RTRIM(pos23), RTRIM(pos24), RTRIM(pos25), poq01, poq02, poq03, poq04, poq05, poq06, poq07,poq08, poq09, poq10, poq11, poq12, poq13, poq14, poq15, poq16, poq17, poq18, poq19, poq20, poq21, poq22, poq23, poq24, poq25, rtrim(poa_m) from sfTPOxx0 where ponbr=@ponbrdeclare FSZ cursor for select RTRIM(szodn), szitm, szsch, RTRIM(szsiz), szqty from sfFSZxx0/*Calcula a data no formato yyyymmdd *//*convert(varchar,year(getdate()))+convert(varchar,month(getdate()))+convert(varchar(2),day(getdate()))*/select @res=count(*) from sfTPOxx0if(@res!=0)begin set @res=0 open TPO fetch next from TPO into @Col001, @Col002, @Col003, @Col004, @Col005, @Col006, @Col007, @Col008, @Col009, @Col010, @Col011, @Col012, @Col013, @Col014, @Col015, @Col016, @Col017, @Col018, @Col019, @Col020, @Col021, @Col022, @Col023, @Col024, @Col025, @Col026, @Col027, @Col028, @Col029, @Col030, @Col031, @Col032, @Col033, @Col034, @Col035, @Col036, @Col037, @Col038, @Col039, @Col040, @Col041, @Col042, @Col043, @Col044, @Col045, @Col046, @Col047, @Col048, @Col049, @Col050, @Col051, @Col052, @Col053, @Col054, @Col055, @Col056, @Col057, @Col058 while @@fetch_status=0 begin print '??? Verifica se já existe FPO' select @res=count(*) from sfFPOxx0 where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 if (@res!=0) begin print 'Existe FPO' select @res=count(*) from sfFPOxx0 where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 and convert(numeric,posta) > 20 and convert(numeric,posta) < 70 print '??? Verifica se existe FPO com Status >20 e <70 ?' if (@res!=0) begin print 'Existe FPO com Status >20 e <70' /* Já exite este PO e está em produção, logo grava em backup */ INSERT INTO sfBPOxx0 values (@Col001, @Col002, @Col003, @Col004, @Col005, @Col006, @Col007, @Col008, @Col009, @Col010, @Col011, @Col012, @Col013, @Col014, @Col015, @Col016, @Col017, @Col018, @Col019, @Col020, @Col021, @Col022, @Col023, @Col024, @Col025, @Col026, @Col027, @Col028, @Col029, @Col030, @Col031, @Col032, @Col033, @Col034, @Col035, @Col036, @Col037, @Col038, @Col039, @Col040, @Col041, @Col042, @Col043, @Col044, @Col045, @Col046, @Col047, @Col048, @Col049, @Col050, @Col051, @Col052, @Col053, @Col054, @Col055, @Col056, @Col057, @Col058,'',Null,'', convert(datetime, getdate(),112),'') GOTO fim end else begin print 'NÃO existe FPO com Status >20 e <70 ?' UPDATE sfFPOxx0 set poman=@Col003, pomac=@Col004, pomad=@Col005, pounm=@Col006, pos01=@Col008, pos02=@Col009, pos03=@Col010, pos04=@Col011, pos05=@Col012, pos06=@Col013, pos07=@Col014, pos08=@Col015, pos09=@Col016, pos10=@Col017, pos11=@Col018, pos12=@Col019, pos13=@Col020, pos14=@Col021, pos15=@Col022, pos16=@Col023, pos17=@Col024, pos18=@Col025, pos19=@Col026, pos20=@Col027, pos21=@Col028, pos22=@Col029, pos23=@Col030, pos24=@Col031, pos25=@Col032, poq01=poq01+@Col033, poq02=poq02+@Col034,poq03=poq03+@Col035, poq04=poq04+@Col036, poq05=poq05+@Col037, poq06=poq06+@Col038, poq07=poq07+@Col039, poq08=poq08+@Col040, poq09=poq09+@Col041, poq10=poq10+@Col042, poq11=poq11+@Col043, poq12=poq12+@Col044, poq13=poq13+@Col045, poq14=poq14+@Col046, poq15=poq15+@Col047, poq16=poq16+@Col048, poq17=poq17+@Col049, poq18=poq18+@Col050, poq19=poq19+@Col051, poq20=poq20+@Col052, poq21=poq21+@Col053, poq22=poq22+@Col054, poq23=poq23+@Col055, poq24=poq24+@Col056, poq25=poq25+@Col057, poa_m=@Col058, poadt=convert(datetime, getdate(),112), poaus='SERVER' where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 set @status1=1executa_ciclo: ---Verifica se existe SO select @res=count(*) from sfFSOxx0 where soodn=@Col002 print '??? Verifica se existe FSO ?' if (@res=0) begin ---Não existe SO UPDATE sfFPOxx0 set posta='92' where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 GOTO fim end print 'Existe FSO' select @soitm=soitm from sfFSOxx0 where soodn=@Col002 select @sopda=sopda from sfFSOxx0 where soodn=@Col002 select @sornb=sornb from sfFSOxx0 where soodn=@Col002 select @sodde=sodde from sfFSOxx0 where soodn=@Col002 select @soman=soman from sfFSOxx0 where soodn=@Col002 select @socat=socat from sfFSOxx0 where soodn=@Col002 select @soodn=soodn from sfFSOxx0 where soodn=@Col002 ---------------------------------------------------------------------------------------------------------------- -- Verifica se a production order tem categoria diferente da sales order -- ---------------------------------------------------------------------------------------------------------------- if(@Col007!=@socat) begin set @Col002= '' GOTO actualiza_stock end ---------------------------------------------------------------------------------------------------------------- select @res=count(*) from sfFSOxx0 where soodn=@Col002 and sodde='N' if (@res=0) begin print '---------------- Não é uma ordem PDC---------------------' ---Verifica se existe BC select @res=count(*) from sfFBCxx0 where bcodn=@Col002 print '??? Verifica se existe FBC ?' if (@res=0) begin ---Não existe BC UPDATE sfFPOxx0 set posta='93' where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 GOTO fim end end print 'Existe FBC, ou é uma ordem PDC' ---Verifica se existe SL select @res=count(*) from sfFSLxx0 where substring(slbun, 1, 9)=@Col001 print '??? Verifica se existe FSL ?' if (@res=0) begin ---Não existe SL, logo actualiza o status do PO UPDATE sfFPOxx0 set posta='94' where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 GOTO fim end print 'Existe FSL' select @res=count(*) from sfFSOxx0 where soodn=@Col002 and sodde='N' if (@res=0) begin print '---------------- Não é uma ordem PDC---------------------' ---Verifica o status do SO select @res=count(*) from sfFSOxx0 where soodn=@Col002 and convert(numeric,sosta)=0 print '??? Verifica se existe FSO e status="00" ?' if (@res!=0) begin print' Existe o status do SO="00"' ---Verifica se as quantidades de BC correspondem as do SO select @soodn=soodn from sfFSOxx0 where soodn=@Col002 and convert(numeric,sosta)=0 select @soitm=soitm from sfFSOxx0 where soodn=@Col002 and convert(numeric,sosta)=0 open FSZ fetch next from FSZ into @FSZ001, @FSZ002, @FSZ003, @FSZ004, @FSZ005 while @@fetch_status=0 begin select @size_SO=szsiz from sffszxx0 where szodn=@Col002 and szitm=@soitm select @qtd_SO=szqty from sffszxx0 where szodn=@Col002 and szitm=@soitm ---Verifica no FBZ a quantidade para este tamanho e compara com a quantidade do FSZ select @qtd_BZ=sum(bzqty) from sfFBZxx0 where bzodn=@soodn and bzitm=@soitm and bzsiz=@size_SO print '??? Verifica se as quantidades de SZ!=BZ)' if (@qtd_BZ!= @qtd_SO) begin print 'Quantidades diferentes entre o FSO e o FBC' ---Actualiza status do FPO UPDATE sfFPOxx0 set posta='95' where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 deallocate FSZ GOTO fim end fetch next from FSZ into @FSZ001, @FSZ002, @FSZ003, @FSZ004, @FSZ005 end deallocate FSZ end end ---Permite rebuild print '??? Verifica se permite rebuild ?' select @res=count(*) from sfFSOxx0 where soodn=@Col002 and sornb='Y' if (@res!=0) begin print 'Permite rebuild' select @res=count(*) from sfFCAxx0 where caman=@Col003 print '??? Verifica se caman=poman' if (@res!=0) begin print 'caman=poman' select @res=count(*) from sfFSBxx0 where sbman=@Col003 print '??? Verifica se sbman=poman' if (@res=0) begin print 'sbman != poman' UPDATE sfFPOxx0 set posta='96' where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 GOTO fim end end else begin print 'caman != poman' UPDATE sfFPOxx0 set posta='97' where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 GOTO fim end end print 'verifica status' if (@status1=1) begin UPDATE sfFPOxx0 set posta='10' where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 end else begin UPDATE sfFPOxx0 set posta='00' where ponbr=@Col001 and poodn=@Col002 and pocat=@Col007 end set @status1=0 ACTUALIZA_STOCK: /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ /*------------------------------------------------------Actualiza stock control-online---------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ select @gssto=gssto from sffgsxx0 set @i=8 while @i<34 begin if (@i<10) begin set @size_PO='@Col00' + convert(varchar(1), @i) set @qtd_PO='@Col0' + convert(varchar(2), @i+25) end else begin set @size_PO='@Col0' + convert(varchar(2), @i) set @qtd_PO='@Col0' + convert(varchar(2), @i+25) end print '************************@size_PO=' + @size_po + ' @qtd_PO=' + @qtd_po if(@size_PO!=null and @size_PO!='') begin select @res=count(*) from sffscxx0 where scsto=@gssto and sccat=@Col007 and scman=@Col003 and scsiz=@size_PO and scodn=@Col002 print '??? Verifica se existe dados na Stock control on-line ?' if (@res!=0) begin print 'Já existem dados na stock control-online' UPDATE sffscxx0 set scqte=scqte+@qtd_PO, sclst=convert(datetime, getdate(),112), scusr='SERVER' where scsto=@gssto and sccat=@Col007 and scman=@Col003 and scsiz=@size_PO and scodn=@Col002 end else begin print 'Não existem dados na stock control-online' INSERT into sffscxx0 values(@gssto, @Col007, @Col003, @size_PO, @Col002, @qtd_PO, 0, @Col006, convert(datetime, getdate(),112), 'SERVER') end end if(@qtd_PO!=0) begin UPDATE sffgsxx0 set gscnt=gscnt+1 select @cursor=gscnt from sffgsxx0 INSERT into sffsmxx0 values(@cursor,@gssto, @Col007, @Col003, @size_PO, @Col002, @qtd_PO, @Col001, 'I', '', '', @Col006, 'A', convert(datetime, getdate(),112), 'SERVER') end set @i=@i+1 end ---------------------------------------------------------------------------------------------------------------- -- Verifica se a production order tem categoria igual à da sales order -- ---------------------------------------------------------------------------------------------------------------- if(@Col007=@socat) begin print 'Verifica se já existe este registo na tabela sfFNWxx0' select @socat=socat from sffsoxx0 where soodn=@soodn select @res=count(*) from sfFNWxx0 where nwodn=@soodn and nwcat=@socat and nwnbr=@Col001 if (@res!=0) begin print 'Já existe na FNW, por isso faz o update' UPDATE sffnwxx0 set nwflg=0, nwpda=@sopda, nwrnb=@sornb, nwdde=@sodde, nwman=@soman, nwa_m=@Col058, nwdtc=convert(datetime, getdate(),112) where nwodn=@soodn and nwitm=@soitm and nwnbr=@Col001 and nwcat=@socat end else begin INSERT into sffnwxx0 values(0, @soodn, @soitm, @Col001, @socat, @sopda, @sornb, @sodde, @soman, @Col058, convert(datetime, getdate(),112), Null) end end end end else begin print 'Não existe PO' --- Verifica se a sales order está completa select @res= sosta from sffsoxx0 where soodn=@soodn if(@res=20) begin INSERT into sfBPOxx0 values(@Col001, @Col002, @Col003, @Col004, @Col005, @Col006, @Col007, @Col008, @Col009, @Col010, @Col011, @Col012, @Col013, @Col014, @Col015, @Col016, @Col017, @Col018, @Col019, @Col020, @Col021, @Col022, @Col023, @Col024, @Col025, @Col026, @Col027, @Col028, @Col029, @Col030, @Col031, @Col032, @Col033, @Col034, @Col035, @Col036, @Col037, @Col038, @Col039, @Col040, @Col041, @Col042, @Col043, @Col044, @Col045, @Col046, @Col047, @Col048, @Col049, @Col050, @Col051, @Col052, @Col053, @Col054, @Col055, @Col056, @Col057, @Col058, '99', Null,'',convert(datetime, getdate(),112),'SERVER') end else begin ---------------------------------------------------------------------------------------------------------------- -- Verifica se a production order tem categoria diferente da sales order -- ---------------------------------------------------------------------------------------------------------------- if(@Col007!=@socat) begin INSERT into sfFPOxx0 values(@Col001, @Col002, @Col003, @Col004, @Col005, @Col006, @Col007, @Col008, @Col009, @Col010, @Col011, @Col012, @Col013, @Col014, @Col015, @Col016, @Col017, @Col018, @Col019, @Col020, @Col021, @Col022, @Col023, @Col024, @Col025, @Col026, @Col027, @Col028, @Col029, @Col030, @Col031, @Col032, @Col033, @Col034, @Col035, @Col036, @Col037, @Col038, @Col039, @Col040, @Col041, @Col042, @Col043, @Col044, @Col045, @Col046, @Col047, @Col048, @Col049, @Col050, @Col051, @Col052, @Col053, @Col054, @Col055, @Col056, @Col057, @Col058, '20', Null,'',convert(datetime, getdate(),112),'SERVER') end else begin INSERT into sfFPOxx0 values(@Col001, @Col002, @Col003, @Col004, @Col005, @Col006, @Col007, @Col008, @Col009, @Col010, @Col011, @Col012, @Col013, @Col014, @Col015, @Col016, @Col017, @Col018, @Col019, @Col020, @Col021, @Col022, @Col023, @Col024, @Col025, @Col026, @Col027, @Col028, @Col029, @Col030, @Col031, @Col032, @Col033, @Col034, @Col035, @Col036, @Col037, @Col038, @Col039, @Col040, @Col041, @Col042, @Col043, @Col044, @Col045, @Col046, @Col047, @Col048, @Col049, @Col050, @Col051, @Col052, @Col053, @Col054, @Col055, @Col056, @Col057, @Col058, '99', Null,'',convert(datetime, getdate(),112),'SERVER') end end print 'Executa ciclo outra vez' GOTO executa_ciclo endfim: DELETE from sfTPOxx0 where ponbr=@Col001 and poodn=@soodn and pocat=@Col007 fetch next from TPO into @Col001, @Col002, @Col003, @Col004, @Col005, @Col006, @Col007, @Col008, @Col009, @Col010, @Col011, @Col012, @Col013, @Col014, @Col015, @Col016, @Col017, @Col018, @Col019, @Col020, @Col021, @Col022, @Col023, @Col024, @Col025, @Col026, @Col027, @Col028, @Col029, @Col030, @Col031, @Col032, @Col033, @Col034, @Col035, @Col036, @Col037, @Col038, @Col039, @Col040, @Col041, @Col042, @Col043, @Col044, @Col045, @Col046, @Col047, @Col048, @Col049, @Col050, @Col051, @Col052, @Col053, @Col054, @Col055, @Col056, @Col057, @Col058 end select @variable=posta from sfFPOxx0 where ponbr=@ponbr print 'valor a returnar ' + @variable deallocate TPO deallocate FSZ RETURN @variableend |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-01-22 : 10:38:29
|
quote: Originally posted by Nazim Can you post the whole Sp Code .
Asked and answered....Cursor's...GOTO's...I feel dirty....How long does this transaction take?And you want to add dynamic sql on top of all this?What is this meant to do?You need to think set based...where's a good used sql salesman when you need one?Brett8-) |
 |
|
|
pap
Starting Member
13 Posts |
Posted - 2004-01-22 : 10:49:08
|
| Hello Brett,Thank you for your post but it wasn't so helpfull. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-01-22 : 11:18:11
|
My Apologies.How long does this transaction take?quote: I have a table with 25 fields of the same time "shoe size" (@Col008 to @Col032) and another 25 fields "quantity" (@Col033 to @Col057).
Not very noramlized...do the columns relate to each other?quote: What I need is to pick all the sizes and quantities and put them in another table "Stock control online" based in some conditions, Sales order, production order, etc.
Ok...what condition? Dynamic, or do you know what it's going to be...How about something like...INSERT INTO InventorytTableSELECT Col8, Col33FROM DenormalizedTableWHERE CriteriaUNION ALLSELECT Col9, Col34FROM DenormalizedTableWHERE CriteriaUNION ALLect...And again...sorryBrett8-) |
 |
|
|
|
|
|