Solved

SQL Server 2005 Table-Valued Function Problem

Posted on 2007-11-26
4
1,251 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
  • 2
  • 2
4 Comments
 
LVL 142

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 142

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now