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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SS converting varchar to int on its own...
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

crazycat503
Starting Member

Eritria
16 Posts

Posted - 04/30/2012 :  07:05:31  Show Profile  Reply with Quote
I have a Stored Pro that accepts gender argument. It is like


ALTER PROCEDURE [dbo].[dslmsSP_memberReportsLists]
-- Add the parameters for the stored procedure here


@gender varchar(1)='',


AS
DECLARE @runsql varchar(4000)='SELECT DISTINCT studentidcard,firstname from tblstudents'
DECLARE @limits varchar(4000)=''

BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- add limits now
IF @gender<>''
SET @limits=@limits + ' sex=' + CHAR(39) + @gender + CHAR(39)
-- DO WE NEED TO LIMIT STUFF OR NOT?
if @limits<>''
BEGIN

SET @runsql=@runsql + ' WHERE ' + @limits
END

-- exectue statement

EXEC (@runsql)
return @@ERROR

[/CODE]

When I run it, gives the following error:

Msg 245, Level 16, State 1, Procedure dslmsSP_memberReportsLists, Line 113
Conversion failed when converting the varchar value ' sex='M' ' to data type int.


I dont want it to be converted to int at all nor I have casted anywhere. It is drivin me really crazy..anyone can spot the source please? The error is raised if I pass gender argument only.

All guys!

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 04/30/2012 :  07:14:14  Show Profile  Visit webfred's Homepage  Reply with Quote
I can't see a reason why this should be done using dynamic sql.

Just
select ... from ... where sex=@gender

would do it


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/30/2012 :  07:18:45  Show Profile  Reply with Quote
I am assuming you have a rationale for doing this in dynamic SQL other than what can be observed from the stored proc. Regardless, do two things:

a) Check what the data type of the column sex in tblstudents is. Is it an int or numeric type.

b) Instead of calling EXEC (@runsql), print out @runsql and execute that to see if you are able to execute it correctly from an SSMS window.
Go to Top of Page

crazycat503
Starting Member

Eritria
16 Posts

Posted - 05/01/2012 :  14:23:16  Show Profile  Reply with Quote
1st...there are other pms required...it is custom. Every parameter causes the error so it is why i just listed that one..

2. sex field is not int/numeric in db.

All guys!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 05/01/2012 :  14:28:01  Show Profile  Reply with Quote
change
SET @limits=@limits + ' sex=' + CHAR(39) + @gender + CHAR(39)
to
SET @limits=@limits + ' sex=''' + @gender + ''''

and try


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

crazycat503
Starting Member

Eritria
16 Posts

Posted - 05/03/2012 :  10:37:59  Show Profile  Reply with Quote
i tried that...not workin either.

All guys!
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3832 Posts

Posted - 05/03/2012 :  13:18:41  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

change
SET @limits=@limits + ' sex=' + CHAR(39) + @gender + CHAR(39)
to
SET @limits=@limits + ' sex=''' + @gender + ''''

I might be missing something, but why change from using the nice CHAR(39) method to mashing single-quotes together method?

Edited by - Lamprey on 05/03/2012 13:19:19
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.08 seconds. Powered By: Snitz Forums 2000