Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 Error "Conversion failed when converting the..."
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tykhai
Starting Member

Vietnam
1 Posts

Posted - 04/04/2012 :  12:53:07  Show Profile  Reply with Quote
Hi, i have stored procedures:

ALTER PROCEDURE [dbo].[KHP_DEB_AdvanceGetByUsers]
@StaffID int
AS

Declare @a as int
select @a=RoleID from userroles
where UserID=@StaffID and RoleID=0

SELECT
[KHP_DEB_Advance].[AdvanceID],
[KHP_DEB_Advance].[PortalID],
[KHP_DEB_Advance].[DateAdvance],
[KHP_DEB_Advance].[Amount],
[Users].[UserID] as [UserID],
[Users].[UserName] as UserName,
[KHP_DEB_Advance].[StaffID],
[KHP_DEB_Advance].[Description]
FROM [KHP_DEB_Advance]
INNER JOIN Users ON [KHP_DEB_Advance].[StaffID] = [Users].[UserID]
WHERE
StaffID LIKE CASE WHEN @a=0 THEN
'%'
ELSE
@StaffID
END

but....error
Conversion failed when converting the varchar value '%' to data type int.
Please help me!

sql-programmers
Posting Yak Master

USA
190 Posts

Posted - 04/25/2012 :  06:09:12  Show Profile  Visit sql-programmers's Homepage  Reply with Quote
Try this script,

ALTER PROCEDURE [dbo].[KHP_DEB_AdvanceGetByUsers]
@StaffID int
AS

Declare @a as int
select @a=RoleID from userroles
where UserID=@StaffID and RoleID=0

SELECT
[KHP_DEB_Advance].[AdvanceID],
[KHP_DEB_Advance].[PortalID],
[KHP_DEB_Advance].[DateAdvance],
[KHP_DEB_Advance].[Amount],
[Users].[UserID] as [UserID],
[Users].[UserName] as UserName,
[KHP_DEB_Advance].[StaffID],
[KHP_DEB_Advance].[Description]
FROM [KHP_DEB_Advance]
INNER JOIN Users ON [KHP_DEB_Advance].[StaffID] = [Users].[UserID]
WHERE
StaffID LIKE CASE WHEN @a=0 THEN
StaffID
ELSE
@StaffID
END

SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8781 Posts

Posted - 04/25/2012 :  07:18:49  Show Profile  Visit webfred's Homepage  Reply with Quote
quote:
Originally posted by sql-programmers

Try this script,

ALTER PROCEDURE [dbo].[KHP_DEB_AdvanceGetByUsers]
@StaffID int
AS

Declare @a as int
select @a=RoleID from userroles
where UserID=@StaffID and RoleID=0

SELECT
[KHP_DEB_Advance].[AdvanceID],
[KHP_DEB_Advance].[PortalID],
[KHP_DEB_Advance].[DateAdvance],
[KHP_DEB_Advance].[Amount],
[Users].[UserID] as [UserID],
[Users].[UserName] as UserName,
[KHP_DEB_Advance].[StaffID],
[KHP_DEB_Advance].[Description]
FROM [KHP_DEB_Advance]
INNER JOIN Users ON [KHP_DEB_Advance].[StaffID] = [Users].[UserID]
WHERE
StaffID LIKE = CASE WHEN @a=0 THEN
StaffID
ELSE
@StaffID
END

SQL Server Programmers and Consultants
http://www.sql-programmers.com/


Slight modification.
Don't use LIKE if it is not really needed.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000