Access - Check if a value exists in another table after insert - sql

I have a table with the field "Name" among the others. When I insert a new value in that field, I need a check to see if it exists in another table (same field, "Name"), and if it doesn't, I need that it creates a new row in this table with the value inserted
Example:
First Table Second Table
Name Cost Name Cost
John 1000€ John 1200€
John 200€ James 2000€
James 2000€
If I try to insert, for example, "Mark" in the first table, since it isn't in the second table, it adds a new row in the second, so the result is
Second Table
Name Cost
John 1200€
James 2000€
Mark 0€
The check is on the field "name"
Here is the actual database, but it is in italian. The Form "Commesse" has the button "aggiungi cliente" where I write a new name and then save it with "Salva cliente". I need that the other three tables and relative forms to have updated the "Nome_Cliente" field.

This is easy to do with an After Insert data macro on the first table:
For more information on data macros, see:
Create a data macro

Related

replace text or value in same column in oracle sql

how to replace value or text in same column in oracle sql?
If I add rows in column actual_year for plant 1234, it is equal to value in the row for plant 1235. Similarly if I add rows in column actual_year for plant 9876, it is equal to value in the row for plant 5432. Now, I want to replace plant 1234 by 1235 and 9876 by 5432. There are many cases like these in my table. Apologies for adding image instead of a table. But I dont know how to add a table here.

SSRS - Parameter based on reference table

I have multiple datasets with the WHERE clause = #customernumber.
I have a customer number data set that contains a field with a unique id and a field that contains multiple ids.
I.e.
Customer Number | Old Customer Numbers
123 123
123 34324
123 4363
123 124214
345 345
345 436346
345 234532
678 678
I want to be able to search in the parameter box for the old customer number but use the customer number in querying my datasets.
My datasets only contain the customer number, not the old customer numbers.
While creating the parameter you can select the value and the label of the dropdown list.
In the Available Values tab select Get values from a query, for dataset just choose the dataset that query the table with new and old codes. In Value field select the new code and in the Label field select the old code.
UPDATE:
Create a parameter called OldCode. Set the data type to text or integer based on old customer code number. Don't use default or available values for this parameter.
Create a dataset that use the #OldCode parameter to return the customer number. Use something like this:
select
Customer_Number
from CustomerTable
where Old_Customer_Number = #OldCode
Create a parameter called CustomerNumber. Set Hidden property and select the data type based on the Customer_Number column. In Default Values tab select the dataset created previously and the Customer_Number column as Value field.
After that you can use #CustomerNumber in all your datasets. When user input a old code the #CustomerNumber will be populated with the related Customer_Number value.
Let me know if this helps.

Modifying a SQL table to insert data from an existing row into the previous row

I have a SQL table called RawData with the following six columns (all type VARCHAR):
Name StreetAddress State Phone Website Email
I import new data from a CSV file into this table on a regular basis and 99% of it is formatted correctly. For 1% of the data, for reasons that I understand but are not important for this question, the data is imported such that all of the first five columns (Name, StreetAddress, State, Phone, Website) have data that is formatted correctly but the sixth column Email is blank. Furthermore, the email is imported into its own row immediately below this "problem" row but the email is placed in the Name column. These situations are readily identified begins the email address is always inserted in the Name column with the mailto: prefix .
What I would like to do is devise a query to take the incorrectly placed email address from the Name, strip the mailto: prefix and then place it into the Email column in the previous row. I would then like to delete the row with the 'mailto:' email in the `Name' column.
I've done a bit of research and believe that I need to use a cursor to copy the data from the row below the correct row and insert it into the Email column but having never used cursors before, I am struggling to come up with anything. Any help would be appreciated.
Edit: For the purposes of brevity, I omitted that the table does contain an identity column ID.
In SQL Server, there is no concept of "previous" row. You are going to need to add an identity column to the table so you can identify these situations. Fortunately, you can insert your data into a view, so the identity is automatically incremented in the load order.
Once you have the id, you can use an update:
update nd
set nd.email = stuff(ndnext.name, 1, 7, '')
from newdata nd join
newdata ndnext
on nd.id = ndnext.id - 1
where ndnext.name like 'mailto:%';
Once you have verified that this is correct, you probably want to do:
delete nd from nd
where nd.name like 'mailto:%';

How to insert non duplicate names in SQL

I want to add a new user in my table users
users
------
ID NAME
1 abc
2 cde
how could I use the syntax 'no exists' in this case if for example someone by mistake insert another user with the name 'abc' I dont want to enter with the same name user again.
You just need to make the column as UNIQUE
ALTER IGNORE TABLE users ADD UNIQUE (NAME);
This will prevent to have duplicates in the column NAME

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.