0

I am working on an application that involves SQL Server tables. I am writing a query in which I need to check for a table and if the table exists I need to select records from that table and if the table does not exist in that database that I need to select from a different table.

I tried with writing the same with UDF but I am not successful. I am unable to use a stored procedure as I need to get the output to be joined to another table.

Is there any way to do this?

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

The above is throwing the error

Msg 444, Level 16, State 2, Procedure udf_site_information, Line 24 [Batch Start Line 15] Select statements included within a function cannot return data to a client.

4
  • 1
    why doesn't your approach work? do you get an error? Commented Aug 28, 2018 at 6:57
  • @linden.phoenix Updated the error. Commented Aug 28, 2018 at 7:26
  • Then use my answer. Commented Aug 28, 2018 at 7:33
  • 1
    yeah you need to INSERT INTO @returntable SELECT university_id, ... Commented Aug 28, 2018 at 7:35

3 Answers 3

1

You don't receive your result, apparently. That's because you don't insert your dataset in the result table:

insert into @returntable
SELECT university_id,...

Add this and it should work. And change this existence check as well because everyone thinks that the problem is there:)

You should be able to simply use this form

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

The same with the information_schema:

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME like 'feeds')
BEGIN
 print 'exists'
END
Sign up to request clarification or add additional context in comments.

Comments

1

Use OBJECT_ID() with N'U' as object type:

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

Comments

1

This will work! You need to return data in query as variable. You need to define all variables whichever u want as output . i have changed your example with my dummy tables example. You can use as per your requirements Let me give you one example

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

2 Comments

Your code won't probably work as you select into the @outputdata and still not fill @returntable
Just fill your @returntable at the end. this will work. Itried in local and it is working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.