0

I have a report writer that will allow me to query my db directly, however the query has to select from a SQL view. I have to develop a lot tracking solution but because of the logic involved, the only way I have been able to accomplish this so far is to hijack the SQL statement before it leaves the report writer and point it towards a function (instead of the view).

I need to develop a more user friendly way to accomplish this. My first though was to populate the view that the report writer sees with an item and lot number from one of my tables, call my function with the item and lot number and then somehow append the original view with usage and consumption transactions for that item/lot. Because of how the report writer is designed, the original view that returns just the item/lot must be the same object as the view that is eventually populated with the transactions.

Is there a way to use an alter view statement as part of a query? Is there a better way to accomplish this goal? I am a bit lost here.

1 Answer 1

0

Well not having the reputation to comment, and seeing that this is SQL Server could you do the following?:

SELECT st.*
, dbo.ufn_usage_and_consumption(st.item_number, st.lot_number)
from some_table st

Basically, you are calling both the view and FOR EACH ROW of the view, calling the SQL Server Function.

Please note that this is NOT OPTIMAL. You are essentially doing RBAR processing (Row by Agonizing Row) and calling the function for every row.

Really, I would see about creating a stored procedure, if your report writer supports that and pass parameters to call the query and pass results back.

I'm making the following assumption: 1) the data coming back from the function is a scalar (one value only), if it's not you can return it as a comma delimitted string

Don't know if that helps or not but good luck with your query!

2
  • The function is an inline table-valued function so the results are not scalar. I could maybe gather the data another way, however I need to drill up through the Work Order table recursively. For example, a RM may be used to produce a bulk formula which in turn may be shipped in bulk, or it may be used to produce another bulk which may be packaged and shipped. I need to see all "outputs" of the RM in the result set no matter how many levels deep. Because of this, I need to limit my data set to a single item/lot through parameters so that I don't sacrifice performance. Nov 13, 2013 at 22:11
  • Ok, could you somehow use a recursive CTE or common table expression? Come to think of it, you can still return and reference the call to tabled-valued function while still joining to your base table. Don't know if that helps?
    – ODU90DBA
    Nov 14, 2013 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.