Avatar of bfuchs
bfuchs
Flag for United States of America asked on

How to perform an update query from a UDF that receives parameters from multiple tables?

Hi Experts,

I have a table called Employeestbl

I would like to perform an update to a field of this table to all records that a UDF called EmployeesMissingDocs returns '1'.

Now my main issue is that this UDF expects values as parameters from different tables.

See example below

FUNCTION [dbo].[EmployeesMissingDocs] (
@EmployeeID int,
@Title varchar(50) = null,
@LicenseNumSignedYN bit = null,
@LicenseExpires DATETIME= null,
@BclsExpires DATETIME= null 
) RETURNS VARCHAR(2000)

Open in new window


Where EmployeeID and Title come from Employeestbl, License info come from a second table and Bcls from a third one..

How can I perform this in a single update statement?
DatabasesMicrosoft SQL ServerSQL

Avatar of undefined
Last Comment
bfuchs

8/22/2022 - Mon
PortletPaul

Use a CTE, contruct a query that uses the neede tables and returns the wanted rows. Then update the tsble using that CTE.
SOLUTION
Sharath S

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kent Olsen

The UDF really doesn't care where the parameters come from.  It's up to the calling operation to pass the correct parameter(s).

Can you clarify what you're trying to do?
bfuchs

ASKER
Hi Experts,

First thanks for replying..

Perhaps you can help me apply your suggestions given the details below:

A- the table/field I'm looking to update is EmployeesJcahoInfo.EmployeeID

(So in other words the SQL statement should be something like "Update EmployeesJcahoInfo.EmployeeID set MyField = False where EmployeesMissingDocs (Param1,Param2,Param3..) = '1')

B- the query joins needed for the UDF parameters are as follows

           
dbo.CoreMandatory RIGHT OUTER JOIN
                         dbo.Employeestbl ON dbo.CoreMandatory.EmployeeID = dbo.Employeestbl.ID LEFT OUTER JOIN
                         dbo.Facilitiestbl LEFT OUTER JOIN
                         dbo.FacilitiesDocumentsRequirements ON dbo.Facilitiestbl.ID = dbo.FacilitiesDocumentsRequirements.FacilityID RIGHT OUTER JOIN
                         dbo.TovInfo ON dbo.Facilitiestbl.Tov_ID1 = dbo.TovInfo.TovCode ON dbo.Employeestbl.ID = dbo.TovInfo.EmployeeID 

Open in new window


C- the list of parameters is quite long (I just listed in OQ the first few), I guess you dont need them, will figure this out already once I have an example.

D- let me know if you need more info.

Thanks,
Ben
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
bfuchs

ASKER
@KDO,
The UDF really doesn't care where the parameters come from.
Well if they would all come from one table (let say EmployeesJcahoInfo), I would have no problem executing it like this
Update EmployeesJcahoInfo.EmployeeID set MyField = False where EmployeesMissingDocs (EmployeesJcahoInfo.Field1,EmployeesJcahoInfo.Field2,EmployeesJcahoInfo.Field3..) = '1')

Open in new window

The whole issue is how to get those parameters supplied, and if I try to do this on a joined sql results then it becomes non updatable.

Thanks,
Ben
ASKER CERTIFIED SOLUTION
Kent Olsen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
bfuchs

ASKER
@Kdo,
write the update statement without relying on the UDF......WHERE
    condition(s)
Not sure what you mean by that, as the where condition is the point I need to use the UDF for?
SQL Server allows you to join tables in an UPDATE statement
Are you saying I can have all those outer joins shown above (ID: 41738208) embedded in this update statement?
Thanks,
Ben
Sharath S

Did you look into my post?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kent Olsen

>> Are you saying I can have all those outer joins shown above (ID: 41738208) embedded in this update statement?

Sure.  All modern database engines support SQL that allows you to join values in a sub-query, though vendors generally don't agree on the correct form so the statements don't always work across platforms without some changes.

Sharath posted an example earlier, too.

Kent
bfuchs

ASKER
@Sharath, @Kdo,

Testing it, will let you know..

Thanks,
Ben
bfuchs

ASKER
Worked, thank you guys!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23