Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Transact-SQL子查询调用函数语法不正确_Sql_Sql Server_Tsql_Syntax Error - Fatal编程技术网

Transact-SQL子查询调用函数语法不正确

Transact-SQL子查询调用函数语法不正确,sql,sql-server,tsql,syntax-error,Sql,Sql Server,Tsql,Syntax Error,我在“.”附近得到了不正确的语法,似乎无法在以下代码中确定原因: select o.object_id, (select top 1 Zone from dbo.getzone(o.object_id)) as Zone from object as o getzone是一个表值函数,当我直接引用它时,或者如果我把一个特定的object_id放进去时,它可以很好地工作,但是每次我试图使它成为动态的,我都会得到语法错误 我遗漏了什么?修复您的别名 select o.object_id,

我在“.”附近得到了不正确的语法,似乎无法在以下代码中确定原因:

select 
o.object_id,
(select top 1 Zone from dbo.getzone(o.object_id)) as Zone from object as o
getzone是一个表值函数,当我直接引用它时,或者如果我把一个特定的object_id放进去时,它可以很好地工作,但是每次我试图使它成为动态的,我都会得到语法错误

我遗漏了什么?

修复您的别名

select  o.object_id, 
        (select top 1 Zone from dbo.getzone(o.object_id)) as Zone 
from object AS o
修复你的别名

select  o.object_id, 
        (select top 1 Zone from dbo.getzone(o.object_id)) as Zone 
from object AS o
修复你的别名

select  o.object_id, 
        (select top 1 Zone from dbo.getzone(o.object_id)) as Zone 
from object AS o
修复你的别名

select  o.object_id, 
        (select top 1 Zone from dbo.getzone(o.object_id)) as Zone 
from object AS o

你不能那样做。您需要一个只返回一个结果的标量版本。如果需要,它可以只是一个包装器脚本。大概是这样的:

CREATE FUNCTION [dbo].[getSingleZone](@object_id varchar(20))
RETURNS varchar(20)
AS
BEGIN
DECLARE @Zone varchar(20)
select @Zone = max(Zone) from dbo.getzone(@object_id)
return @Zone 
END

select 
o.object_id,
dbo.getSingleZone(o.object_id) as Zone from object o

我不知道您的数据类型,所以我猜。

您不能这样做。您需要一个只返回一个结果的标量版本。如果需要,它可以只是一个包装器脚本。大概是这样的:

CREATE FUNCTION [dbo].[getSingleZone](@object_id varchar(20))
RETURNS varchar(20)
AS
BEGIN
DECLARE @Zone varchar(20)
select @Zone = max(Zone) from dbo.getzone(@object_id)
return @Zone 
END

select 
o.object_id,
dbo.getSingleZone(o.object_id) as Zone from object o

我不知道您的数据类型,所以我猜。

您不能这样做。您需要一个只返回一个结果的标量版本。如果需要,它可以只是一个包装器脚本。大概是这样的:

CREATE FUNCTION [dbo].[getSingleZone](@object_id varchar(20))
RETURNS varchar(20)
AS
BEGIN
DECLARE @Zone varchar(20)
select @Zone = max(Zone) from dbo.getzone(@object_id)
return @Zone 
END

select 
o.object_id,
dbo.getSingleZone(o.object_id) as Zone from object o

我不知道您的数据类型,所以我猜。

您不能这样做。您需要一个只返回一个结果的标量版本。如果需要,它可以只是一个包装器脚本。大概是这样的:

CREATE FUNCTION [dbo].[getSingleZone](@object_id varchar(20))
RETURNS varchar(20)
AS
BEGIN
DECLARE @Zone varchar(20)
select @Zone = max(Zone) from dbo.getzone(@object_id)
return @Zone 
END

select 
o.object_id,
dbo.getSingleZone(o.object_id) as Zone from object o

我不知道您的数据类型,所以我猜。

也许我忽略了这个问题,但这似乎有效。使用内置函数的名称(
OBJECT\u ID
)作为列名可能没有帮助

下面的示例或代码

-- TVF without parameter.
create function dbo.GetZone()
  returns table as
  return
    select Id, Letter
      from
      ( values ( 1, 'Aleph' ), ( 2, 'Beth' ), ( 3, 'Gimmel' ) ) as Letters( Id, Letter );
go

-- TVF with parameter;
create function dbo.GetZone2( @Id as Int )
  returns table as
  return
    select Id, Letter
      from dbo.GetZone() where Id = @Id;
go

select * from dbo.GetZone();
select * from dbo.GetZone2( 2 );

-- Sample table and data.
declare @Objects as table ( Id Int Identity, Letter VarChar(16) );
insert into @Objects values ( 'Alpha' ), ( 'Beta' ), ( 'Gamma' );
select * from @Objects;

-- Correlated subquery.
select O.Id, O.Letter as [Greek],
  ( select top 1 Letter from dbo.GetZone( ) where Id = O.Id ) as [Hebrew]
  from @Objects as O;
select O.Id, O.Letter as [Greek],
  ( select top 1 Letter from dbo.GetZone2( O.Id ) ) as [Hebrew]
  from @Objects as O;

-- Houseclean.
drop function dbo.GetZone;
drop function dbo.GetZone2;

也许我忽略了这个问题,但这似乎有效。使用内置函数的名称(
OBJECT\u ID
)作为列名可能没有帮助

下面的示例或代码

-- TVF without parameter.
create function dbo.GetZone()
  returns table as
  return
    select Id, Letter
      from
      ( values ( 1, 'Aleph' ), ( 2, 'Beth' ), ( 3, 'Gimmel' ) ) as Letters( Id, Letter );
go

