Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2210
  • Last Modified:

Msg 402 Error in Code SQL in Sql Server 2005 Enviroment

Am getting the following error message and do not understand why.
Msg 402, Level 16, State 1, Line 21 - The data types varchar and varchar are incompatible in the modulo operator.  I have validated part of the code as indicated below.  I am new to this enviroment so any explanation as to why the code is worng is greatly apprecieated

Code being used -
declare @tabtemp table (i int identity, sl_effected_user_id varchar(20)null)
declare @rows int
declare @var1 as varchar(20)
declare @query as varchar(8000)

insert into @tabtemp
select sl_effected_user_id
from table1

----------------------------I have validated to here and all is good

Select @rows = @@ROWCOUNT
While @rows >0

Begin

Select @var1 = sl_effected_user_id
from @tabtemp WHERE i = @Rows

set @query = '
insert into table3

select sl_id, sys_creation_date, operator_id, application_id, sl_app_id,
sl_effected_environment, sl_effected_profile_id, sl_effected_user_id,
sl_location, sl_event_description
from table2
where sl_effected_user_id like ('% +@var1+ ')
'
print @query

SET @Rows = @Rows -1
exec (@query)
END
0
frogman22
Asked:
frogman22
2 Solutions
 
chapmandewCommented:
gin

Select @var1 = sl_effected_user_id
from @tabtemp WHERE i = @Rows

set @query = '
insert into table3

select sl_id, sys_creation_date, operator_id, application_id, sl_app_id,
sl_effected_environment, sl_effected_profile_id, sl_effected_user_id,
sl_location, sl_event_description
from table2
where sl_effected_user_id like (%' +@var1+ ')'
print @query

SET @Rows = @Rows -1
exec (@query)
END
Tags:
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
set @query = '
insert into table3

select sl_id, sys_creation_date, operator_id, application_id, sl_app_id,
sl_effected_environment, sl_effected_profile_id, sl_effected_user_id,
sl_location, sl_event_description
from table2
where sl_effected_user_id like '+'''% '+@var1+ ''''
0
 
frogman22Author Commented:
the first solution fixed the % issue but when added caused a new error.  The second solution has fixed the problem.  Thanks for all the help and rapid response
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now