Solved

Msg 402 Error in Code SQL in Sql Server 2005 Enviroment

Posted on 2008-06-09
3
2,186 Views
Last Modified: 2010-04-21
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
Comment
Question by:frogman22
3 Comments
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 100 total points
ID: 21746074
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
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 400 total points
ID: 21746099
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
 

Author Closing Comment

by:frogman22
ID: 31465532
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to SUM hours for the same record 1 36
SQL help 5 54
how many extra RAM for SQL server is needed 22 38
kill process lock Sql server 9 47
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question