I'm trying to use a user-defined table in a stored procedure but while running my procedure im getting error as
"Operand type clash: varchar is incompatible with ..."
I have defined one user defined tabel as below:
CREATE TYPE [myUserTable] AS TABLE(
[Id] [nvarchar](10) NOT NULL)
Stored procedure:
Create PROCEDURE FETCHUSER
(@USERS [myUserTable] readonly,
@Firstname varchar(max))
select * from UserTable as ut, @USERS as mt where mt.Id = ut.Id
While executing, I have supplied input params as:
@USERS = 'Sample'
but getting error as
"Operand type clash: varchar is incompatible with ..."
Please help me out. Any suggestions, appreciated!
You need to pass a parameter value which exists. If you want to select
from myUserTable
, then pass that instead of sample
. Also, you need to pass a FirstName
of varchar(max)
type. You will need to make sure that whatever you pass is compatible with this type. Finally, if my.Id
and ut.Id
are of incompatible types, then you will need to convert one of them into the other's type for the sake of comparison.
It seems you are declaring a @USERS
variable as varchar
whereas the expected stored procedure parameter type is myUserTable
. To pass a table-valued parameter value, you need to declare a variable of the table type, insert rows into the table variable, and then call the proc.
Below is a refactored example based on the code in your question using modern ISO SQL join syntax. Not sure how @FirstName
is used in your actual code but I kept it here.
CREATE PROCEDURE FETCHUSER
@USERS [myUserTable] readonly
,@Firstname varchar(max)
AS
SELECT *
FROM UserTable AS ut
JOIN @USERS AS mt ON mt.Id = ut.Id;
GO
DECLARE @myUserTable [myUserTable];
INSERT INTO @myUserTable(ID) VALUES(N'Sample');
EXEC FETCHUSER @USERS = @myUserTable, @FirstName = '';
GO
I think the following code can help you
CREATE TYPE [dbo].[myUserTable] AS TABLE([Id] [nvarchar](10) NOT NULL);
CREATE PROCEDURE [dbo].FETCHUSER
@USERS [myUserTable] READONLY,
@Firstname VARCHAR(MAX)
AS
BEGIN
SELECT *
FROM UserTable AS ut
JOIN @USERS AS mt
ON mt.Id = ut.Id;
END
GO
DECLARE @users [myUserTable]
INSERT INTO @users
VALUES
(
'1'
) ,
(
'2'
)
EXEC FETCHUSER @users,
'yourFirstName'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.