![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello Folks, I encountered a problem with SQL server 2000 and UDFs. I have a scalar UDF and a table UDF where I would like the scalar UDF to provide the argument for the table UDF like in: SELECT * FROM transaction_t WHERE trxn_gu_id in ( select get_trxns_for_quarter(get_current_quarter( GetDate() ) ) ) 'get_current_quarter' returns an integer which is a GUID in a table containing business quarter definitions, like start date, end date. 'get_current_quarter' is a scalar UDF. 'get_trxns_for_quarter' will then get all transctions that fall into that quarter and return their GUID's in a table. 'get_trxns_for_quarter' is a table UDF. This doesn't seem to work at all. Regardless whether I provide the namespace (schema) calling the scalar UDF or not. Error message is just different. Both functions operate correctly invoked un-nested. The whole expression does work fine if I turn 'get_trxns_for_quarter' into a scalar UDF as well, e.g. by returning just one trxn_gu_id with e.g. MAX() in a scalar datatype. But of course that's no good to me. It also works fine if I select the result of 'get_current_quarter' into a variable and pass that variable into 'get_trxns_for_quarter'. But that's no good to me either since then I cannot use the whole thing embedded into other SELECT clauses. Both UDF's are non-deterministic but I couldnt see how that would have an impact anyway. Never mind the syntax on that example or anyhting, I tried all the obvious and not so obvious stuff and it really seems to come down to the fact that one UDF is scalar and the other one is not. However, I did not come across any type of information saying that this cannot be done. Have you any ideas? Any help would be greatly appreciated. Carsten |
#3
| |||
| |||
|
|
Hello Folks, I encountered a problem with SQL server 2000 and UDFs. I have a scalar UDF and a table UDF where I would like the scalar UDF to provide the argument for the table UDF like in: SELECT * FROM transaction_t WHERE trxn_gu_id in ( select get_trxns_for_quarter(get_current_quarter( GetDate() ) ) ) 'get_current_quarter' returns an integer which is a GUID in a table containing business quarter definitions, like start date, end date. 'get_current_quarter' is a scalar UDF. 'get_trxns_for_quarter' will then get all transctions that fall into that quarter and return their GUID's in a table. 'get_trxns_for_quarter' is a table UDF. |
#4
| |||
| |||
|
|
'get_current_quarter' returns an integer which is a GUID in a table containing business quarter definitions, like start date, end date. 'get_current_quarter' is a scalar UDF. |
#5
| |||
| |||
|
|
On Fri, 20 Jul 2007 09:39:44 -0700, Carsten wrote: Hello Folks, I encountered a problem with SQL server 2000 and UDFs. I have a scalar UDF and a table UDF where I would like the scalar UDF to provide the argument for the table UDF like in: SELECT * FROM transaction_t WHERE trxn_gu_id in ( select get_trxns_for_quarter(get_current_quarter( GetDate() ) ) ) 'get_current_quarter' returns an integer which is a GUID in a table containing business quarter definitions, like start date, end date. 'get_current_quarter' is a scalar UDF. 'get_trxns_for_quarter' will then get all transctions that fall into that quarter and return their GUID's in a table. 'get_trxns_for_quarter' is a table UDF. Hi Carsten, You need to select from a table-valued function. And you need to schema-qualify UDF's. |
#6
| |||
| |||
|
|
Hugo Kornelis wrote: On Fri, 20 Jul 2007 09:39:44 -0700, Carsten wrote: Hello Folks, I encountered a problem with SQL server 2000 and UDFs. I have a scalar UDF and a table UDF where I would like the scalar UDF to provide the argument for the table UDF like in: SELECT * FROM transaction_t WHERE trxn_gu_id in ( select get_trxns_for_quarter(get_current_quarter( GetDate() ) ) ) 'get_current_quarter' returns an integer which is a GUID in a table containing business quarter definitions, like start date, end date. 'get_current_quarter' is a scalar UDF. 'get_trxns_for_quarter' will then get all transctions that fall into that quarter and return their GUID's in a table. 'get_trxns_for_quarter' is a table UDF. Hi Carsten, You need to select from a table-valued function. And you need to schema-qualify UDF's. Hugo, you are right (as usual). The trick is in schemabinding the scalar UDF (and addressing the UDF with its 2-part name). Do you have any documentation or BOL reference that specifies that schemabinding is required? Because I don't understand why this would be relevant. |
#7
| |||
| |||
|
#8
| |||
| |||
|
|
Sorry about the confusion. I first tested on SQL Server 2000 and later (when Hugo gave a reply) I retested on 2005. In SQL Server 2005, this all works just fine. All you need to do is access the scalar UDF with its 2-part name. However, in SQL Server 2000 (the OP's platform), it doesn't work. Calling it with the 2-part name gives the error "Incorrect syntax near '.'", calling it with the 1-part name gives the error "Incorrect syntax near '('.". Schemabinding doesn't make a difference. |
#9
| |||
| |||
|
#10
| |||
| |||
|
|
I was hoping that once I found a suitbale way of replacing such stuff I could consolidate the codebase entirely. |
|
This has been very helpful nonetheless and I'm sure I'll find a way around this. Thank you. |
![]() |
| Thread Tools | |
| Display Modes | |
| |