question

mbgreen2 avatar image
mbgreen2 asked

Need help with SQL function?

1. Assume that the fields "ID" and "Manager" exist as Integers for each entry in the "Persons" table. Write a SQL Function that accepts an "ID" and returns the value from the "Manager" of the specified person. 2. Assume the table "Persons" has a field named "Location" that matches up with the "ID" field of the "Locations" table. Write the SQL to create a view that returns all fields for "Persons" with the correct value of the "Description" field from the "Locations" table.
sqlfunctions
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Squirrel avatar image Squirrel commented ·
I assume that this is your homework. We don't do homework here. Show us what you have tried so far and any questions or area that you d not understand, we will gladly help you. But we don't do your homework
2 Likes 2 ·
ThomasRushton avatar image ThomasRushton ♦♦ commented ·
As well as being taught (or at least having to do homework about) how to write functions, are you also being taught about the performance impact of using functions in your larger queries?
2 Likes 2 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
I'm with the others. I'd be happy to help you with you homework if you show your work and show where you're stuck. But if you think I'm going to do your homework for you, you're way off base.
2 Likes 2 ·
DenisT avatar image DenisT commented ·
We should not do you homework!
1 Like 1 ·

1 Answer

·
GPO avatar image
GPO answered
Assuming it's not homework (hahaha)... The "function" to which you refer is probably a "scalar function". Scalar functions are generally a poor choice in these situations because they are... well... procedural code, not set based code. It's my understanding that they get executed once for each row (slow). There's an interesting article here ( http://www.sqlservercentral.com/articles/T-SQL/91724/) that treats an in-line table valued function (which is set-based rather than procedural) as if it were a scalar function. It returns a "table" of one row, one column, so you get (some of) the performance benefits of set-based code, with the advantage of encapsulated logic. Having said all of that, you'd test performance in each case because the right solution might depend on some peculiarity of a particular situation.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.