?
Solved

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

Posted on 2016-08-01
11
Medium Priority
?
109 Views
Last Modified: 2016-08-02
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?
0
Comment
Question by:bfuchs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41738161
Use a CTE, contruct a query that uses the neede tables and returns the wanted rows. Then update the tsble using that CTE.
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 1000 total points
ID: 41738168
You can try CROSS APPLY. Replace <join condition> with your actual JOIN condition between tables.

SELECT *
  FROM Employeestbl e
  JOIN License l ON <join condition>
  JOIN third_table a ON <join condition>
 CROSS APPLY (SELECT dbo.EmployeesMissingDocs(e.EmployeeID, e.Title, l.LicenseNumSignedYN, l.LicenseExpires, a.BclsExpires) AS ReturnValue) t1

Open in new window

 UPDATE e
    SET your_column = t1.ReturnValue
   FROM Employeestbl e
  JOIN License l ON <join condition>
  JOIN third_table a ON <join condition>
 CROSS APPLY (SELECT dbo.EmployeesMissingDocs(e.EmployeeID, e.Title, l.LicenseNumSignedYN, l.LicenseExpires, a.BclsExpires) AS ReturnValue) t1

Open in new window

0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 41738194
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?
0
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
LVL 4

Author Comment

by:bfuchs
ID: 41738208
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
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41738223
@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
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 1000 total points
ID: 41738353
Hi Ben,

It may be best to write the update statement without relying on the UDF.  It may not code to be the shortest, but it will likely be the fastest and least complicated.

SQL Server allows you to join tables in an UPDATE statement like this:

UPDATE
    mytable
SET
    col1 = value1,
    col2 = value2 ...
FROM
    TableA A
    JOIN
    TableB B
      ON A.colx = B.colxy
  WHERE
    condition(s)

Based on what I've seen in this thread you should be able to fit your requirements (parameters) into this structure.


Kent
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41738468
@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
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41738531
Did you look into my post?
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 41738899
>> 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
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41739744
@Sharath, @Kdo,

Testing it, will let you know..

Thanks,
Ben
0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 41739901
Worked, thank you guys!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question