【发布时间】:2011-07-26 10:13:51
【问题描述】:
问题如下:
我在 DB2 中有一个接收几个参数并进行过滤的过程。
其中一个参数是类似语句所需的 VARCHAR。在 SQLServer 中,我只是像 '%'+prmName+'%' 这样加入字符串,自然这在 DB2 中不起作用,需要替换为 '%'||prmName||'%'。
但随后弹出此错误:
DB2 Database Error: ERROR [42824] [IBM][DB2/NT] SQL0132N A LIKE predicate or POSSTR scalar function is not valid because the first operand is not a string expression or the second operand is not a string. A LOCATE or POSITION scalar function is not valid because the first operand is not a string or the second operand is not a string expression. LINE NUMBER=6. SQLSTATE=42824
在谷歌搜索错误后,我发现 DB2 不支持 LIKE 语句中的列名和参数,等等。
由于我无法更改应用程序的代码,但我需要将 SQLServer 过程传输到 DB2,因此感谢任何帮助以找到解决方法。
这是 SQLServer 中的代码
CREATE PROCEDURE [DATABYUSERNAME]
(
@prmQuestionnaireId int,
@prmStartDate DateTime,
@prmEndDate DateTime,
@prmUserName nvarchar(100)
)
AS
SELECT * from ExecutedQuestionnaire EQ left outer join SecurityUserTable SUT
on EQ.CreatedBy = SUT.UserId
where EQ.CreationDate between @prmStartDate and @prmEndDate
and SUT.Name LIKE '%'+@prmUserName+'%'
and EQ.QuestionnaireId = @prmQuestionnaireId
RETURN
这是 DB2 中 select 语句的尝试:
BEGIN ATOMIC
DECLARE bla VARCHAR(100);
DECLARE search VARCHAR(100);
set bla = 'sup';
SELECT EQ.EXECUTEDQUESTIONNAIREID,EQ.EXECUTEDQUESTIONNAIRESTATUSID,EQ.CREATIONDATE,EQ.CREATEDBY,EQ.ISCOMPLETE,EQ.QUESTIONNAIREID,
SUT.NAME, SUT.USR
FROM EXECUTEDQUESTIONNAIRE EQ
LEFT OUTER JOIN SECURITYUSERTABLE SUT
ON EQ.CREATEDBY = SUT.USERID
WHERE EQ.CREATIONDATE BETWEEN '2010-12-01' and '2011-12-01'
AND UPPER(SUT.NAME) LIKE Upper(CAST('%'||bla||'%' as VARCHAR(100)))
AND EQ.QUESTIONNAIREID = 1;
END
【问题讨论】:
标签: sql-server stored-procedures db2