Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
alter function fzz(@mdvn int,@fdate date,@tdate date)
returns @tabs table
(
	DVN int,
	PHC int,
	HSC int,
	ANC int,
	TT1 int,
	TTB int,
	IFA int,
	BP int,
	HB int
)
as
begin
declare @DVN int,@PHC int,@HSC int,@ANC int,@TT1 int,@TTB int,@IFA int,@BP int,@HB int,@ANC4 int
declare fnc cursor for
select dvn_cd,phc_cd,hsc_cd,sum(ANC1) as ANC,SUM(TT1) as TT1,sum(TTB2) as TT2,sum(IFA1) as IFA,sum(BP1) as BP,sum(HB1) as HB,sum(ANC4) as ANC4 from
(
select dvn_cd,phc_cd,hsc_cd,
    case when visit_no=3 and Visit_date between @fdate and @tdate then 1 else 0 end as ANC1,
    case when TTB=1 and TTDate between @fdate and @tdate then 1 else 0 end as TT1,
    case when TTB>1 and TTDate between @fdate and @tdate then 1 else 0 end as TTB2,
    case when IFA=100 and ANEDD between @fdate and @tdate then 1 else 0  end as IFA1, 	 
    case when BP>='140/90' and ANEDD between @fdate and @tdate then 1 else 0  end as BP1,
    case when HB<11 and ANEDD between @fdate and @tdate then 1 else 0  end as HB1,
    case when visit_no=4 and Visit_date between @fdate and @tdate then 1 else 0 end as ANC4
from anvisits3 where dvn_cd=@mdvn and ANEDD between @fdate and @tdate
)a group by dvn_cd,phc_cd,hsc_cd
open fnc
	fetch next from fnc into @DVN,@PHC,@HSC,@ANC,@TT1,@TTB,@IFA,@BP,@HB,@ANC4 
	while @@fetch_status=0
	begin
		 insert into @tabs
            select 'DVN'+convert(varchar(20),@DVN),'PHC'+convert(varchar(20),@PHC),'HSC'+convert(varchar(20),@HSC),
            'ANC'+convert(varchar(20),@ANC),'TT1'+Convert(varchar(20),@TT1),'TTB'+convert(varchar(20),@TTB),'IFA'+convert(varchar(20),@IFA),
            'BP'+convert(varchar(20),@BP),'HB'+convert(varchar(20),@HB)+'ANC4'+convert(varchar(20),@ANC4)
        fetch next from fnc into @DVN,@PHC,@HSC,@ANC,@TT1,@TTB,@IFA,@BP,@HB,@ANC4
	end
return
end

SQL
-----------------
SELECT * FROM fzz(40,'2011/01/01','2011/12/31')
-----------------

Error as
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'DVN40' to data type int.
Posted
Updated 8-Jan-14 23:19pm
v2

SQL
alter function fzz(@mdvn int,@fdate date,@tdate date)
returns @tabs table
(
	DVN varchar(23),
        -- Leave the rest unchanged


You are trying to store a varchar value in an integer variable; which is obviously not possible.
You have to change the type of the DVN column in the @tabs table.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 9-Jan-14 6:43am    
Your state, that "trying to store a varchar value in an integer variable; which is obviously not possible" is not quite true.
It depends on the value...
phil.o 9-Jan-14 6:49am    
Could you give an example where storing a value with letters in an int variable is possible?
Kornfeld Eliyahu Peter 9-Jan-14 7:07am    
You right if we talk about letters, but you state that nvarchar can't be stored in int - that's not true - it depends on it's value...
phil.o 9-Jan-14 7:22am    
I was talking about concrete facts deducted from the code that has been given.
Kornfeld Eliyahu Peter 9-Jan-14 7:24am    
In that case you wasn't so clear...to me :-)
@DVN gets the value 40 (as @mdvn is 40) and then in your select
SQL
select 'DVN'+convert(varchar(20),@DVN)

you push the value 'DVN40' back to @DVN which is int!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900