Simplify SQL by nesting - sql

Currently I have to copy and the following code into SQL developer and then copay & paste the results. How can I nest the following SQL to make life easier?
Select * from ir.application where application_number = 'xxxxxxxxxx';
Transpose xxxxxxxxxx with actual application number
Run this statement. Copy below SQL
--application ID = aaaaaaa
--aaaaaaa
Transpose --aaaaaaa with ID number by highlighting and hitting CTRL + C
Copy and paste the following SQL into SQL Developer
select * from document where app_id = aaaaaaa;
Copy the ID result into the SQL code transposing "aaaaaaa" with the result from above
Hit Run
Copy and paste the following SQL into SQL Developer
--document ID = ddddddd
--ddddddd
Transpose --ddddddd with ID number by highlighting and hitting CTRL + C
Copy and paste the following into SQL Developer
--aaaaaaa = application ID from 1st query
--ddddddd = document ID from 2nd query
update ir.application set application_number = null where id = aaaaaaa;
update ir.application set ttl_id = null where id = aaaaaaa;
update document set app_id = null where id = ddddddd;
update document set title_number = null where id = ddddddd;
update document set case_number = null where id = ddddddd;
commit;
Transpose as appropriate
Now Run each line in turn from "update..." down to "commit;" by highlighting and hitting Run after each semicolon

I think you've massively over-complicated your sql. For a start, you can update multiple columns in an update statement, so there's no need to have an update statement per column.
I think you just need two statements:
update ir.application
set application_number = null,
ttl_id = null
where application_number = :app_num;
update document
set app_id = null,
title_number = null,
case_number = null
where app_id = (select app_id from ir.application where application_number = :app_num);
commit;

Related

A query to find the value of a variable (MySQL) and saving it to another variable for a different query

I am having a table with 2 columns which map my internal_id with a external_id.
my_Id external_id
32312 23412
23234 12345
I am getting data from an API and I receive external_ids. I am trying to make a function using python to check if the external_ids I got from the api exist in my mapping table.
If exist, the corresponding my_id should be the output from this function.
I want to save it in a variable which will be inserted in another table together with other info I get from the API.
def get_my_id(external_system_id):
query = cursor.execute("""SELECT my_id FROM table
WHERE external_system_id=:external_system_id, external_system_id=external_system_id""")
print(query)
return(query)
My query is wrong but not sure how to amend it to take a variable and to be able to work with any value received
EDIT: I tried the solution from nbk, :
query = cursor.execute("""SELECT my_id FROM `tablename` WHERE customer_id=%s""",(customer_id,))
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s""", (external_system_id)' at line 2
You forgot to replace the place holder or switch to a prepared statement
Query is a number but it isn't the result, for that you must fetch the row if it exists and return them the array element.
For mysql.connector the code would look like this
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "password1",
database = "testdb"
)
mycursor = mydb.cursor(prepared=True)
my_id = 0
customer_id = 1
def get_my_id(customer_id):
result = mycursor.execute("SELECT my_id FROM `tablename` WHERE customer_id=%s",(customer_id,))
if result is None:
row = mycursor.fetchone()
return row[0]
else:
return 'Internal system_id not found'
my_id = get_my_id(customer_id)
print(my_id)
This goves back 1 as i added the table jaut now and add the column

Get last updated column from an update action in SQL Server

UPDATE sms.customerOtp
SET validateCounts = validateCounts + 1
WHERE customerID = '123'
After this update I need to run a select statement that should return the last updated value of validateCounts.
Is there any functions similar to SCOPE_IDENTITY to get the last updated value of a non-identity column?
You can use the OUTPUT clause:
UPDATE sms.customerOtp
OUTPUT validateCounts
SET validateCounts = validateCounts + 1
WHERE customerID = 123;
Normally, I would put the result into a table variable, but you can use it without a table variable.

Merge statement not inserting data

I have just started learning how to use a merge statement, but whenever I execute the procedure in which the merge is located, none of the data is inserted. Below is my code for the merge where I have created table customers_dw as a data warehouse & customers_ex_view is just a select statement for the columns I need.
MERGE INTO customers_dw dw
USING customers_ex_view ex
ON (dw.customer_id = ex.customer_id)
WHEN MATCHED THEN
UPDATE SET
customer_id = ex.customer_id,
first_name = ex.customer_firstname ,
last_name = ex.customer_lastname ,
shipping_address = ex.customer_address,
city = ex.customer_city,
state = ex.customer_state,
zip = ex.customer_zipcode,
phone = ex.customer_phonenum,
data_source = 'EX'
WHEN NOT MATCHED THEN
INSERT (customer_id,first_name,last_name,shipping_address,city,state,zip,phone)
VALUES (ex.customer_id,ex.customer_firstname,ex.customer_lastname,ex.customer_address,
ex.customer_city,ex.customer_state, ex.customer_zipcode,ex.customer_phonenum)
;
Your statement is not actually executing. In the UPDATE section you're setting the value of the CUSTOMER_ID field, but CUSTOMER_ID is used in the ON clause. This is an error, and it's causing an ORA-38104: Columns referenced in the ON Clause cannot be updated: "DW"."CUSTOMER_ID" exception to be thrown. Remove the line in the UPDATE section which says customer_id = ex.customer_id, and make sure you're properly trapping and reporting exceptions.
db<>fiddle here

Selecting rows based on a SQL Server "bit" column

In a SQL Server table, I have a BIT column and based its value, I need to update that table's other columns with some values. I tried this
UPDATE tablename SET Completed = GETDATE() WHERE CheckTaskAvailable = TRUE
but I get the error
Msg 207, Level 16, State 1, Server SQLDEV01, Line 1
Invalid column name 'TRUE'.
How to do this in a T-SQL query?
if you want to set as true try
Update table set columnName = 1 where ...
if you want to set as false try
Update table set columnName = 0 where ...
In addition to using the values 0 and 1, the T-SQL documentation for bit says that
The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.
so these work, too:
UPDATE tablename SET bitcolumn = 'TRUE' WHERE ...
UPDATE tablename SET othercolumn = 'something' WHERE bitcolumn = 'TRUE'
I had a need to do something similar where I was looking to update a field based on whether a record existed or not existed in another table so I used above code (thank you RezaRahmati) and added:
Update table set columnName = 1 where ID IN (SELECT ID FROM other table)
or for false
Update table set columnName = 0 where ID NOT IN (SELECT ID FROM other table)
I really enjoy Stack Overflow it has really helped me learn.

SQL Server 2008: how to give a value of a field in a column and return the column identity

I have a stored procedure that updates my table data.
update tbl_duty
set Name = #DName,
Necessity = #DNecessity,
Payment = #DPayment,
[Estimation Time] = #DEstimationTime,
Term = #DTerm,
[Description] = #DDescription
where
Id = "what can I put here"
but I don't now how get the ID of column to update because it generated itself (identity column)
Anyone can help me?
Do we have something like GETIDENTITY(column name)?
Do you want something like this?
update tbl_duty
set Name = #DName,
Necessity = #DNecessity,
Payment = #DPayment,
[Estimation Time] = #DEstimationTime,
Term = #DTerm,
[Description] = #DDescription
where id = (select max(id) from tbl_duty);
This seems very dangerous. Why wouldn't you insert the records with the right values in the first place?