TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Azaad Abbas
NA
221
41.7k
How to prevent Sql Error : Must declare the scalar variable
Apr 18 2015 9:32 AM
I tried on one query without paging concept like below
alter procedure temp
@result varchar(50)
as
begin
if @result='all'
begin
select Receive_Payment.PayId,Receive_Payment.UserId,C_Register.Name,C_Register.UserName
,Packages.PackageName,Packages.PackagePeriod,Receive_Payment.InstallCharge
,Receive_Payment.AmountToPay,Receive_Payment.PyingAmount,Receive_Payment.Balance
,Receive_Payment.DiscountToPay,Area.AreaName,C_Register.MobNo
,C_Register.CreatedDate,Employee.EmpName,Receive_Payment.PayBy
,Receive_Payment.BankName,Receive_Payment.ChequeNo,Receive_Payment.ChequeDate
FROM Receive_Payment INNER JOIN C_Register ON Receive_Payment.UserId = C_Register.UserId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
where C_Register.AccountExpiry=Receive_Payment.OldExpiryDate
end
if @result='single'
begin
select Receive_Payment.PayId,Receive_Payment.UserId,C_Register.Name,C_Register.UserName
,Packages.PackageName,Packages.PackagePeriod,Receive_Payment.InstallCharge
,Receive_Payment.AmountToPay,Receive_Payment.PyingAmount,Receive_Payment.Balance
,Receive_Payment.DiscountToPay,Area.AreaName,C_Register.MobNo
,C_Register.CreatedDate,Employee.EmpName,Receive_Payment.PayBy
,Receive_Payment.BankName,Receive_Payment.ChequeNo,Receive_Payment.ChequeDate
FROM Receive_Payment INNER JOIN C_Register ON Receive_Payment.UserId = C_Register.UserId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
where C_Register.AccountExpiry=Receive_Payment.OldExpiryDate And Packages.PackageName='2000 package'
end
end
its working good by execute it `temp '1'` or `temp 'all'`
but with paging concept I applied same this procedure in like below storeprocedure.
alter PROCEDURE [dbo].[Get_Payment]
@PageIndex INT = 1
,@PageSize INT = 10
,@RecordCount INT OUTPUT
,@discount float output
,@paidamount float output
,@tableName varchar(50)=NULL
,@ColumnName VARCHAR(50)=NULL
,@Value VARCHAR(50)=NULL
,@result varchar(50)=NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @cmd AS NVARCHAR(max)
CREATE TABLE #payInfo
(
rownum INT,
payId INT,
userid INT,
NAME VARCHAR(100),
username VARCHAR(100),
packageperiod VARCHAR(50),
packagename VARCHAR(100),
installCharge float,
amountToPay float,
balance float,
pyingamount float,
discountToPay float,
areaName varchar(100),
mobno VARCHAR(50),
createddate DATE,
empname VARCHAR(100),
PayBy VARCHAR(100),
bankName varchar(100),
chequeNo varchar(100),
chequeDate date
)
SET @cmd ='if @result=''all''
begin
SELECT ROW_NUMBER() OVER
(
ORDER BY Receive_Payment.PayId desc
)AS rownum
,Receive_Payment.PayId,Receive_Payment.UserId,C_Register.Name,C_Register.UserName
,Packages.PackageName,Packages.PackagePeriod,Receive_Payment.InstallCharge
,Receive_Payment.AmountToPay,Receive_Payment.PyingAmount,Receive_Payment.Balance
,Receive_Payment.DiscountToPay,Area.AreaName,C_Register.MobNo
,C_Register.CreatedDate,Employee.EmpName,Receive_Payment.PayBy
,Receive_Payment.BankName,Receive_Payment.ChequeNo,Receive_Payment.ChequeDate
FROM Receive_Payment INNER JOIN C_Register ON Receive_Payment.UserId = C_Register.UserId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
where C_Register.AccountExpiry=Receive_Payment.OldExpiryDate
end
if @result=''single''
begin
SELECT ROW_NUMBER() OVER
(
ORDER BY Receive_Payment.PayId desc
)AS rownum
,Receive_Payment.PayId,Receive_Payment.UserId,C_Register.Name,C_Register.UserName
,Packages.PackageName,Packages.PackagePeriod,Receive_Payment.InstallCharge
,Receive_Payment.AmountToPay,Receive_Payment.PyingAmount,Receive_Payment.Balance
,Receive_Payment.DiscountToPay,Area.AreaName,C_Register.MobNo
,C_Register.CreatedDate,Employee.EmpName,Receive_Payment.PayBy
,Receive_Payment.BankName,Receive_Payment.ChequeNo,Receive_Payment.ChequeDate
FROM Receive_Payment INNER JOIN C_Register ON Receive_Payment.UserId = C_Register.UserId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
where C_Register.AccountExpiry=Receive_Payment.OldExpiryDate And '+@tableName+'.'+@ColumnName+'='''+@Value+'''
end'
Insert into #payInfo
EXEC(@cmd)
SELECT @RecordCount = COUNT(*) FROM #payInfo
select @discount = sum(discountToPay) FROM #payInfo
select @paidamount = sum(pyingamount) FROM #payInfo
SELECT * FROM #payInfo
WHERE rownum BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
DROP TABLE #payInfo
print @cmd
END
then throwing an error by executing from codebehind side liek below
Must declare the scalar variable "@result".
Must declare the scalar variable "@result".
if @result='all'
begin
SELECT ROW_NUMBER() OVER
(
ORDER BY Receive_Payment.PayId desc
)AS rownum
,Receive_Payment.PayId,Receive_Payment.UserId,C_Register.Name,C_Register.UserName
,Packages.PackageName,Packages.PackagePeriod,Receive_Payment.InstallCharge
,Receive_Payment.AmountToPay,Receive_Payment.PyingAmount,Receive_Payment.Balance
,Receive_Payment.DiscountToPay,Area.AreaName,C_Register.MobNo
,C_Register.CreatedDate,Employee.EmpName,Receive_Payment.PayBy
,Receive_Payment.BankName,Receive_Payment.ChequeNo,Receive_Payment.ChequeDate
FROM Receive_Payment INNER JOIN C_Register ON Receive_Payment.UserId = C_Register.UserId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
where C_Register.AccountExpiry=Receive_Payment.OldExpiryDate
end
if @result='single'
begin
SELECT ROW_NUMBER() OVER
(
ORDER BY Receive_Payment.PayId desc
)AS rownum
,Receive_Payment.PayId,Receive_Payment.UserId,C_Register.Name,C_Register.UserName
,Packages.PackageName,Packages.PackagePeriod,Receive_Payment.InstallCharge
,Receive_Payment.AmountToPay,Receive_Payment.PyingAmount,Receive_Payment.Balance
,Receive_Payment.DiscountToPay,Area.AreaName,C_Register.MobNo
,C_Register.CreatedDate,Employee.EmpName,Receive_Payment.PayBy
,Receive_Payment.BankName,Receive_Payment.ChequeNo,Receive_Payment.ChequeDate
FROM Receive_Payment INNER JOIN C_Register ON Receive_Payment.UserId = C_Register.UserId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
where C_Register.AccountExpiry=Receive_Payment.OldExpiryDate And C_Register.UserId='147'
end
so how to prevent from this?
Reply
Answers (
1
)
case queries using multiple tables in sql
In Sql query Which part of Sql query first Execute ????