Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2007
    Posts
    2

    Unanswered: Problem passing a variable into a table-valued function

    Hi,

    i am encountering a problem in a stored procedure when a pass a variable value into a table-valued function. The table-valued function is named getCurrentDriver and has 1 attribute: car-ID.

    The syntax is as follows:

    Code:
    select car.id, car.licenceNumber, car.brand, car.model,
    (select driverName from getCurrentDriver(car.id)) as driverName
    	from car
    When I try to compile I get following error on the line of the function:
    Code:
    Incorrect syntax near '.'
    The database version is SQL Server 2000 SP3.

    What am I doing wrong? Is there a workaround for this error?
    Last edited by TBP; 09-26-07 at 10:04.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Code:
    select car.id, car.licenceNumber, car.brand, car.model,
    dbo.getCurrentDriver(car.id) as driverName
    from car
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    [sniped]

    Code:
    select car.id, car.licenceNumber, car.brand, car.model,
    , dbo.getCurrentDriver(car.id) as driverName
    	from car
    ????
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  4. #4
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    The problem is that he is putting a table-valued function in the select clause. This is not allowed:

    Code:
    select	car.id,
    	car.licenceNumber,
    	car.brand,
    	car.model,
    	(select	driverName
    	from	getCurrentDriver(car.id)) as driverName
    from car
    TBP, you need to JOIN to the results of a table function as if it were a table or a view:
    Post the code for getCurrentDriver(), and we can help you out. Maybe you should be using a scalar function instead...
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  5. #5
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    dote. had'nt thought about that.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  6. #6
    Join Date
    Feb 2004
    Location
    Burbank & Santa Cruz de la Sierra
    Posts
    3,716
    also, part of the problem is that "license" is spelled wrong
    aka "Paul"
    Non est ei similis.

    I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

  7. #7
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Good eye. That would certainly not get past SQL Server 2005's Spell Checker.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  8. #8
    Join Date
    Sep 2007
    Posts
    2
    Quote Originally Posted by blindman
    The problem is that he is putting a table-valued function in the select clause. This is not allowed:

    Code:
    select	car.id,
    	car.licenceNumber,
    	car.brand,
    	car.model,
    	(select	driverName
    	from	getCurrentDriver(car.id)) as driverName
    from car
    TBP, you need to JOIN to the results of a table function as if it were a table or a view:
    Post the code for getCurrentDriver(), and we can help you out. Maybe you should be using a scalar function instead...
    Hi Blindman,
    are you sure you can't use table-defined function in a select clause?
    The syntax works when I do this:

    Code:
    declare @CarID int
    select @CarID = 123
    
    select	car.id,
    	car.licenseNumber,
    	car.brand,
    	car.model,
    	(select	driverName
    	from	getCurrentDriver(@CarID)) as driverName
    from car
    where car.id = @CarID
    The function getCurrentDriver is very straightforward and is tested successfully.
    It seems to be a bug in SQL Server 2000 but I'm not sure...

  9. #9
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Quote Originally Posted by blindman
    Good eye. That would certainly not get past SQL Server 2005's Spell Checker.
    It's certainly fun trying to write SQL for tables whose columns are called "identifer" and "sirname"
    George
    Home | Blog

  10. #10
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Quote Originally Posted by TBP
    Hi Blindman,
    are you sure you can't use table-defined function in a select clause?
    The syntax works when I do this:

    Code:
    declare @CarID int
    select @CarID = 123
    
    select	car.id,
    	car.licenseNumber,
    	car.brand,
    	car.model,
    	(select	driverName
    	from	getCurrentDriver(@CarID)) as driverName
    from car
    where car.id = @CarID
    The function getCurrentDriver is very straightforward and is tested successfully.
    It seems to be a bug in SQL Server 2000 but I'm not sure...
    What do you expect to happen if your table function returns more than one record or more than one column? And if it always returns one record and one column, then it is a scalar function and should be defined as such.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  11. #11
    Join Date
    Mar 2005
    Location
    Netherlands
    Posts
    280
    Hasn't this something to do with the missing schema name (owner in SQL 2000) when calling the function? Althought it beats me why the @CarID example seems to work.

    Code:
    select	car.id,
    	car.licenceNumber,
    	car.brand,
    	car.model,
    	(select	driverName
    	from dbo.getCurrentDriver(car.id)) as driverName
    from car

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •