473,462 Members | 1,036 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

call user defined sql function within sql statement

120 100+
hi guys

I have a user defined sql function in my sql server db called TenPercentDiscount but not sure how to call it within sql statement?

I'm ideally looking for my function to be applied to a select * sql statement similar to my amateur attempt below

iv tried both
"SELECT * FROM TenPercentDiscount(RMT_2DayTours) WHERE ID IN (1,2,3)"
"SELECT TenPercentDiscount, * FROM RMT_2DayTours WHERE ID IN (1,2,3)"

but not having any joy? my function code is
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION TenPercentDiscount 
  2. (@InputVals int)
  3. RETURNS int
  4. AS
  5. BEGIN
  6. declare @convertedVal varchar(30)
  7. set @convertedVal =  Cast(@InputVals  * 0.9 as int)
  8. return  @convertedVal 
  9. end
please advise
omar
Nov 11 '10 #1
4 3655
code green
1,726 Expert 1GB
If RMT_2DayTours is a table then
Expand|Select|Wrap|Line Numbers
  1. SELECT TenPercentDiscount(field_from_RMT_2DayTours) As discounted
  2. FROM RMT_2DayTours 
  3. WHERE ID IN (1,2,3)"
Nov 11 '10 #2
omar999
120 100+
hi code green - yes RMT_2DayTours is a table. Is it bad practice to not include dbo. before a table name? if it is please let me know as I dont want to continue with bad habits.

I've tried your suggestion
Expand|Select|Wrap|Line Numbers
  1. SELECT TenPercentDiscount(Double_Redleaf_Price) as Double_Redleaf_Price 
  2. FROM RMT_2DayTours 
  3. WHERE ID IN (1,2,3)"
but im getting the following error
Microsoft OLE DB Provider for SQL Server error '80040e14'

'TenPercentDiscount' is not a recognized built-in function name.

please advise
Nov 11 '10 #3
ck9663
2,878 Expert 2GB
The "dbo" prefix means the object you are calling is owned by dbo. If you have a lot of object (tables, SP, function) in your database that's owned by different users, they may or may not have the same name. To make sure you are calling, reading or executing the right object, the owner name is usually included. As rule of thumb, yes, you should use the owner.

In calling the function, SQL Server, more often than not, expects the owner's name. So do a dbo.TenPercentDiscount(Double_Redleaf_Price). To be very sure, put the database name in front.

Happy Coding!!!

~~ CK
Nov 11 '10 #4
code green
1,726 Expert 1GB
Sorry about the missing dbo.
I should know better because my SQL Server will throw an error without the dbo
(similar to yours, strangely)
Nov 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Joel Thornton | last post by:
Is it possible to call a user-defined function without prefixing it with 'dbo.' within a SELECT clause somehow? Just curious; it's not a big issue but just a stylistic one for me. Thanks! ...
2
by: Steve D | last post by:
I've looked all over but can't find a solid answer. I've got a function that runs from a View and when the function runs the first time it is calculating a Temperature for a group of Formulas. ...
2
by: Mountain Man | last post by:
Hi, I'm trying to use window.open from inside a user defined function, and it's not working. A code example is shown below. Thanks for any help you can give. Mountain Man ============ ...
3
by: Gary Besta | last post by:
I am trying to add a simple case statement to a stored procedure or user defined function. However when I try and save the function/procedure I get 2 syntax errors. Running the query in query...
13
by: Maroon | last post by:
Hi, I want to write a standard java user defined function, like this example:- select db_fun("roll"), roll from student; **db_fun() will work for all tables of the all databse here db_fun is...
2
by: Mrs Howl | last post by:
I have a query that just reads one table and appends to an output table, one-for-one. No criteria. It's not a Total query (i.e. no group by). It normally run run in minutes, but gets horribly...
1
by: Mphoza | last post by:
Hi! Programmers,,,,,, Scenario I have a DataGrid with the delete button, If the user delete the row I'd like to show a confirmation dialog box with two buttons(Yes,No). If the user click yes...
4
by: Mahesh S | last post by:
Hi I want to write a user defined function which does the following. 1. Take a string value as input parameter 2. Should execute a sql statement: select col1, col2, col3, col4 from table_name...
2
by: Mark B | last post by:
I have been able to retrieve the scalar value of a SQL Server Stored Procedure as follows: Shared Function fGetGroupPerformanceStatistic(ByVal strGroup As String) As String Dim sqlConnection1...
1
by: qwedster | last post by:
Hola! In the following code snippet, I am creating User Defined Function (T-SQL) programmatically into database from C# Code: BTW, this code originally I wrote for creating StoredProcedure...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.