开发者

Executing a Table-Valued Function from a Stored Procedure with multiple Table-Valued Parameters being passed through?

开发者 https://www.devze.com 2023-01-17 23:20 出处:网络
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:

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开发者_StackOverflow 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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消