动态SQL,数据库表名用变量表示老是出错,大侠们不吝赐教啊...

victoryl 2005-01-05 05:25:08
具体代码如下:
ls_table为表名变量
ls_sql="select max(right(move_no,3)) into :ls_move_no from "+ls_table+" where left (move_no,10) like :ls_move_no"
execute immediate :ls_sql using sqlca;
if sqlca.sqlcode<>0 then
messagebox("提示","检索"+ls_table+"数据表主键日流水号出错,代码:"+string(sqlca.sqlcode))
return
end if

修改过很多地方,SQLCA.SQLCODE返回值都是-1,高手们帮帮忙啊,快搞不定了,烦了我好几个小时了..
...全文
190 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
hygougou 2005-01-06
  • 打赏
  • 举报
回复
select max(right(move_no,3)) into :ls_move_no from 表名 where left (move_no,10) like :ls_move_no;
hygougou 2005-01-06
  • 打赏
  • 举报
回复
select max(right(move_no,3)) into :ls_move_no from "+ls_table+" where left (move_no,10) like :ls_move_no;

改成你的表名即可

直接引用

如果你的记录多于一行,返回的结果就不完整了,得用游标
victoryl 2005-01-06
  • 打赏
  • 举报
回复
万分感谢各位,特别感谢balloonman2002,你给我推荐的那个链接对我很有帮助,狗狗可能是还没明白我的意思, liuyxit的程序小生水平有限,看不懂啊。。
victoryl 2005-01-06
  • 打赏
  • 举报
回复
类似的问题又出现了,
ls_sql="insert into "+ls_table+" (move_no,cust_en,prod_no,lot_no) "+&
"values(:ls_move_no,:ls_cust_en,:ls_prod_no,:ls_craft_set);"
execute immediate :ls_sql using sqlca;
语法没有问题了啊,可就是不行,老是提示count filel incorrect 大侠们,再帮帮小生吧,解决了再加分
victoryl 2005-01-06
  • 打赏
  • 举报
回复
多谢各位,我用第三种语法尝试了一下,好像可以了,但还是有些小问题,正在调试中,,
深情感谢各位对小生的帮助,问题解决了,立马送分 结帖。。
liuyxit 2005-01-06
  • 打赏
  • 举报
回复
...
怎不试我的!?
qyqtn 2005-01-06
  • 打赏
  • 举报
回复
ls_sql="select max(right(move_no,3)) into :ls_move_no from "+ls_table+" where left(move_no,10) like '"+ls_move_no+"'"
lijian1008 2005-01-05
  • 打赏
  • 举报
回复
liuyxit(深蓝(O_0)) 的方法肯定得
zhangdatou 2005-01-05
  • 打赏
  • 举报
回复
帮顶
missj 2005-01-05
  • 打赏
  • 举报
回复
up
balloonman2002 2005-01-05
  • 打赏
  • 举报
回复
首先你要保证你的数据库支持max\right\left函数,这些可不是PB的函数,而要求是数据库的函数

再者你的脚本不能象上面的写法,必须用第三种动态SQL语句,如下格式:

integer Emp_id_var

DECLARE my_cursor DYNAMIC CURSOR FOR SQLSA ;
PREPARE SQLSA FROM "SELECT emp_id FROM employee" ;
OPEN DYNAMIC my_cursor ;
FETCH my_cursor INTO :Emp_id_var ;
CLOSE my_cursor ;

换言之不能简单的SELECT ... INTO ...,必须SELECT出来一个结果集,然后用游标取出输出值,更多用法参见:

http://cpb.cn/disdocument.asp?id=141
victoryl 2005-01-05
  • 打赏
  • 举报
回复
这是SQLCA.SQLERRTEXT出错信息:SQLSTATE = 37000
[Sybase][ODBC Driver][Adaptive Server Anywhere]Syntax error or access violation: near 'ls_move_no' in ...(move_no,3)) into :[ls_move_no] from C_KAM where...
victoryl 2005-01-05
  • 打赏
  • 举报
回复
要怎么写啊,我试过“狗狗”的方法,不行,“天策”也不行,真的不知道怎么办了。。
balloonman2002 2005-01-05
  • 打赏
  • 举报
回复
同意楼上各位,这样写肯定不对
xzxxiong 2005-01-05
  • 打赏
  • 举报
回复
ls_sql="select max(right(move_no,3)) into :ls_move_no from "+ls_table+" where left (move_no,10) like " + ls_move_no
liuyxit 2005-01-05
  • 打赏
  • 举报
回复
给个版你看看:

//////////////////////////////////////////////////////////////////////////////
// Public Function: of_DistinctValues
// 参数: as_table: 表名
// as_column: 表中的字段
// as_values: 字段串数组,返回的表中指定字段不同的值
// Returns: long - the SQLCode based on the SQL fetch
// Description: Get the distinct values from the database
//////////////////////////////////////////////////////////////////////////////

long ll_rc
string ls_sqlstatement
string ls_value
string ls_name

// Build the SQL Select statement
ls_sqlstatement = "SELECT DISTINCT " + as_column + " FROM " + as_table

// If SQLSpy service is on, add to the history
if IsValid (gnv_app) then
if IsValid(gnv_app.inv_debug) then
if IsValid (gnv_app.inv_debug.inv_sqlspy) then
ls_name = this.is_Name
if Len (ls_name) = 0 then
ls_name = this.ClassName()
end if
gnv_app.inv_debug.inv_sqlspy.of_SQLSyntax &
("Dynamic SQL using " + ls_name, ls_sqlstatement)
end if
end if
end if

// Execute the SQL
prepare sqlsa from :ls_sqlstatement using this;
describe sqlsa into sqlda;
declare c_values_cursor dynamic cursor for sqlsa;
open dynamic c_values_cursor using descriptor sqlda;
fetch c_values_cursor using descriptor sqlda;
ll_rc = this.SQLCode

// Retrieve the distinct values and add them to the array
do while this.SQLCode = 0
choose case sqlda.OutParmType[1]
case TypeString!
ls_value = GetDynamicString (sqlda, 1)
case TypeDate!
ls_value = String (GetDynamicDate (sqlda, 1))
case TypeTime!
ls_value = String (GetDynamicTime (sqlda, 1))
case TypeDateTime!
ls_value = String (GetDynamicDateTime (sqlda, 1))
case else
ls_value = String (GetDynamicNumber (sqlda, 1))
end choose

as_values[UpperBound(as_values)+1] = ls_value
fetch c_values_cursor using descriptor sqlda;
ll_rc = this.SQLCode
loop

close c_values_cursor;

return ll_rc
hygougou 2005-01-05
  • 打赏
  • 举报
回复
你这样写肯定不对,直接写
select max(right(move_no,3)) into :ls_move_no from "+ls_table+" where left (move_no,10) like :ls_move_no;

752

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