mysql 动态传入表名参数的存储过程写法

原文链接:http://www.cnblogs.com/xuehuashanghe/p/9487043.html
delimiter //
create procedure oneKey(in newName varchar(250),in oldName varchar(250),in idNum INT)
BEGIN	
	SET @sqlStmt = CONCAT('insert into ',newName,' (`name`,`age`,`sex`,`major`,`pass`,`photo`)
		select `name`,`age`,`sex`,`major`,`pass`,`photo` from ',oldName,' where id = ',idNum);
	PREPARE stmt FROM @sqlStmt;
	EXECUTE	stmt;
END;
//
delimiter ;

call oneKey('stu1','student',5);

begin,end中的sql语句,无法通过“+”号拼接字符串直接识别表名(where条件后的参数可以通过“+”号拼接,能识别!前提是不在concat()方法里面写!),所以需要使用 concat() 方法,在括号里面写sql语句,拼接处使用“,”逗号来拼接,同时切记一点:“拼接处要留有空格!!”比如 concat('insert into ') 这里into后要留有空格,否则无法正常识别传进来的表名!

转载于:https://www.cnblogs.com/xuehuashanghe/p/9487043.html

展开阅读全文
博主设置当前文章不允许评论。

动态表名存储过程

06-30

想用存储过程来向SQL 2008数据库中插入数据,但是表名不定,所以把表名作为参数传入,由于初次写存储过程,搞了一上午也没弄成功,老是报IDispatch error #3092错误,有会的帮忙看看什么问题,多谢。rn存储过程:rn[code=c]rnCREATE PROCEDURE insert_defectrn@table_name nvarchar(255),rn@coil_id int, rn@class int, rn@suface int, rn@xpos int,rn@ypos int,rn@area int,rn@defltime nvarchar(255)rn ASrn declare @strSQL nvarchar(1000)rn declare @param1 nvarchar(64)rn declare @param2 nvarchar(64)rn declare @param3 nvarchar(64)rn declare @param4 nvarchar(64)rn declare @param5 nvarchar(64)rn declare @param6 nvarchar(64)rn set @param1 = @coil_idrn set @param2 = @classrn set @param3 = @sufacern set @param4 = @xposrn set @param5 = @yposrn set @param6 = @arearn select @strSQL = N'insert into '+@table_name+N'(COIL_ID ,CLASS ,SURFACE ,XPOS ,YPOS ,AREA ,DEFECT_TIME) values('+@param1+N','+@param2+N','+@param3+N','+@param4+N','+@param5+N','+@param6+N','+@defltime+N'); select SCOPE_IDENTITY()'rn exec(@strSQL)rnGOrn[/code]rnrn我在T-SQL里写入execute insert_defect @table_name='defect_table0',@coil_id=1,@class=0,@suface=1,@xpos=2,@ypos=3,@area=100,@defltime='2013-02-19'是可以成功的,但是用ADO调用老是不行:rn[code=c]rn tryrn rn _CommandPtr _pCmd; rn _pCmd.CreateInstance("ADODB.Command");//__uuidof(Command)); rn _pCmd->ActiveConnection=pCnn; rn _pCmd->CommandType=adCmdStoredProc; rn _pCmd->CommandText=_bstr_t("insert_defect"); rnrnrn _ParameterPtr pParam;rn pParam.CreateInstance("ADODB.Parameter");rnrn pParam=_pCmd->CreateParameter("tab_name",adVarChar,adParamInput,255,_variant_t(_bstr_t(strTable))); rn _pCmd->Parameters->Append(pParam); rnrn pParam=_pCmd->CreateParameter("coil_id",adInteger,adParamInput,sizeof(int),(_variant_t)(int)m_dwCurCoilID); rn _pCmd->Parameters->Append(pParam); rnrn pParam=_pCmd->CreateParameter("class",adInteger,adParamInput,sizeof(int),(_variant_t)lpInfo->uType); rn _pCmd->Parameters->Append(pParam); rnrn pParam=_pCmd->CreateParameter("suface",adInteger,adParamInput,sizeof(int),(_variant_t)lpInfo->uiFace); rn _pCmd->Parameters->Append(pParam); rnrn pParam=_pCmd->CreateParameter("xpos",adInteger,adParamInput,sizeof(int),(_variant_t)lpInfo->uPositionFromL); rn _pCmd->Parameters->Append(pParam); rnrnrn pParam=_pCmd->CreateParameter("ypos",adInteger,adParamInput,sizeof(int),(_variant_t)lpInfo->uPositionFromH); rn _pCmd->Parameters->Append(pParam); rnrn pParam=_pCmd->CreateParameter("area",adInteger,adParamInput,sizeof(int),(_variant_t)lpInfo->uiErea); rn _pCmd->Parameters->Append(pParam); rnrn pParam=_pCmd->CreateParameter("deftime",adVarChar,adParamInput,255,_bstr_t(_T("1989-02-19 02:03:04"))); rn _pCmd->Parameters->Append(pParam); rn rn _variant_t vNull; rn vNull.vt=VT_ERROR; rn vNull.scode=DISP_E_PARAMNOTFOUND; rn _pCmd->Execute(&vNull,&vNull,adCmdStoredProc); //这里报错误rn pParam.Release();rn catch (_com_error e)rn rn TRACE("InsertNewDefect error:%S\n",e.ErrorMessage());rn rn[/code] 论坛

没有更多推荐了,返回首页