从存储过程执行表值函数并传递多个表值参数?
我有一个执行一些重复代码的存储过程,因此我决定将冗余代码放入表值函数中。我遇到的问题是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我有用。 (添加了一些
Go
批处理分隔符并从存储过程调用中删除了括号)如果这对您不起作用,您的数据库的兼容性级别是什么?
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?