I found this wonderful code here

    CREATE TYPE [dbo].[StringList] AS TABLE(
    [Item] [NVARCHAR](MAX) NULL
);

    GO 
    CREATE PROCEDURE [dbo].[sp_UseStringList]
        @list StringList READONLY
    AS
    BEGIN
        -- Just return the items we passed in
        SELECT l.Item FROM @list l;
    END

    GO

but I dont know how to test it via SSMS please

declare @list   StringList  = '1,2,3,4,5'
exec sp_UseStringList @list

gives the error:

Operand type clash: varchar is incompatible with StringList

How can I test the list '1,2,3,4,5' please

有帮助吗?

解决方案

As you have declared dbo.StringList to be a User-Defined Table Type, you have to utilise it as you would a table.

So instead of assigning '1,2,3,4,5' as a string, you must INSERT this value into the table.

There's a pretty decent example in BOL, but I'll create one based on your example above.

CREATE TYPE [dbo].[StringList] AS TABLE(
  [Item] [NVARCHAR](MAX) NULL
);

GO 
CREATE PROCEDURE [dbo].[sp_UseStringList]
    @list StringList READONLY
AS
BEGIN
    -- Just return the items we passed in
    SELECT l.Item FROM @list l;
END

GO


DECLARE @StringList StringList

INSERT Into @StringList values('1,2,3,4,5') 

EXEC sp_UseStringList @StringList
许可以下: CC-BY-SA归因
不隶属于 dba.stackexchange
scroll top