The variable name '@p1' has already been declared
Posted: Thu 21 Apr 2011 19:48
I'm writing a migration wizard to convert many projects from BDE to SDAC. One of the forms has a query component with the SQL text shown at the end of this posting. It uses two parameters. After converting to SDAC, when the query is run, the error 'The variable name '@p1' has already been declared' occurs. However, this error does not occur in the BDE version of the program.
The only thing migrated in this form is the BDE TQuery component to a TMSQuery component. Interestingly, the error does not occur for the other parameter (@SchemeRef). If I remove the 'declare @p1 integer' from the SQL text, then it works.
To narrow down the problem, I created a blank project, placed a TMSQuery component on it and, for the SQL attribute, placed the following code:
declare @SchemeRef char(8)
declare @p1 integer
select @Schemeref = :Schemeref
Even with only the above code, I still get 'The variable name @p1 has already been declared'.
If I remove the 'select @Schemeref = :Schemeref' line, then the error does not occur.
Incidentally, if I change the name of @p1 to anything else (e.g. @p2, @paul, @jack), then the problem does not occur.
What is wrong with @p1 variable using SDAC?
Here is the complete SQL text for the TMSQuery component:
declare @SchemeRef char(8)
declare @p1 integer
select @Schemeref = :Schemeref
select @p1 = :P1
Create table #list1
(
SchemeRef char(8) null,
EmploymentNbr char(8) null,
Salary1 money null,
Salary2 money null
)
insert into #list1
(Schemeref,EmploymentNbr,Salary1,Salary2)
select SchemeMembers.Schemeref,SchemeMembers.Employmentnbr,0,0
from Employment,Person,Company,SchemeMembers
where SchemeMembers.Schemeref = @SchemeRef
and SchemeMembers.EmploymentNbr = Employment.EmploymentNbr
and Employment.companyRef = Company.Companyref
and Employment.Personref =Person.Personref
and ((ExitStatus = 'N') or (ExitStatus is null))
update #list1
set Salary1 = salary
from SalaryDetails,Employment,Person,Company,#list1
where SalaryDetails.Schemeref = @SchemeRef
and DatePart(year,DtSalEffective) * 100 + DatePart(month,DtSalEffective) < @p1
and DtSalEffective in
(select max(DtSalEffective) from SalaryDetails T1
where t1.Schemeref = SalaryDetails.Schemeref
and t1.Employmentnbr = SalaryDetails.Employmentnbr
and DatePart(year,t1.DtSalEffective) * 100 + DatePart(month,t1.DtSalEffective) < @p1)
and SalaryDetails.EmploymentNbr = Employment.EmploymentNbr
and Employment.companyRef = Company.Companyref
and Employment.Personref =Person.Personref
and SalaryDetails.Schemeref = #list1.Schemeref
and SalaryDetails.EmploymentNbr = #list1.EmploymentNbr
update #list1
set Salary2 = salary
from SalaryDetails,Employment,Person,Company,#list1
where SalaryDetails.Schemeref = @SchemeRef
and DatePart(year,DtSalEffective) * 100 + DatePart(month,DtSalEffective) = @p1
and DtSalEffective in
(select max(DtSalEffective) from SalaryDetails T1
where t1.Schemeref = SalaryDetails.Schemeref
and t1.Employmentnbr = SalaryDetails.Employmentnbr
and DatePart(year,t1.DtSalEffective) * 100 + DatePart(month,t1.DtSalEffective) = @p1)
and SalaryDetails.EmploymentNbr = Employment.EmploymentNbr
and Employment.companyRef = Company.Companyref
and Employment.Personref =Person.Personref
and SalaryDetails.Schemeref = #list1.Schemeref
and SalaryDetails.EmploymentNbr = #list1.EmploymentNbr
select Surname,Initials,IDNo,rtrim(CompanyName) + ' ' + rtrim(CompanyName1) CompanyName,
Salary1,Salary2,EmployeeNo,'N' SelectedYN, SchemeMembers.Schemeref,SchemeMembers.EmploymentNbr,
Person.PersonRef
from Employment,Person,Company,SchemeMembers,#list1
where Employment.companyRef = Company.Companyref
and Employment.Personref =Person.Personref
and #list1.Schemeref = SchemeMembers.Schemeref
and #list1.EmploymentNbr = SchemeMembers.EmploymentNbr
and #list1.EmploymentNbr = Employment.EmploymentNbr
order by Salary2,Salary1
drop table #list1
I'm running SDACE 5.00.0.5 for Delphi 5
Thank you for your assistance.[/b]
The only thing migrated in this form is the BDE TQuery component to a TMSQuery component. Interestingly, the error does not occur for the other parameter (@SchemeRef). If I remove the 'declare @p1 integer' from the SQL text, then it works.
To narrow down the problem, I created a blank project, placed a TMSQuery component on it and, for the SQL attribute, placed the following code:
declare @SchemeRef char(8)
declare @p1 integer
select @Schemeref = :Schemeref
Even with only the above code, I still get 'The variable name @p1 has already been declared'.
If I remove the 'select @Schemeref = :Schemeref' line, then the error does not occur.
Incidentally, if I change the name of @p1 to anything else (e.g. @p2, @paul, @jack), then the problem does not occur.
What is wrong with @p1 variable using SDAC?
Here is the complete SQL text for the TMSQuery component:
declare @SchemeRef char(8)
declare @p1 integer
select @Schemeref = :Schemeref
select @p1 = :P1
Create table #list1
(
SchemeRef char(8) null,
EmploymentNbr char(8) null,
Salary1 money null,
Salary2 money null
)
insert into #list1
(Schemeref,EmploymentNbr,Salary1,Salary2)
select SchemeMembers.Schemeref,SchemeMembers.Employmentnbr,0,0
from Employment,Person,Company,SchemeMembers
where SchemeMembers.Schemeref = @SchemeRef
and SchemeMembers.EmploymentNbr = Employment.EmploymentNbr
and Employment.companyRef = Company.Companyref
and Employment.Personref =Person.Personref
and ((ExitStatus = 'N') or (ExitStatus is null))
update #list1
set Salary1 = salary
from SalaryDetails,Employment,Person,Company,#list1
where SalaryDetails.Schemeref = @SchemeRef
and DatePart(year,DtSalEffective) * 100 + DatePart(month,DtSalEffective) < @p1
and DtSalEffective in
(select max(DtSalEffective) from SalaryDetails T1
where t1.Schemeref = SalaryDetails.Schemeref
and t1.Employmentnbr = SalaryDetails.Employmentnbr
and DatePart(year,t1.DtSalEffective) * 100 + DatePart(month,t1.DtSalEffective) < @p1)
and SalaryDetails.EmploymentNbr = Employment.EmploymentNbr
and Employment.companyRef = Company.Companyref
and Employment.Personref =Person.Personref
and SalaryDetails.Schemeref = #list1.Schemeref
and SalaryDetails.EmploymentNbr = #list1.EmploymentNbr
update #list1
set Salary2 = salary
from SalaryDetails,Employment,Person,Company,#list1
where SalaryDetails.Schemeref = @SchemeRef
and DatePart(year,DtSalEffective) * 100 + DatePart(month,DtSalEffective) = @p1
and DtSalEffective in
(select max(DtSalEffective) from SalaryDetails T1
where t1.Schemeref = SalaryDetails.Schemeref
and t1.Employmentnbr = SalaryDetails.Employmentnbr
and DatePart(year,t1.DtSalEffective) * 100 + DatePart(month,t1.DtSalEffective) = @p1)
and SalaryDetails.EmploymentNbr = Employment.EmploymentNbr
and Employment.companyRef = Company.Companyref
and Employment.Personref =Person.Personref
and SalaryDetails.Schemeref = #list1.Schemeref
and SalaryDetails.EmploymentNbr = #list1.EmploymentNbr
select Surname,Initials,IDNo,rtrim(CompanyName) + ' ' + rtrim(CompanyName1) CompanyName,
Salary1,Salary2,EmployeeNo,'N' SelectedYN, SchemeMembers.Schemeref,SchemeMembers.EmploymentNbr,
Person.PersonRef
from Employment,Person,Company,SchemeMembers,#list1
where Employment.companyRef = Company.Companyref
and Employment.Personref =Person.Personref
and #list1.Schemeref = SchemeMembers.Schemeref
and #list1.EmploymentNbr = SchemeMembers.EmploymentNbr
and #list1.EmploymentNbr = Employment.EmploymentNbr
order by Salary2,Salary1
drop table #list1
I'm running SDACE 5.00.0.5 for Delphi 5
Thank you for your assistance.[/b]