問題如下: 我在DB2中有一個過程,它接收幾個參數並執行過濾。 其中一個參數是一個VARCHAR,它需要一個類似的語句。在SQLServer中,我只需像'%'+prmName+'%'
那樣加入字符串,這當然在DB2中不起作用,需要用'%'||prmName||'%'
替換。 但後來這個錯誤彈出:DB2在類似聲明中使用參數
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
後谷歌-ING我發現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 '%'[email protected]+'%'
and EQ.QuestionnaireId = @prmQuestionnaireId
RETURN
代碼這是select語句的DB2的嘗試:
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
@Drakche:Then this ?:'AND UPPER(SUT.NAME)LIKE Upper('%'|| CAST(bla as CHAR(3))||'%')' –
'set bla =' %SUP%';'不能使用,因爲我需要傳遞值。 'SUP'是虛擬值。但第一個解決方案起作用。謝謝。 – Drakche
+1,這也解決了我的問題。 – 2013-06-24 10:03:08