从存储过程执行表值函数并传递多个表值参数?

发布于 2024-09-25 12:52:48 字数 830 浏览 8 评论 0原文

我有一个执行一些重复代码的存储过程,因此我决定将冗余代码放入表值函数中。我遇到的问题是:

Msg 137, Level 16, State 1, Procedure Search, Line 98
Must declare the scalar variable "@myTVP".

我正在使用的 SQL 代码的一个简单示例是:

CREATE TYPE [dbo].textTable_type AS TABLE(
[text] [nvarchar] (36)
)

CREATE FUNCTION dbo.Search_fn
(@myTVP AS textTable_type READONLY)
RETURNS TABLE
AS
RETURN
(   SELECT * from @myTVP    )
GO

CREATE PROCEDURE [dbo].[Search]
@myTVP AS textTable_type READONLY
AS
BEGIN

SELECT * FROM dbo.Search_fn(@myTVP)

END
GO

DECLARE @TVP as textTable_type
INSERT INTO @TVP VALUES ('abc')
INSERT INTO @TVP VALUES ('123')
exec dbo.Search(@myTVP = @TVP)
GO

DROP FUNCTION Search_fn
DROP PROCEDURE Search

如果任何人都可以提供任何见解,那就太好了!

(如果您尝试运行此示例,则会出现一些额外的错误,但它们源自所包含的错误。问题是因为无法创建存储过程 Search。

谢谢。

I've got a stored procedure that executes some repetitive code, so I decided to make the redundant code into a table-valued function. The problem that I'm encountering is:

Msg 137, Level 16, State 1, Procedure Search, Line 98
Must declare the scalar variable "@myTVP".

A simple example of the SQL code that I'm using is:

CREATE TYPE [dbo].textTable_type AS TABLE(
[text] [nvarchar] (36)
)

CREATE FUNCTION dbo.Search_fn
(@myTVP AS textTable_type READONLY)
RETURNS TABLE
AS
RETURN
(   SELECT * from @myTVP    )
GO

CREATE PROCEDURE [dbo].[Search]
@myTVP AS textTable_type READONLY
AS
BEGIN

SELECT * FROM dbo.Search_fn(@myTVP)

END
GO

DECLARE @TVP as textTable_type
INSERT INTO @TVP VALUES ('abc')
INSERT INTO @TVP VALUES ('123')
exec dbo.Search(@myTVP = @TVP)
GO

DROP FUNCTION Search_fn
DROP PROCEDURE Search

If anyone can provide any insight, that would be wonderful!

(There are a couple extra errors if you try to run this example, but they originate from the error included. The problem is because the stored procedure Search cannot be created.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-10-02 12:52:48

这对我有用。 (添加了一些 Go 批处理分隔符并从存储过程调用中删除了括号)

如果这对您不起作用,您的数据库的兼容性级别是什么?

CREATE TYPE [dbo].textTable_type AS TABLE(
[text] [nvarchar] (36)
)
GO

CREATE FUNCTION dbo.Search_fn
(@myTVP AS textTable_type READONLY)
RETURNS TABLE
AS
RETURN
(   SELECT * from @myTVP    )
GO


CREATE PROCEDURE [dbo].[Search]
@myTVP AS textTable_type READONLY
AS
BEGIN

SELECT * FROM dbo.Search_fn(@myTVP)

END
GO

DECLARE @TVP as textTable_type
INSERT INTO @TVP VALUES ('abc')
INSERT INTO @TVP VALUES ('123')
exec dbo.Search @myTVP = @TVP 

GO

DROP FUNCTION [dbo].Search_fn
DROP PROCEDURE [dbo].Search
DROP TYPE [dbo].textTable_type

This works for me. (Added some Go batch delimiters and removed brackets from the stored procedure call)

If this doesn't work for you what is the compatibility level of your database?

CREATE TYPE [dbo].textTable_type AS TABLE(
[text] [nvarchar] (36)
)
GO

CREATE FUNCTION dbo.Search_fn
(@myTVP AS textTable_type READONLY)
RETURNS TABLE
AS
RETURN
(   SELECT * from @myTVP    )
GO


CREATE PROCEDURE [dbo].[Search]
@myTVP AS textTable_type READONLY
AS
BEGIN

SELECT * FROM dbo.Search_fn(@myTVP)

END
GO

DECLARE @TVP as textTable_type
INSERT INTO @TVP VALUES ('abc')
INSERT INTO @TVP VALUES ('123')
exec dbo.Search @myTVP = @TVP 

GO

DROP FUNCTION [dbo].Search_fn
DROP PROCEDURE [dbo].Search
DROP TYPE [dbo].textTable_type
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文