Hi Shambhu,
If I am understanding this correctly, you want to use the following and convert it to a dataframe so you can write the result to a table.
%sql
MERGE into Transactions_Crdx AS TARGET
USING (SELECT H100.*,
GLOBAL_SITE_ID = ufnGetGlobslSiteIdForTxns("H100.H100_PICKUP_COMPANY","H100.H100_SUPPLY_COUNTRY",'CDX',"H100.HT_GLOPS_LOS_NO","H100.H100_EXTERNAL_LOS_NO")
FROM hist100_tempview AS H100) AS SOURCE
ON (TARGET.TxnGUID = SOURCE.TxnGUID)
WHEN MATCHED THEN
UPDATE
SET RecordMOdifiedDate = GETDATE()
DataFrames code (generated from ChatGPT and not tested as I do not have a dataset to work with):
from pyspark.sql import SparkSession
from pyspark.sql.functions import current_timestamp
# Assuming you have a SparkSession named spark
# Read the temp views into DataFrames
target_df = spark.table("Transactions_Crdx")
source_df = spark.sql("SELECT H100.*, ufnGetGlobslSiteIdForTxns(...) AS GLOBAL_SITE_ID FROM hist100_tempview AS H100")
# Define the condition for the merge
condition = target_df["TxnGUID"] == source_df["TxnGUID"]
# Perform the merge
merged_df = target_df \
.merge(source_df, condition, "WHEN MATCHED THEN UPDATE SET RecordMOdifiedDate = current_timestamp()")
# Display the result or write it back to a table
merged_df.show()
# If you want to write the result back to a table, you can use:
# merged_df.write.mode("overwrite").saveAsTable("Transactions_Crdx")
If this is helpful please accept answer.