Operand type clash using user defined table type on SQL Server

wgxvkvu9  于 2023-10-15  发布在  SQL Server
关注(0)|答案(2)|浏览(141)

I have created a user defined type (with script to CREATE) given below:

CREATE TYPE [dbo].[udt_ProductID_SellingPrice] AS TABLE(
    [Product_Id] [int] NOT NULL,
    [Selling_Price] [money] NULL,
    PRIMARY KEY CLUSTERED 
(
    [Product_Id] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)

On the same database I have created a function that uses this type:

CREATE FUNCTION [dbo].[fn_NetGP_tbl](@InputSKUs udt_ProductID_SellingPrice READONLY, @Currency VARCHAR(3), @SiteName VARCHAR(255), @CatalogueSite VARCHAR(5), @SiteGroup VARCHAR(255))
RETURNS @Results TABLE
(
   Product_Id INT NOT NULL,
   Selling_Price MONEY NULL,
   Net_GP MONEY NULL
)
AS
BEGIN

I declare the Input SKUs as an in-memory table as follows and call the function as follows:

DECLARE @InputSKUs dbo.udt_ProductId_SellingPrice

INSERT INTO @InputSKUs VALUES(10352316, 500.00)

SELECT * FROM Sensu_Staging.dbo.fn_NetGP_tbl(@InputSKUs, 'GBP', 'Site_Name', 'SITE', 'Site_Group')

And get the (what I'm finding, fairly confusing) error message:

Operand type clash: udt_ProductID_SellingPrice is incompatible with udt_ProductID_SellingPrice

I can't really work out what I'm doing wrong - this has worked on previous databases, the only change I've made is adding a primary key/clustered index to ProductID_SellingPrice type and this seems to have thrown everything off.

Do I need to change the function? Or is it not possible to create an index on an in-memory custom table type? We were hoping to speed the process up by indexing that table but if it's not possible then I guess we'll have to find other ways.

Cheers,

Matt

y3bcpkx1

y3bcpkx11#

I had previously defined the user data type in a different database and had referenced it explicitly in the functions I was calling (the Net GP function in this case).

I had then changed the data type to include a primary key (both with the same name) and yet was still referencing the previous data type in the body of my function hence the confusing, but entirely accurate error message.

In short, make sure that you either change all references at all points in your function or, more sensibly, call your data types different things so as to be able to troubleshoot this better!

7bsow1i6

7bsow1i62#

In the stored procedure, there will be miss match with stored procedure name and user defined table or in the code level the naming of stored procedure and user defined table will be miss match. So this issue occurs.

Make sure that how the stored procedure name and user defined table will be matching.

相关问题