?
Solved

SQL Server 2005 Table-Valued Function Problem

Posted on 2007-11-26
4
Medium Priority
?
1,283 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 200 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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 …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

743 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