標籤:tco tab mssql 過程 div varchar parentId play mss
pymssql調用sqlserver預存程序帶output 參數
直接上代碼
def api_adddept(): data = json.loads(request.get_data()) userdata={} with UseDataBaseSQLServer() as cursor: msg=cursor.callproc(‘[AppData].[dbo].[Fyit_AddDept]‘, (data["parentdept_add"],data["deptname_add"],1,pymssql.output(str),pymssql.output(str))) userdata[‘result_code‘]=msg[3] userdata[‘result_msg‘]=msg[4] # sql=‘DECLARE @return_value int,@result_code nvarchar(1024),@result_msg nvarchar(1024);‘\ # ‘EXEC @return_value = [AppData].[dbo].[Fyit_AddDept] @parentid = {0},@dept = N\‘{1}\‘,@display = 1,@result_code = @result_code OUTPUT,@result_msg = @result_msg OUTPUT;‘\ # ‘SELECT @result_code as N\‘@result_code\‘,@result_msg as N\‘@result_msg\‘,@return_value as N\‘Return Value\‘;‘ # cursor.execute(sql.format(data["parentdept_add"],data["deptname_add"])) # result = cursor.fetchone()
# while result: # userdata[‘resultcode‘]=str(result[0]) # userdata[‘resultmsg‘]=result[0] # userdata[‘returnvalue‘]=str(result[2]) # break resp = jsonify(userdata) resp.status_code = 200 return resp
其上兩種寫法都可行。
第一種簡單,方便。第二種還要自己拼SQL。
pymssql調用sqlserver預存程序帶output 參數