2012-05-20 114 views
1

我正在使用MySql,Delphi。 下面的代碼有什麼問題?'where子句'中的未知列'x'

我在'where子句'中得到「Unknown Column'x' Error - >未知列'_code_ehda_konandeh' 我很需要它。 我不得不這樣做凸出2天后 請........


DELIMITER $$ 

USE `bimarestan`$$ 

DROP PROCEDURE IF EXISTS `Save_Ehda_Konande`$$ 

CREATE DEFINER=`root`@`localhost` PROCEDURE `Save_Ehda_Konande`(
IN _code_ehda_konandeh VARCHAR(50), 
IN _tarikh_moarefi VARCHAR(50), 
IN _tarikh_tashkil_parvandeh VARCHAR(50), 
IN _name VARCHAR(50), 
IN _name_khanevadegi VARCHAR(50), 
IN _name_pedar VARCHAR(50), 
IN _tarikh_tavalod VARCHAR(20), 
IN _tahsilat VARCHAR(50), 
IN _vaziyat_taahol VARCHAR(50), 
IN _noe_bimeh VARCHAR(50), 
IN _gorooh_khoon VARCHAR(50), 
IN _rezayat_dahandeh VARCHAR(50), 
IN _shoghl VARCHAR(50), 
IN _address VARCHAR(50), 
IN _telphone VARCHAR(50), 
OUT _out INT) 
BEGIN 
IF LTRIM(RTRIM(_code_ehda_konandeh))<>'' THEN 
BEGIN 
SET _out = 0; 
    IF NOT EXISTS(SELECT * FROM bimar WHERE `_code_ehda_konandeh` = `code_ehda_konandeh`) THEN 
    INSERT INTO `ehda_konandeh` (
code_ehda_konandeh, 
tarikh_moarefi, 
tarikh_tashkil_parvandeh, 
NAME, 
name_khanevadegi, 
name_pedar, 
tarikh_tavalod, 
tahsilat, 
vaziyat_taahol, 
noe_bimeh, 
gorooh_khoon, 
rezayat_dahandeh, 
shoghl, 
address, 
telphone) 
    VALUES(
_code_ehda_konandeh, 
_tarikh_moarefi, 
_tarikh_tashkil_parvandeh, 
_name, 
_name_khanevadegi, 
_name_pedar, 
_tarikh_tavalod, 
_tahsilat, 
_vaziyat_taahol, 
_noe_bimeh, 
_gorooh_khoon, 
_rezayat_dahandeh, 
_shoghl, 
_address, 
_telphone 
); 
    ELSE 
    SET _out = 1; 
    END IF; 
    END; 
END IF; 
END$$ 

DELIMITER ; 

該存儲過程運行MySQL的,沒有任何問題,但是當我想送德爾福參數對此,我得到那個錯誤!

,這是我的Delphi代碼:

if flag_new_edit = 1 then 
    sp_sabt.StoredProcName := 'Save_Ehda_Konande' 
else 
    sp_sabt.StoredProcName := 'Edit_Ehda_Konande'; 
    With sp_sabt Do 
    Begin 
    ParamByName('_code_ehda_konandeh').Value := Trim(txt_code_ehda_konande.Text); 
    ParamByName('_tarikh_moarefi').Value := Trim(txt_tarikh_tavalod.Text); 
    ParamByName('_tarikh_tashkil_parvandeh').Value := Trim(txt_name_khanevadegi.Text); 
    ParamByName('_name').Value := Trim(txt_name.Text); 
    ParamByName('_name_khanevadegi').Value := Trim(txt_name_khanevadegi.Text); 
    ParamByName('_name_pedar').Value := Trim(txt_name_pedar.Text); 
    ParamByName('_tarikh_tavalod').Value := Trim(txt_tarikh_tavalod.Text); 
    ParamByName('_tahsilat').Value := Trim(cmb_tahsilat.Text); 
    ParamByName('_vaziyat_taahol').Value := Trim(cmb_vaziyat_taahol.Text); 
    ParamByName('_noe_bimeh').Value := Trim(cmb_noe_bime.Text); 
    ParamByName('_gorooh_khoon').Value := Trim(cmb_gorooh_khoon.Text); 
    ParamByName('_rezayat_dahandeh').Value := Trim(txt_rezayat_dahande.Text); 
    ParamByName('_shoghl').Value := Trim(txt_shoghl.Text); 
    ParamByName('_address').Value := Trim(txt_adress.Text); 
    ParamByName('_telphone').Value := Trim(txt_tel.Text); 
    ExecProc; 
End; 

問候

回答

3

雖然我指出了你的sql代碼中可能出現的錯誤,但我想詳細說明一下。

That Stored Procedure Runing at MySql WithOut any problems but when send Delphi parameters to sp , i get that Error !!但是你錯了。

您的程序創建中有a known bug。由於運行時異常的原因導致
Semantics of Stored procedure code is not checked at CREATE time. At runtime, undeclared variables are detected, and an error message is generated for each reference to an undeclared variable. However, SP's seem to believe any reference denotes a column, even though the syntactic context excludes that. This leads to a very confusing error message in case the procedure.

標準的測試示例如下圖所示:

mysql> drop procedure proc_test; 
Query OK, 0 rows affected (0.00 sec) 

mysql> delimiter // 
mysql> CREATE PROCEDURE proc_test() 
    -> BEGIN 
    ->  select current_day; 
    -> END 
    -> // 
Query OK, 0 rows affected (0.00 sec) 

在這裏你可以瞭解忽略什麼current day是程序的編寫。

mysql> delimiter ; 
mysql> call proc_test(); 
ERROR 1054 (42S22): Unknown column 'current_day' in 'field list' 
mysql> 

有了這個,你應該明白,That Stored Procedure Runing at MySql WithOut any problems ...是不正確的。


快速修復您的查詢將解決問題。你提到I defined input parameters with _ prefix. I don't know waht i must do !。如果這是真的,那麼
變化

SELECT * FROM bimar WHERE `_code_ehda_konandeh` = `code_ehda_konandeh` 

SELECT * FROM bimar WHERE `code_ehda_konandeh` = _code_ehda_konandeh 

,它應該是工作。這裏我假設code_ehda_konandeh也是表bimar的一列。

3
IF NOT EXISTS(SELECT * FROM bimar WHERE `_code_ehda_konandeh` = `code_ehda_konandeh`) THEN 
              ^---    ^--- 

卸下指示的報價。反引號用於「逃避」保留字。他們還強制MySQL將那些被轉義的單詞當作字段/表名。你的參數不是字段,所以MySQL被迫像這樣誤解它們。

+0

該存儲過程運行在MySql WithOut任何問題,但當發送德爾福參數sp,我得到錯誤! –

+0

@hamedkamrava - 絕對應該是錯誤。 '_code_ehda_konandeh'必須是過程的輸入參數,'code_ehda_konandeh',*沒有下劃線作爲前綴*,是列名稱。我對錯誤堆棧跟蹤有疑問。向我們展示'desc bimar'的結果。 –

+0

@Ravinder - 我用_前綴定義了輸入參數。我不知道,我必須這樣做! –