HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

Operand type clash: my first User Defined Type with list of Integers, how to test via SSMS

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
ssmswithusertypeclashoperandfirsttesthowvia

Problem

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

Solution

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

Code Snippets

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

Context

StackExchange Database Administrators Q#75770, answer score: 8

Revisions (0)

No revisions yet.