-- TVF with parameter;
create function dbo.GetZone2( @Id as Int )
  returns table as
  return
    select Id, Letter
      from dbo.GetZone() where Id = @Id;
go

select * from dbo.GetZone();
select * from dbo.GetZone2( 2 );

-- Sample table and data.
declare @Objects as table ( Id Int Identity, Letter VarChar(16) );
insert into @Objects values ( 'Alpha' ), ( 'Beta' ), ( 'Gamma' );
select * from @Objects;

-- Correlated subquery.
select O.Id, O.Letter as [Greek],
  ( select top 1 Letter from dbo.GetZone( ) where Id = O.Id ) as [Hebrew]
  from @Objects as O;
select O.Id, O.Letter as [Greek],
  ( select top 1 Letter from dbo.GetZone2( O.Id ) ) as [Hebrew]
  from @Objects as O;

-- Houseclean.
drop function dbo.GetZone;
drop function dbo.GetZone2;

也许我忽略了这个问题,但这似乎有效。使用内置函数的名称(
OBJECT\u ID
)作为列名可能没有帮助

下面的示例或代码

-- TVF without parameter.
create function dbo.GetZone()
  returns table as
  return
    select Id, Letter
      from
      ( values ( 1, 'Aleph' ), ( 2, 'Beth' ), ( 3, 'Gimmel' ) ) as Letters( Id, Letter );
go

-- TVF with parameter;
create function dbo.GetZone2( @Id as Int )
  returns table as
  return
    select Id, Letter
      from dbo.GetZone() where Id = @Id;
go

select * from dbo.GetZone();
select * from dbo.GetZone2( 2 );

-- Sample table and data.
declare @Objects as table ( Id Int Identity, Letter VarChar(16) );
insert into @Objects values ( 'Alpha' ), ( 'Beta' ), ( 'Gamma' );
select * from @Objects;

-- Correlated subquery.
select O.Id, O.Letter as [Greek],
  ( select top 1 Letter from dbo.GetZone( ) where Id = O.Id ) as [Hebrew]
  from @Objects as O;
select O.Id, O.Letter as [Greek],
  ( select top 1 Letter from dbo.GetZone2( O.Id ) ) as [Hebrew]
  from @Objects as O;

-- Houseclean.
drop function dbo.GetZone;
drop function dbo.GetZone2;

也许我忽略了这个问题,但这似乎有效。使用内置函数的名称(
OBJECT\u ID
)作为列名可能没有帮助

下面的示例或代码

-- TVF without parameter.
create function dbo.GetZone()
  returns table as
  return
    select Id, Letter
      from
      ( values ( 1, 'Aleph' ), ( 2, 'Beth' ), ( 3, 'Gimmel' ) ) as Letters( Id, Letter );
go

-- TVF with parameter;
create function dbo.GetZone2( @Id as Int )
  returns table as
  return
    select Id, Letter
      from dbo.GetZone() where Id = @Id;
go

select * from dbo.GetZone();
select * from dbo.GetZone2( 2 );

-- Sample table and data.
declare @Objects as table ( Id Int Identity, Letter VarChar(16) );
insert into @Objects values ( 'Alpha' ), ( 'Beta' ), ( 'Gamma' );
select * from @Objects;

-- Correlated subquery.
select O.Id, O.Letter as [Greek],
  ( select top 1 Letter from dbo.GetZone( ) where Id = O.Id ) as [Hebrew]
  from @Objects as O;
select O.Id, O.Letter as [Greek],
  ( select top 1 Letter from dbo.GetZone2( O.Id ) ) as [Hebrew]
  from @Objects as O;

-- Houseclean.
drop function dbo.GetZone;
drop function dbo.GetZone2;

你的别名错了。语句的最后一部分应该说“from object o”如果您是正确的,那么object的别名就不正确了。然而,改变这一点并不能帮助解决在这部分代码中引发的实际语法错误:dbo.getzone(o.object_id)我将尝试@Necreax所说的,但您也可以尝试在对象表和object_id列周围放上括号。这两个词都是保留词,它可能不喜欢。示例[object],而不仅仅是object。您的别名错误。语句的最后一部分应该说“from object o”如果您是正确的,那么object的别名就不正确了。然而,改变这一点并不能帮助解决在这部分代码中引发的实际语法错误:dbo.getzone(o.object_id)我将尝试@Necreax所说的,但您也可以尝试在对象表和object_id列周围放上括号。这两个词都是保留词,它可能不喜欢。示例[object],而不仅仅是object。您的别名错误。语句的最后一部分应该说“from object o”如果您是正确的,那么object的别名就不正确了。然而,改变这一点并不能帮助解决在这部分代码中引发的实际语法错误:dbo.getzone(o.object_id)我将尝试@Necreax所说的,但您也可以尝试在对象表和object_id列周围放上括号。这两个词都是保留词,它可能不喜欢。示例[object],而不仅仅是object。您的别名错误。语句的最后一部分应该说“from object o”如果您是正确的,那么object的别名就不正确了。然而,改变这一点并不能帮助解决在这部分代码中引发的实际语法错误:dbo.getzone(o.object_id)我将尝试@Necreax所说的,但您也可以尝试在对象表和object_id列周围放上括号。这两个词都是保留词,它可能不喜欢。示例[对象]而不仅仅是对象。感谢您的关注。尽管如此,别名似乎不是问题所在,因为我仍然在函数调用中收到语法错误调用。感谢您捕捉到这一点。尽管如此,别名似乎不是问题所在,因为我仍然在函数调用中收到语法错误调用。感谢您捕捉到这一点。尽管如此,别名似乎不是问题所在,因为我仍然在函数调用中收到语法错误调用。感谢您捕捉到这一点。尽管如此,别名似乎不是问题所在,因为我仍然在函数调用中收到语法错误调用。