Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with passing variable parameters to an SP

Status
Not open for further replies.

suel

Programmer
May 9, 2002
13
GB
I have written the following SP. This can have have aany or all of the 3 parameters.
In Enterprise Manager, on checking the syntax of the SP it says it is OK. When I try and test this in QA I get the following message :
'Syntax error converting varchar value 'and hospcode =' to a column of data type int'

HELP .... Please ...
Thanks
Suel

Code:
CREATE PROCEDURE slFind_Errors_Rpt

	@hcode	int = null,
	@survyear	int = null,
	@speriod	int = null
as

declare @select varchar(255)
declare @from varchar(255)
declare @where varchar(255)
	

select @select = 'select serialnum,hospcode,errorid,syear,survperiod '
select @from = 'from errorlog'
select @where = ''


if @hcode is not null
	begin
	select @where  = @where + ' and  hospcode =  ''' + @hcode + ''''
	end

if @survyear  is not null
	begin
	select @where = @where + ' and syear = ''' + @survyear + ''''
	end

if @speriod is not null
	begin
	select @where = @where + ' and survperiod = ''' + @speriod + ''''
	end

if datalength(@where) <> 0
begin
	select @where = stuff(@where,1,4,'where')
end

print @select + @from + @where

--exec(@select + @from + @where)
GO
 
You need to CAST or CONVERT you INT variable to VARCHAR to put them into a string....

if @hcode is not null
begin
select @where = @where + ' and hospcode = ''' + CAST(@hcode as varchar(10))+ ''''
end
 
BTW - I'm guessing that hospcode is stored as CHAR or VARCHAR and that is why you are putting quotes around your sql string. If they are int then it should read:

if @hcode is not null
begin
select @where = @where + ' and hospcode = ' + CAST(@hcode as varchar(10))
end
 
Cheers mwolf00 using CAST did the trick !

Many Thanks

Suel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top