问题
I am using the following cursor command to update a record on my table based on a join.
I have some similar code working fine in another part of my program, but without using LIKE logic on the join.
But when I execute the this code it throws an error.
Msg 402, Level 16, State 1, Line 12
The data types nvarchar and varchar are incompatible in the modulo operator.
Here is my code:
DECLARE @tablevalue NVARCHAR(MAX), @sql NVARCHAR(MAX);
DECLARE table_value_cursor CURSOR
FOR
SELECT DISTINCT [Tariff Lookup]
FROM [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325];
OPEN table_value_cursor
FETCH NEXT FROM table_value_cursor INTO @tablevalue
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql = N'
UPDATE [masked_2014-06-30-2014-06-01-customer325]
SET [masked_2014-06-30-2014-06-01-customer325].[Sell Price] =
ROUND ([Orbisrates].[dbo].[Orbis_Import_June2014].[Peakperminute] / 60 * [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[ChargedTimeSecs] + [Orbisrates].[dbo].[Orbis_Import_June2014].[Peakconnect], 4)
FROM [OrbisRates].[dbo].[Orbis_Import_June2014]
INNER JOIN [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325] on [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[To] LIKE [Orbis_Import_June2014].[Destination]+'%'
WHERE
[OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[tariff lookup] = '''+ @tablevalue +'''';
EXEC sp_executesql @sql;
FETCH NEXT FROM table_value_cursor INTO @tablevalue;
END
CLOSE table_value_cursor
DEALLOCATE table_value_cursor;
PS. I have tested the update section in the middle and that runs fine on its own.
Any help greatly appreciated.
回答1:
You are getting that error because of wrong use LIKE
operator as in below statement from your code
INNER JOIN [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325]
on LIKE [Orbis_Import_June2014].[Destination]+'%' <-- Here
Just put it inside quote. You should do something like below (a sample example)
select * from table1
where order_id like '%''' + order_no + '%'''
So in your case you should change it as below
SELECT @sql = N'
<....Rest Of Code ...>
on [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325].[To]
LIKE' + '%''' + '[Orbis_Import_June2014].[Destination]' + '%''' +
' WHERE [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325] ...'
EDIT:
You don't need a cursor for what you are trying to do. What you are essentially doing is
for each value of [Tariff Lookup]
under table [masked_2014-06-30-2014-06-01-customer325]
you are
trying to do the UPDATE
. The same can be achieved simply by a single query; by means of making the
selection of [Tariff Lookup]
a subquery like below. give it a try and see how it goes.
UPDATE masked325
SET masked325.[Sell Price] = ROUND (orbit14.[Peakperminute] / 60 * masked325.
[ChargedTimeSecs] + orbit14.[Peakconnect], 4)
FROM [OrbisRates].[dbo].[Orbis_Import_June2014] orbit14
INNER JOIN [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325] masked325
on orbit14.[Destination] = masked325.[To]
WHERE masked325.[tariff lookup]
IN (
SELECT DISTINCT [Tariff Lookup]
FROM [OrbisBilling].[dbo].[masked_2014-06-30-2014-06-01-customer325]
);
Sidenote: The WHERE
condition actually doesn't make much sense here.
来源:https://stackoverflow.com/questions/24596886/the-data-types-nvarchar-and-varchar-are-incompatible-in-the-modulo-operator