Hey ALL,
I have the following table-valued functions that works well if I pass its arguments with literal numbers or from declared variables.
Is there any way to pass the values right from table and have its result return part of a select statment, as follows for e.g.
tbl1 contains a list in such a way :
type id
0 0
1 12
1 14
1 8
1 12 means the row is head and so the function would get name of a head with id 12. The error i am getting from sql server is err 4104
"The multi-part identifier "tbl1.type" could not be bound."
I thought of using case in this way:
But it is incorrect basically. Are there any alternatives?
I have the following table-valued functions that works well if I pass its arguments with literal numbers or from declared variables.
Code:
USE [db] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[returnowner] (@type smallint, @typeid int ) RETURNS @RESULTS TABLE (OwnerName varchar(500)) AS BEGIN DECLARE @Value varchar(100) if @type=0 --owned by company BEGIN SELECT @Value='Company' INSERT INTO @RESULTS (OwnerName) SELECT @Value END else if @type=1 --owned by a specific head BEGIN SELECT @Value=headname FROM tblHeads WHERE headid=@typeid INSERT INTO @RESULTS (OwnerName) SELECT @Value END RETURN END
Code:
SELECT * tbl1, returnowner.ownername FROM dbo.returnowner(tbl1.type,tbl1.id)
type id
0 0
1 12
1 14
1 8
1 12 means the row is head and so the function would get name of a head with id 12. The error i am getting from sql server is err 4104
"The multi-part identifier "tbl1.type" could not be bound."
I thought of using case in this way:
Code:
[case] case type WHEN 1 THEN SELECT headname FROM tblheads WHERE headid=@typeid [/case]
Comment