Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a UDF to derive a singular vaule 2

Status
Not open for further replies.

NuJoizey

MIS
Aug 16, 2006
450
US
ok, what am i doing wrong here? this sproc is choking on my UDF called ReturnCompanyID which eats a parameter called @CompanyLegalName - I get the error Invalid object name 'dbo.ReturnCompanyID'.

Code:
ALTER PROCEDURE [dbo].[usp_AddNewProducer]
	-- Add the parameters for the stored procedure here
	@ProducerName varchar(150),	
	@Website varchar (150),
	@Add1 varchar,
	@Add2 varchar,
	@City varchar,
	@StateID int,
	@CountryID int,
	@PostalCode varchar,
	@Phone varchar,
	@Fax varchar

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	INSERT INTO COMPANY
	  (CompanyTypeID, CompanyLegalName, CompanyRefName, CompanyWebsite)
	VALUES
	  ('2', @ProducerName, @ProducerName, @Website)

	INSERT INTO COMPANY_BRANCH
		(CompanyID, StateID, CountryID, Address1, Address2, City, PostalCode, Phone, Fax, Active)
	VALUES
		[COLOR=red](dbo.ReturnCompanyID(@ProducerName)[/color], @StateID, @CountryID, @Add1, @Add2, @City, @PostalCode, @Phone, @Fax, 'True')

END

RETURN

...and here is my UDF
Code:
ALTER FUNCTION [dbo].[COLOR=red][ReturnCompanyID][/color]
(
	-- Add the parameters for the function here
	@ProducerName varchar(255)
)
RETURNS TABLE
AS
	
RETURN (SELECT CompanyID FROM COMPANY WHERE CompanyLegalName=@ProducerName)
 
I think the problem is with the UDF.

You said the UDF derives a singular value. This is also known as a scalar. The problem is that your UDF Returns TABLE.

Instead, you should use something like this...

Code:
Create FUNCTION [dbo].[ReturnCompanyID]
(
    -- Add the parameters for the function here
    @ProducerName varchar(255)
)
RETURNS Int
AS
  Begin  
    RETURN (SELECT CompanyID FROM COMPANY WHERE CompanyLegalName=@ProducerName)
  End

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Hi gmmastros - i tried that. When i test the sql code it doesn't like it, the error is:

Msg 102, Level 15, State 31, Procedure ReturnCompanyID, Line 30
Incorrect syntax near 'RETURN'.


 
Is CompanyID in your UDF select

Code:
RETURN (SELECT CompanyID FROM COMPANY WHERE CompanyLegalName=@ProducerName)

an integer or some other standard datatype? If it is, you'll want to return that datatype from your UDF, rather than a table.

 
ok, here is my UDF:

Code:
ALTER FUNCTION [dbo].[ReturnCompanyID]
(
	-- Add the parameters for the function here
	--<@Param1, sysname, @p1> <Data_Type_For_Param1, , int>
	@ProducerName varchar(255)
)
RETURNS bigint
AS
BEGIN
	
	
RETURN (SELECT CompanyID FROM COMPANY WHERE CompanyLegalName=@ProducerName)

END

and when i test it, it seems to be fine, but when I run it, I now get:

Msg 2010, Level 16, State 1, Procedure ReturnCompanyID, Line 30
Cannot perform alter on 'dbo.ReturnCompanyID' because it is an incompatible object type.


I checked the table, and CompanyID is of type bigint. so now what?


 
Have you tried dropping it, and then creating it?

[tt][blue]Drop Function dbo.ReturnCompanyId[/blue][/tt]

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top