Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

sql - How to resolve SQL0418N Error

I'm using the statement below to update/insert some data to a table and, if I run it without parameters, it's fine. However, as soon as I try to execute it using parameters it throws:

SQL0418N - A statement contains a use of an untyped parameter marker, the DEFAULT keyword, or a null value that is not valid.

I've read the error information here, but I'm still struggling with why my statement won't execute.

--This statement works
MERGE Into AB.Testing_Table A
USING (VALUES('TEST', 'P')) B(TEST_ID, "ACTION")
ON (A.TEST_ID = B.TEST_ID)
WHEN NOT MATCHED THEN
INSERT (TEST_ID, "ACTION")
VALUES ('TEST', 'P')
WHEN MATCHED THEN
UPDATE SET TEST_ID = 'TEST'
,"ACTION" = 'P';

--This statement fails with error SQL0418N
MERGE Into AB.Testing_Table A
USING (VALUES(@TEST, @ACTION)) B(TEST_ID, "ACTION")
ON (A.TEST_ID = B.TEST_ID)
WHEN NOT MATCHED THEN
INSERT (TEST_ID, "ACTION")
VALUES (@TEST, @ACTION)
WHEN MATCHED THEN
UPDATE SET TEST_ID = @Test
,"ACTION" = @Action;

Thanks in advance for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Basically, DB2 doesn't know what data types you're sending in on those parameters. I'm guessing you're either on an older version of DB2 (less than 9.7 on Linux/Unix/Windows, or on a Mainframe version older than 10.1), which doesn't do a whole lot of "automatic" type conversion. Or you're sending in NULL values (which still have to be "typed", strange as it sounds).

You can fix the problem by creating your parameter markers as typed parameters (I'm assuming data types here, use what would be appropriate):

MERGE INTO AB.TESTING_TABLE A
USING (VALUES (
          CAST(@TEST AS CHAR(4))
         ,CAST(@ACTION AS CHAR(1))
       )) B(TEST_ID, "ACTION")
   ON (A.TEST_ID = B.TEST_ID)
 WHEN NOT MATCHED THEN
     INSERT (TEST_ID, "ACTION")
     VALUES (B.TEST_ID, B.ACTION)
WHEN MATCHED THEN
     UPDATE SET "ACTION" = B.ACTION

Additionally, since you're using the MERGE, you don't have to use parameters in the UPDATE or INSERT parts, you can refer to the values in the USING table you passed in. Also, since you're matching on TEST_ID, you don't need to include that in your UPDATE statement, since it wouldn't be updated, anyway.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...