Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
In this query I m getting the error "Must declare @PR_Name as scalar variable "
Can anybody reply????

SQL
exec sp_Select_Building_FlatNEW 1,1                                
 select @UserNames =UserName    
    
  if exists (select * from sysobjects where xtype='u' and name='#flnDtls')    
  begin    
  drop table #flnDtls    
  end    
      
  create table #flnDtls     
 (UserNames nvarchar(max),    
 Flat_No varchar(50),    
 Flat_Id int,    
 PR_ID int)    
      
  insert into #flnDtls exec sp_Select_Building_FlatNEW @SC_ID,@Flat_Id    
      
  select * from #flnDtls    
  --declare @sss nvarchar(max)    
  set @PR_Name = (select Top 1 UserNames from #flnDtls)    
  print @PR_Name    
      
if(@Quat=1)                
begin               
set @sql='SELECT DISTINCT       
                      dbo.iBillHead.BillHeadName, dbo.Society.SCName, CONVERT(varchar(500), dbo.Flat.Flat_No) AS Flat_No, dbo.Building.Building_Name,       
                      dbo.Flat.Flat_Area, dbo.TransMember_BillHead.Trans_Bill_Quat AS Amount, dbo.Building.Building_Id, dbo.Bill_Generation.Bill_No, CONVERT(varchar,       
                      dbo.Bill_Generation.Bill_Date, 103) AS BillDate, SUBSTRING(DATENAME(m, dbo.Bill_Generation.Bill_To_Date), 0, 4) AS FromMonth, SUBSTRING(DATENAME(m,       
                      dbo.Bill_Generation.Bill_From_Date), 0, 4) AS ToMonth, dbo.Society.SCRegDate, dbo.Society.SCAddr, dbo.Society.SCRegNo, dbo.Flat.Flat_Id,       
                      dbo.Bill_Generation.Bill_Id, dbo.Bill_Generation.Total_Amount AS Total, dbo.Bill_Generation.Pending_Amount AS pending, dbo.Bill_Generation.Bill_To_Date,       
                      dbo.Bill_Generation.Bill_From_Date, dbo.Bill_Generation.Due_Date, dbo.Bill_Generation.LateCharge_Amount, dbo.Bill_Generation.Discount,       
                      dbo.Bill_Generation.Bill_Date, dbo.Bill_Generation.Arrears, dbo.Bill_Generation.Grand_Total, dbo.Bill_Generation.Principal_Amount , @PR_Name as Username    
FROM         dbo.Member INNER JOIN      
                      dbo.Society INNER JOIN      
                      dbo.Flat INNER JOIN      
                      dbo.Building ON dbo.Flat.Building_Id = dbo.Building.Building_Id INNER JOIN      
                      dbo.Member_Flat ON dbo.Flat.Flat_Id = dbo.Member_Flat.Flat_Id ON dbo.Society.SC_ID = dbo.Building.Soc_Id INNER JOIN      
                      dbo.iBillHead INNER JOIN      
                      dbo.TransSoc_BillHead ON dbo.iBillHead.BillHeadID = dbo.TransSoc_BillHead.Trans_BillHeadID INNER JOIN      
                      dbo.TransMember_BillHead ON dbo.iBillHead.BillHeadID = dbo.TransMember_BillHead.Trans_BillHeadID ON       
                      dbo.Society.SC_ID = dbo.TransSoc_BillHead.Trans_SC_ID ON dbo.Member.PR_ID = dbo.Member_Flat.PR_ID AND       
                      dbo.Member.PR_ID = dbo.TransMember_BillHead.Trans_Member_ID INNER JOIN      
                   dbo.Bill_Generation ON dbo.Flat.Flat_Id = dbo.Bill_Generation.Flat_Id      
            

end            
            
exec(@sql)
Posted
Updated 2-Sep-12 9:55am
v3

Yes, you are getting this error because you should. And in my opinion, the error is self explanatory. It says you have to declare this variable, and even if you do a find in your code, you can see that you never declared this variable.

SQL
set @PR_Name = (select Top 1 UserNames from #flnDtls)
print @PR_Name


Apart from that, I don't even see that you actually made use of this variable anywhere. I would either declare it some thing like this:

SQL
DECLARE @PR_Name NVARCHAR(256)


Or remove the redundant code.
 
Share this answer
 
Hi,

you are setting the value into the variable @PR_Name but you have not declared that variable.

SQL
Declare @PR_Name NVARCHAR(500)


Please try this thing. may be you find the soltion.

Thanks,
Viprat
 
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