Solved

SQL Server 2005 Table-Valued Function Problem

Posted on 2007-11-26
4
1,269 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 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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