检查并返回行(如果存在SQL Server表)

时间:2018-08-28 06:49:58

标签: sql sql-server tsql sql-function

我正在处理一个涉及SQL Server表的应用程序。我正在写一个查询,其中我需要检查一个表,如果该表存在,则需要从该表中选择记录,如果该表在该数据库中不存在,则需要从另一个表中选择。

我尝试使用UDF编写相同的内容,但未成功。我无法使用存储过程,因为我需要将输出连接到另一个表。

有什么办法吗?

CREATE FUNCTION dbo.udf_school_information 
    (@univ_id INT)
RETURNS @returntable TABLE
(
    univ_id INT,
    school_number INT,
    school_name NVARCHAR(255),
    school_address NVARCHAR(255),
    state NVARCHAR(150),
    district NVARCHAR(100),
    start_date datetime
)
AS
BEGIN
    DECLARE @tbl_exists BIT;

    SET @tbl_exists = ISNULL((SELECT 1 FROM INFORMATION_SCHEMA.TABLES 
                              WHERE Table_Name LIKE '%School'),0)

    IF @tbl_exists = 1      
    BEGIN
        SELECT 
            university_id, school_number, school_name, 
            school_address, school_state, district, school_started_date 
        FROM 
            [dbo].[tbl_school_info] 
        WHERE 
            id = @univ_id
    END
    ELSE
    BEGIN
        ---- My condition if school_ingo table does not exists 
        ---- will be querying another table.
    END

    RETURN;
END;
GO

以上抛出错误

  

第444级消息,状态2,过程udf_site_information,第24行   [Batch Start Line 15]选择函数中包含的语句   无法将数据返回给客户端。

3 个答案:

答案 0 :(得分:1)

您显然没有收到结果。那是因为您没有在结果表中插入数据集:

insert into @returntable
SELECT university_id,...

添加它,它应该可以工作。并更改此存在检查,因为每个人都认为问题出在这里:)

您应该可以简单地使用此表格

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Feeds]') AND type in (N'U'))
  --abc
ELSE
  -- xyz

与information_schema相同:

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME like 'feeds')
BEGIN
 print 'exists'
END

答案 1 :(得分:1)

OBJECT_ID()N'U'一起用作对象类型:

CREATE FUNCTION dbo.udf_school_information (@univ_id INT)
RETURNS @returntable TABLE
(
    univ_id INT,
    school_number INT,
    school_name NVARCHAR(255),
    school_address NVARCHAR(255),
    state NVARCHAR(150),
    district NVARCHAR(100),
    start_date datetime
)
AS
BEGIN
    DECLARE @tbl_exists BIT;

    IF OBJECT_ID(N'[dbo].[tbl_school_info]', N'U') IS NOT NULL SET @tbl_exists = 1
    ELSE SET @tbl_exists = 0;

    IF @tbl_exists = 1      
    BEGIN
       -- Statements
    ELSE
    BEGIN
        -- My condition if school_ingo table does not exists 
        -- will be querying another table.
    END
END;
GO

答案 2 :(得分:1)

这将起作用! 您需要以variable的形式返回查询中的数据。您需要定义所有要用作输出的变量。 我用我的虚拟表示例更改了您的示例。您可以根据需要使用 让我举一个例子

ALTER FUNCTION dbo.udf_school_information (@univ_id INT)
RETURNS @returntable TABLE
(
    univ_id INT

)
AS
BEGIN

    declare @tbl_exists bit
    Declare @Outputdata varchar(100)

    IF OBJECT_ID(N'[dbo].[tblprojectresource]', N'U') IS NOT NULL SET @tbl_exists = 1
    ELSE SET @tbl_exists = 0;

    IF @tbl_exists = 1      
    BEGIN
        select @Outputdata =ProjectResourceId from tblProjectResource Where ProjectResourceId =@univ_id
       -- Statements
    END
    ELSE
    BEGIN
        select @Outputdata =ProjectId from tblprojectmaster Where projectid =@univ_id
    END
END;
GO