如果 SQL 服务器 table 存在,则检查 Return 行
Check and Return rows if SQL Server table exists
我正在开发一个涉及 SQL 服务器 table 的应用程序。我正在编写一个查询,其中我需要检查 table,如果 table 存在,我需要 select 来自 table 的记录,如果 table 在我需要从另一个 table.
select 的那个数据库中不存在
我尝试用 UDF 写同样的东西,但我没有成功。我无法使用存储过程,因为我需要将输出连接到另一个 table.
有什么办法吗?
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
上面是抛出的错误
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.
显然,您没有收到结果。那是因为你没有在结果中插入你的数据集 table:
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
使用 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
这行得通!
您需要 return 查询中的数据为 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
我正在开发一个涉及 SQL 服务器 table 的应用程序。我正在编写一个查询,其中我需要检查 table,如果 table 存在,我需要 select 来自 table 的记录,如果 table 在我需要从另一个 table.
select 的那个数据库中不存在我尝试用 UDF 写同样的东西,但我没有成功。我无法使用存储过程,因为我需要将输出连接到另一个 table.
有什么办法吗?
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
上面是抛出的错误
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.
显然,您没有收到结果。那是因为你没有在结果中插入你的数据集 table:
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
使用 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
这行得通!
您需要 return 查询中的数据为 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