how to accept a databasename in the input parameters of a procedure -
12-15-2011
, 12:40 AM
hi,all:
I had used mysql procedure many times before, while how to accept
databasename in the inputparams stll is puzzled.
for instance:
create procedure sync_data_from_old_db(old_db varchar(10), new_db
varchar(10))
insert into old_db.[tablename]
select new_db.[tablename]
end;
I had made some tries, but still failed.
# 1: failed
CREATE PROCEDURE sync_database(db_old varchar(64),db_new varchar(64))
BEGIN
set @db_old=db_old
set @db_new=db_new
INSERT INTO
@db_new.T_XXT_USER(xxt_uid,login,password,name,mob ile,group_id,timestamp,service_id,status,optype,re mind_flag,plan_id)
SELECT
xxt_uid,login,password,name,mobile,group_id,timest amp,service_id,status,
optype,remind_flag,plan_id
FROM @db_old.T_XXT_USER
ON DUPLICATE KEY
UPDATE xxt_uid = VALUES (xxt_uid),
login = VALUES(login),
password = VALUES(password),
name = VALUES(name),
mobile = VALUES(mobile),
group_id = VALUES(group_id),
timestamp = VALUES(timestamp),
service_id = VALUES (service_id),
status = VALUES(status),
optype = VALUES(optype),
remind_flag = VALUES (remind_flag),
plan_id = VALUES(plan_id);
END
//
2: I have once did something about prepare sql query like below, but i
find it so ugly and do not like to use it.
CREATE PROCEDURE copy_sms_log(m_cmpp_id BIGINT(20),num bigint(20))
BEGIN
DECLARE copy_sql varchar(1000) ;
set copy_sql = "insert into T_SMS_LOG
(cmpp_id,mobile,content,inserttime,fetchtime,categ ory,state)
select cmpp_id,mobile,content,inserttime,fetchtime,catego ry,state
from T_SMS_LOG where cmpp_id = ? limit ?";
SET @SQUERY=copy_sql;
SET @fvar=m_cmpp_id;
SET @svar=num;
PREPARE STMT FROM @SQUERY;
EXECUTE STMT using @fvar,@svar;
END
//
Any reply is appreciated Thanks
-- Jia Xiaolei |