Solved

SQL Server 2005 Table-Valued Function Problem

Posted on 2007-11-26
4
1,279 Views
Last Modified: 2011-10-03
I have created the below function to get a full description for a field from a table in my database.  However, when using my function, I get an error message that says:

"Cannot find either collumn "dbo" or the user-defined function or aggregate "dbo.getCode", or the name is ambiguous."

I don't know a whole lot about these functions, so I may be doing something wrong, but below is what I am executing in my query:

SELECT     dbo.getCode(grade, 'grades') AS Code
FROM         dwellings


And this is my function:

USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[getCode]
(      
@Code char(20),
@CodeType char(20)
)
RETURNS TABLE
AS
RETURN
(
SELECT tbl_element_desc FROM codes_table WHERE tbl_type_code = '@CodeType' AND tbl_element = '@Code'
)



Thanks in advance for the help!
0
Comment
Question by:jayh99
[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
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20350340
you created a table function, and not value function. please try below code.
note: you also had '@parameter' instead of @parameter




USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[getCode] 
(      
@Code char(20),
@CodeType char(20)
)
RETURNS VARCHAR(200)
AS
RETURN 
(
SELECT TOP 1 tbl_element_desc FROM codes_table WHERE tbl_type_code = @CodeType AND tbl_element = @Code
)

Open in new window

0
 
LVL 1

Author Comment

by:jayh99
ID: 20350381
Thanks for the quick response.  I am getting the following error now:

Msg 102, Level 15, State 31, Procedure getCode, Line 8
Incorrect syntax near 'RETURN'.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 20350392
sorry, we need to add BEGIN ... END

USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[getCode] 
(      
@Code char(20),
@CodeType char(20)
)
RETURNS VARCHAR(200)
AS
BEGIN
RETURN 
(
SELECT TOP 1 tbl_element_desc FROM codes_table WHERE tbl_type_code = @CodeType AND tbl_element = @Code
)
END

Open in new window

0
 
LVL 1

Author Closing Comment

by:jayh99
ID: 31410985
Thanks a lot for your help.  That worked perfectly.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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