Click here to Skip to main content
15,887,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here, I am generate WardNo+6 digit = Registration No...How can i test the funtion ....error in Bold Capital words please .....give solution...
I'm getting a frustrating error in one of my SQL Server 2008 r2 queries. It parses fine, but crashes when I try to execute. The error I get is the following :

Msg 8120, Level 16, State 1, Line 1
Column 'dbo.FamilyHead.WardID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

fnFormatSerialNoPostfix:- it's function genrate 6 digite no it's...ok

SQL
SET @RegistrationNo=(SELECT CONVERT(Varchar(15),CONVERT(varchar,@WardID) + 'W-') +         CONVERT(varchar(6),dbo.fnFormatSerialNoPostfix(CONVERT(int,SUBSTRING(MAX(RegistrationNo),10,6)+1))) FROM dbo.ParivarCertificate INNER JOIN dbo.FamilyMember ON dbo.ParivarCertificate.FamilyMemberID = dbo.FamilyMember.FamilyMemberID  INNER JOIN dbo.FamilyHead ON dbo.FamilyMember.FamilyID = dbo.FamilyHead.FamilyID WHERE dbo.FamilyHead.WardID = @WardID )





How to fix it.... All Code bellow...

SQL
ALTER FUNCTION [dbo].[fnRegitrationNoExp] (@WardID int)

RETURNS Varchar(12)
AS
BEGIN
    -- Declare the return variable here
        DECLARE @RegistrationNo Varchar(15)
        if Exists(SELECT RegistrationNo FROM dbo.ParivarCertificate
                  INNER JOIN dbo.FamilyMember ON dbo.ParivarCertificate.FamilyMemberID = dbo.FamilyMember.FamilyMemberID
                  INNER JOIN dbo.FamilyHead ON dbo.FamilyMember.FamilyID = dbo.FamilyHead.FamilyID
                  WHERE dbo.FamilyHead.WardID =@WardID)
                  --WHERE FH.WardID=@Ward)

            Begin

                SET @RegistrationNo=(SELECT CONVERT(Varchar(15),CONVERT(varchar,@WardID) + 'W-') +
                CONVERT(varchar(6),dbo.fnFormatSerialNoPostfix(CONVERT(int,SUBSTRING(MAX(RegistrationNo),10,6)+1)))
                FROM dbo.ParivarCertificate
                  INNER JOIN dbo.FamilyMember ON dbo.ParivarCertificate.FamilyMemberID = dbo.FamilyMember.FamilyMemberID
                  INNER JOIN dbo.FamilyHead ON dbo.FamilyMember.FamilyID = dbo.FamilyHead.FamilyID
                  WHERE dbo.FamilyHead.WardID = @WardID )

            End
        Else
        Begin

                SET @RegistrationNo=(SELECT CONVERT(varchar(15),CONVERT(int,@WardID) + 'W-') +
                CONVERT(varchar(6),dbo.fnFormatSerialNoPostfix(1)))

        End

        -- Return the result of the function
        RETURN @RegistrationNo

END
Posted
Updated 19-May-13 23:18pm
v9
Comments
CHill60 20-May-13 5:03am    
So which of the two errors are you trying to fix first? The one in the heading of your post needs to be first and then the 8120. We'll probably also need to see the contents of fnFormatSerialNoPostfix
Faizymca 20-May-13 5:17am    
Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fnRegistrationNoExp", or the name is ambiguous....I want to fix that ...
Sir, Simply genrate 6 digit no.... here

.....ALTER Function [dbo].[fnFormatSerialNoPostfix]
(@SerialNo Int)
Returns varchar(10)
As
Begin

return Right(Replicate('0',6)+Convert(Varchar(10),@SerialNo),6)

End

1 solution

The problem is happening because you are trying to call dbo.fnRegistrationNoExp but your function has been defined as [dbo].[fnRegitrationNoExp] ... you have misspelled the function name when creating it (missing 's')
 
Share this answer
 
Comments
QUANGPHAT ĐINH 10-Nov-16 3:06am    
thank, You saved my time :))

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