我需要将列表传递给我的SQL Server存储过程。我已经生成了列表,它是使用逗号分隔的数字列表:
<cfquery name="House" datasource="#application.datasource#">
SELECT cGLsubaccount
FROM HOUSE
WHERE Right(cGLsubaccount,3) IN
(
<cfloop index="i" list="#consulsub#">
<cfoutput>#right(Trim(i) , 3)#,</cfoutput>
</cfloop>
00000000
)
</cfquery>
<cfset GLsubacct = Valuelist(House.cGLsubaccount,",")>
现在,我使用cfprocparam
将cfsqltype传递给存储过程到varchar(1000)。 (我在google上读到这是将列表传递给存储过程的方法)。
<cfprocparam type="IN" value="#GLsubacct#" DBVARNAME="@GLsubacct" cfsqltype="cf_sql_varchar">
在存储过程中,我这样做了:
ALTER PROCEDURE [rw].[sp_EFT_NoticeXLS2] (
@Scope varchar(50)
, @AcctPeriod char(6)
, @CompanyID nchar(10)
, @GLsubacct varchar(1000) = NULL
)
AS......
,存储过程中的SQL语句如下所示:
CREATE TABLE #Houses (
iHouse_ID int,
cName varchar(50),
cNumber varchar(10),
cPhoneNumber1 varchar(25),
cAddressLine1 varchar(50),
cCity varchar(30),
cStateCode varchar(2),
cZipCode varchar(10),
iUnitsAvailable int,
iRegOpsNumber int,
cOpsName varchar(50),
iRegionID int,
cRegionName varchar(50)
)
INSERT INTO #Houses (
iHouse_ID,
cName,
cNumber,
cPhoneNumber1,
cAddressLine1,
cCity,
CStateCode,
cZipCode,
iUnitsAvailable,
iRegOpsNumber,
cOpsName,
iRegionID,
cRegionName )
SELECT
h2.iHouse_ID,
h.cName,
h.cNumber,
h.cPhoneNumber1,
h.cAddressLine1,
h.cCity,
h.CStateCode,
h.cZipCode,
h.iUnitsAvailable,
r.RegOpsNumber,
r.opsname,
r.iRegion_ID,
r.regionname
FROM rw.fn_GetScopeHouses (@Scope, @dtReport, NULL, NULL, 0) h2
JOIN House h on h.iHouse_ID = h2.iHouse_ID
AND h.cCompanyID = @CompanyID
AND ( @GLsubacct IS NULL OR h.iHouse_ID IN ('+ @GLsubacct +') )
JOIN rw.vw_Reg_Ops r on r.opsareaID = h.iOpsArea_ID
问题是我收到了错误。错误消息是......
Error Executing Database Query.
[Macromedia][SQLServer JDBC Driver][SQLServer]Conversion failed when converting the varchar value '+ @GLsubacct +' to data type int.
The error occurred in E:\inetpub\wwwroot\intranet\TIPS4\Admin\EFTprocesspullfile.cfm: line 46
44 : <cfprocparam type="IN" value="#CompanyID#" DBVARNAME="@CompanyID" cfsqltype="cf_sql_varchar"><!--- TPecku added variable to be passed to the stored Proc --->
45 : <cfif CompanyID EQ 0000>
46 : <cfprocparam type="IN" value="#GLsubacct#" DBVARNAME="@GLsubacct" cfsqltype="cf_sql_varchar">
47 : </cfif>
48 : </cfstoredproc>
错误在存储过程中,因为那是我的'+ @GLsubacct +'。 关于我做错了什么的任何想法? (对不起,很长的帖子,我想尽可能详细地说明你能理解我在做什么。
答案 0 :(得分:0)
为了使这项功能与现有代码接近,您必须在存储过程中创建一个查询字符串(替换insert语句)并执行它。
我已经创建了一些虚拟/测试变量,以查看查询如何使用PRINT语句查看。你可以从最后用“DECLARE @execQuery varchar(MAX)”的代码行开始替换你的insert语句。
这样的逻辑应该在临时表#Houses中填入您可以使用的数据。
-- test variables
DECLARE @Scope varchar(50), @AcctPeriod char(6), @CompanyID nchar(10), @GLsubacct varchar(1000), @dtReport VARCHAR(20);
SET @Scope = 'scope';
SET @AcctPeriod = 'fall';
SET @CompanyID = N'coID';
SET @dtReport = 'dtRpt'
SET @GLsubacct = '123,234,345,456'
-- copy code below this line
DECLARE @execQuery varchar(MAX)
SET @execQuery = 'insert into #Houses (
iHouse_ID,
cName,
cNumber,
cPhoneNumber1,
cAddressLine1,
cCity,
CStateCode,
cZipCode,
iUnitsAvailable,
iRegOpsNumber,
cOpsName,
iRegionID,
cRegionName
)
select
h2.iHouse_ID,
h.cName,
h.cNumber,
h.cPhoneNumber1,
h.cAddressLine1,
h.cCity,
h.CStateCode,
h.cZipCode,
h.iUnitsAvailable,
r.RegOpsNumber,
r.opsname,
r.iRegion_ID,
r.regionname
FROM rw.fn_GetScopeHouses ('''+ @Scope +''', '''+ @dtReport +''', NULL, NULL, 0) h2
JOIN House h on h.iHouse_ID = h2.iHouse_ID
AND h.cCompanyID = N'''+ @CompanyID +''''
IF (@GLsubacct IS NOT NULL)
BEGIN
SET @execQuery = @execQuery + ' AND ( h.iHouse_ID IN ('+ @GLsubacct +') )'
END
SET @execQuery = @execQuery + 'JOIN rw.vw_Reg_Ops r on r.opsareaID = h.iOpsArea_ID'
--PRINT @execQuery
EXEC @execQuery
答案 1 :(得分:0)
我终于想出了如何让它发挥作用。在我将列表传递给存储过程变量之后,我无法直接在我的sql语句中使用该变量,它不起作用,所以我所做的是遍历变量的内容并将它们存储在临时表中。然后我在我的SQL语句中使用临时表的内容。如果你看上面,你会看到我试图做的那些不起作用。我最终做的是....
declare @oneNumber int
declare @POS int
if @GLsubacct <> ''
begin
--Step through the comma delimted list of numbers and plug each one into a temp table
create table #HouseList (HouseID int)
select @POS = patindex('%,%',@GLsubacct)
while @POS > 0
begin
select @oneNumber = cast(left(@GLsubacct,@POS - 1) as int)
insert into #HouseList (HouseID) values (@oneNumber)
select @GLsubacct = right(@GLsubacct,len(@GLsubacct) - @POS)
select @POS = patindex('%,%',@GLsubacct)
end
--do the last one which doesn't have any comma
select @oneNumber = cast(@GLsubacct as int)
insert into #HouseList (HouseID) values (@oneNumber)
end
-- Create #House table
CREATE TABLE #Houses (
iHouse_ID int,
cName varchar(50),
cNumber varchar(10),
cPhoneNumber1 varchar(25),
cAddressLine1 varchar(50),
cCity varchar(30),
cStateCode varchar(2),
cZipCode varchar(10),
iUnitsAvailable int,
iRegOpsNumber int,
cOpsName varchar(50),
iRegionID int,
cRegionName varchar(50))
insert into #Houses (
iHouse_ID,
cName,
cNumber,
cPhoneNumber1,
cAddressLine1,
cCity,
CStateCode,
cZipCode,
iUnitsAvailable,
iRegOpsNumber,
cOpsName,
iRegionID,
cRegionName)
select
h2.iHouse_ID,
h.cName,
h.cNumber,
h.cPhoneNumber1,
h.cAddressLine1,
h.cCity,
h.CStateCode,
h.cZipCode,
h.iUnitsAvailable,
r.RegOpsNumber,
r.opsname,
r.iRegion_ID,
r.regionname
from rw.fn_GetScopeHouses (@Scope, @dtReport, NULL, NULL, 0) h2
JOIN House h on h.iHouse_ID = h2.iHouse_ID
AND h.cGLsubaccount IN (select Houseid from #HouseList)
JOIN rw.vw_Reg_Ops r on r.opsareaID = h.iOpsArea_ID
它完美无缺!