SELECT series
APPEND BLANK
Replace doc_num WITH thisform.pageframe1.page1.docnum.value
Replace doc_type WITH thisform.pageframe1.page1.doctype.value
Replace doc_title WITH thisform.pageframe1.page1.doctitle.value
Replace doc_date WITH thisform.pageframe1.page1.docdate.value
Replace rec_date WITH thisform.pageframe1.page1.recdate.value
Replace pub_date WITH thisform.pageframe1.page1.pubdate.value
Replace sector WITH thisform.pageframe1.page1.sector.value
Replace cluster WITH thisform.pageframe1.page1.cluster.value
Replace reg_office WITH thisform.pageframe1.page1.region.value
Replace revision WITH thisform.pageframe1.page1.revision.value
Replace processedby WITH thisform.pageframe1.page1.procby.value
Replace reviewedby WITH thisform.pageframe1.page1.revby.value
Replace encodedby WITH thisform.pageframe1.page1.encby.value
MESSAGEBOX("New record successfully saved...",0+64,"Success")
This is my code in my add button, can you please help me what's next when i add same info it will trigger that details was already save. thanks.
As per Cetin Basoz the command should be
INSERT INTO series ( doc_num, doc_type, doc_title, doc_date, ;
rec_date, pub_date, sector, cluster, reg_office, revision, processedby, ;
reviewedby, encodedby) VALUES ;
(thisform.pageframe1.page1.docnum.value, ;
thisform.pageframe1.page1.doctype.value
thisform.pageframe1.page1.doctitle.value, ;
thisform.pageframe1.page1.docdate.value, ;
thisform.pageframe1.page1.recdate.value, ;
thisform.pageframe1.page1.pubdate.value, ;
thisform.pageframe1.page1.sector.value, ;
thisform.pageframe1.page1.cluster.value, ;
thisform.pageframe1.page1.region.value, ;
thisform.pageframe1.page1.revision.value, ;
thisform.pageframe1.page1.procby.value, ;
thisform.pageframe1.page1.revby.value, ;
thisform.pageframe1.page1.encby.value)
MESSAGEBOX("New record successfully saved...",0+64,"Success")
You can set primary key if table series is not free and connected to a database. if not then you can use command like
select series
locate for doc_num = thisform.pageframe1.page1.docnum.value or ;
doc_type = thisform.pageframe1.page1.doctype.value or ; ...... and so on
if found()
*here what to do if data found in table
endif
Related
Hi i have this simple trigger in oracle
CREATE OR REPLACE TRIGGER OCAP_CREATE_NCRB
BEFORE INSERT
ON OCAP_TBLOCAP
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
Defect_Type varchar2(16);
out_ varchar2(60);
BEGIN
Select A.DEFECT_TYPE into Defect_Type from OCAP_TBLDEFECT A where A.DEFECT_ID = :NEW.DEFECT;
IF Defect_Type = 'C' THEN
--Create NCRB
SP_INSERTTBLD1D2(23,LPAD(:NEW.ISSUED_BY,6,'0'),0,'0','0','035823','Draft',' ',' ',34,' ',0,461,0,0,'035105',trunc(sysdate),' ','A',Lpad(:NEW.ISSUED_BY,6,'0'),Lpad(:NEW.ISSUED_BY,6,'0'),trunc(sysdate),'A',:New.BATCH_NO,out_);
--insert action
SP_INSERTTBLFORMYACTION(Lpad(:NEW.ISSUED_BY,6,'0'), out_, Lpad(:NEW.ISSUED_BY,6,'0'), Lpad(:NEW.ISSUED_BY,'0'), 'Draft');
--Insert other affected Lots
insert into TBLD2LOT(NCRBSERIESNO,LOTNO,CREATEDBY,CREATEDDT,SEQNO) Select (out_), A.BATCH_NO,Lpad(:NEW.ISSUED_BY,6,'0'),sysdate,(TBLD2LOTSEQ.nextval) from OCAP_OTHERBATCH A where A.OCAP_ID = :NEW.OCAP_NO;
--add NCRBSeries no. to table OCAP_TBLOCAP for referencing
Update OCAP_TBLOCAP set NCRBSERIESNO = out_ where OCAP_NO = :NEW.OCAP_NO;
--Insert ocap history
END IF;
END Ocap_Create_NCRB;
/
the first 2 stored procedure is working fine but the insert query is not .
I try to excute the insert query manunaly by replacing the Out_ and the :new.Ocap_no it is working fine.
Is there something wrong in my query?
Hope someone help me out with this.
If it isn't working, then
from OCAP_OTHERBATCH A
where A.OCAP_ID = :NEW.OCAP_NO; --> this condition is never met
which means that no rows in OCAP_OTHERBATCH contain OCAP_ID value which is equal to :NEW.OCAP_NO.
Might be because of wrong letter case, CHAR datatype (right-padded with spaces up to column's full length), ... who knows. Without tables' description and sample data, it is difficult to guess.
SET STEP ON
Close Databases
Cd e:\ksv\Data
Use ohd IN 0 shared
Use cus IN 0 shared
SELECT * FROM cus inTO TABLE tempcus
ALTER table tempcus ADD COLUMN totalsold int
UPDATE tempcus SET totalsold=RECCOUNT(ohd.status='5') WHERE tempcus.customer=ohd.customer
SELECT * FROM tempcus INTO CURSOR cur
BROWSE
I have tried the above code and i am getting an error saying invalid table number , can someone help me with this.
RECCOUNT() function only gives you a record count for a workarea# or alias, e.g. RECCOUNT("ohd") will give total record count of ohd table.
You want something like:
SELECT COUNT(*) totalsold,cus.customer FROM cus JOIN ohd ON cus.customer=ohd.customer WHERE ohd.cstatus='5' INTO CURSOR cur GROUP BY cus.customer
BROWSE
In VFP, there is a REPLACE command which allows you to replace one or more fields based on whatever values, even if variable results from other queries... or fixed values. Ex: This works on whatever table is the current selected work area and whatever row it is on, unless you apply a scope clause (for condition).
Sample only for context of REPLACE command
use SomeOtherTable in 0 shared
select SomeOtherTable
replace SomeNumberField with 1.234, SomeStringField with 'Hello', etc...
or with condition (bogus, just to show you can apply to multiple rows.
replace SomeNumberField with SomeNumberField * 3 for StatusField = 'X'
Now, back to your original content. It appears you are trying to get a result temporary table with a total number of records from the OHD table where the status = 5. VFP allows you to run SQL-Select into temporary read-write "cursor" tables, that when closed will delete themselves, yet allows them to be modified (such as browse, or other direct manipulation such as with REPLACE command).
You can get the counts you are looking for with a left-join to a query result set. To help you see the pieces individually, I will do in steps so you can follow, then join into one final.
First, you want a count of all records in the OHD table with status = 5 per customer... the "o" and "c" are ALIAS references in the SQL queries below
SET STEP ON
Close Databases
Cd e:\ksv\Data
Use ohd IN 0 shared
Use cus IN 0 shared
select ;
o.customer, ;
count(*) NumberOfRecords ;
from ;
OHD o ;
where ;
o.status = '5' ;
group by ;
o.customer ;
into ;
cursor C_JustCountsPerCustomer READWRITE
The "into cursor" part above will create a workable table and give it the name of "C_JustCountsPerCustomer". I have always tried to use "C_" as a prefix to the table name for the sole purpose to know it is a temporary "CURSOR" result and not a real final table, but that is just my historical naming convention applied.
Now, if you did a browse of this result, you would see each customer's ID and how many with status = '5'. The resulting table "cursor" is like any other table opened and you could index as you need and browse, etc. But this only will give records that HAD status of '5'. But you could have more customers that never had a '5' status record.
Now, getting all your customers and their respective counts into one result table "cursor". I can take the above query and use within a SQL-Select via a LEFT-JOIN meaning, give me everything from the first table (left-side), regardless of a matching record found in the second table (right-side). But if there is a match to the right side, give me those values too.
select ;
c.*, ;
NVL( C_tmpResult.NumberOfRecords, 0000 ) as NumberOfRecords ;
from;
CUS c ;
LEFT JOIN ;
(select ;
o.customer, ;
count(*) NumberOfRecords ;
from ;
OHD o ;
where ;
o.status = '5' ;
group by ;
o.customer ) C_tmpResult ;
ON ;
c.customer = C_tmpResult.customer ;
into ;
cursor C_CusWithCounts readwrite
So, you can see the left-join uses the first query to get the counts, but the primary part of the query gets records from the customer table (alias "c") and is joined on the common customer id column. The "NVL()" states if there IS a value in the C_tmpResult table for the given customer, grab that. If not, assume a count of 0. Yes, I explicitly have 0000 to force a minimum final width to 4 digits in the result in case the first customer does not have any and it make the column only 1 digit wide.
Anyhow, at the end, you would have your result temporary table (cursor) with the customer information AND the count I think you are looking for. You should be able to do a browse and good to go.
I am importing a bunch of tables and have found data errors in some of them. These errors were introduced when the tables were created, years ago. I want to create a simple alert to notify me that I should manually check the table.
The following works, but it pops up the query results, which I don't want.
procedure checkForBadRecord
select * ;
from table_x ;
where field_x = 'thing used to determine it's bad'
if _tally > 0 then
messagebox("Check the table for errors!")
endif
endproc
Is there a way to check if a table has any rows that meet a condition without showing the actual rows?
I am using Visual FoxPro 8.
You could add "INTO ARRAY dummyCursorName" after there WHERE clause:
select * ;
from table_x ;
where field_x = 'thing used to determine it's bad' ;
INTO ARRAY dummyCursorName
_TALLY will still report the statistic and no annoying browse window to deal with.
To prevent the result to be shown just specify a target for the result. "into array" or "into cursor" would do.
According to your current code, you are not interested with the row(s) returned so you could simply get the count instead (you also had typo in the code). ie:
procedure checkForBadRecord
local array laBadCount[1]
select count(*) ;
from table_x ;
where field_x = "thing used to determine it's bad" ;
into array laBadCount
use in (select('table_x'))
if laBadCount[1] > 0 then
messagebox("Check the table for errors!")
endif
endproc
Probably instead of writing such a procedure you would want to write this procedure for a more generic use:
if checkForBadRecord('table_x', 'field_x', "thing used to determine it's bad")
messagebox("Check the table table_x for errors!")
endif
procedure checkForBadRecord(tcTableName, tcFieldToCheck, tuValueToCheck)
local array laBadCount[1]
select count(*) ;
from &tcTableName ;
where &tcFieldToCheck = m.tuValueToCheck ;
into array laBadCount
use in (select(m.tcTableName))
return laBadCount[1] > 0
endproc
Note: You could use "To Screen" as well to suppress the results and get the count via _Tally. ie:
procedure checkForBadRecord
set console OFF
select * ;
from table_x ;
where field_x = "thing used to determine it's bad" ;
to SCREEN
set console ON
use in (select('table_x'))
if _Tally > 0 then
messagebox("Check the table for errors!")
endif
endproc
Trying to create a stored procedure to delete all goods supplied by a distributor (x) as well as the references to the goods in my LINE table. My problem is there are 3 tables in total. So i need to delete the references from my LINE table first before deleting the items in GOODS table and I can't seem to figure it out correctly. I feel like I have it just need some help with a few syntax errors.
CREATE OR REPLACE PROCEDURE PRC_DeleteProd(x IN INTEGER)
AS
v_dcode distributor.d_code%type;
v_gcode goods.g_code%type;
v_gcode2 line.g_code%type;
CURSOR v_delete_cursor IS
SELECT goods.g_code, line.g_code , d_code
FROM distributor
JOIN goods ON (distributor.d_code = goods.g_code)
JOIN line ON (goods.g_code = line.g_code);
BEGIN
OPEN v_delete_cursor;
LOOP
FETCH v_delete_cursor INTO v_dcode, v_gcode, v_gcode2;
EXIT WHEN v_cus_cursor%NOTFOUND;
IF x = v_dcode THEN
DELETE FROM line WHERE v_gcode2 = x;
DELETE FROM goods WHERE v_gcode = x;
END IF;
END LOOP;
END;
/
You are not using correclty AND keyword
DELETE FROM line
WHERE v_gcode = x
AND
DELETE FROM goods
WHERE v_gcode = x;
should be
DELETE FROM line
WHERE v_gcode = x;
DELETE FROM goods
WHERE v_gcode = x;
I have a table in an Informix DB into which I want to insert multiple records at a time.
Data for one of the column should be unique & other column data may be the same for all the records I insert
Typical Insert Statement I use to insert one row :
insert into employee(empid, country, state) values(1, us, ca)
Now I want to pass different values for the 'empid' column, & data in rest of the columns can remain the same.
I am looking for something like looping empid & prompting user to enter the Start & End range of values for empid
When User enters Start Value as 1 & End Value as 100, the script should inset 100 records with empid's from 1 to 100
Please notice that you need to pass the start and end parameters as input arguments to your script:
#!/bin/sh
start=$1
end=$2
while [[ $start -le $end ]];
do
echo "insert into employee(empid, country, state) values(${start}, us, ca)"
start=`expr $start + 1`
done
This solution uses a stored procedure to wrap the insert statement in a loop. Probably more suitable for a large amount of data where performance is critical:
File InsertEmployee.sql
drop procedure InsertEmployee;
create procedure InsertEmployee(
p_start_empid like employee.empid,
p_end_empid like employee.empid,
p_country like employee.country,
p_state like employee.state
) returning char(255);
define i integer;
let i = p_start_empid;
while i <= p_end_empid
insert into employee (
empid,
country,
state
) values (
i,
p_country,
p_state
);
-- logging
-- if (DBINFO('sqlca.sqlerrd2') > 0) then
-- return "inserted empid=" || i || " country=" || p_country || " state=" || p_state with resume;
-- end if
let i = i + 1;
end while;
end procedure;
Load stored procedure into your database:
dbaccess mydatabasename InsertEmployee.sql
Call the stored procedure from the shell prompt:
echo 'execute procedure InsertEmployee(1,100,"us","ca");' | dbaccess mydatabasename
#!/bin/bash
declare -a namesArray=("name1","name2","name3")
inserts=""
for i in "{arr[#]}"
do
inserts+="INSERT INTO persons(id, name) VALUES (0,'$i');"
done
echo $inserts | dbaccess yourDataBase
That will insert 3 rows (i asume that your primary key is serial number, thats why is 0 in values field). In informix you cannot add multiple rows in the same insert, thats why i create an insert per row.
Informix: INSERT INTO table VALUES(0);
mySQL & SQL Server: INSERT INTO table VALUES(0),(1),(2); <- 3 rows