存储过程
百度百科上解释为:
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语集, 存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象。
```
存储过程
ALTER procedure [dbo]. [mysp]
@Station varchar ( 50),
@SN varchar ( 50),
@Info varchar ( 500) output ,
@Msg varchar ( 500) output
```
```
使用adodbapi
from adodbapi import connect
server = 'dbserver'
user = 'username'
password = 'password'
database = 'database'
sp = 'sp'
station = 'station'
sn = 'sn'
try :
db = connect('Provider=SQLOLEDB.1 ;Data Source=%s;Initial Catalog=%s;/
User ID=%s;Password=%s;'%(server, database, user, password))
except Exception, e:
print e
else :
cur = db.cursor()
msg = cur.callproc(sp, (station, sn)) #参数与存储过程有关
if len(msg) > 1 :
if msg[-1 ] is None :
print 'sn is ok. Can be test at this station'
else :
print msg[-1 ]
finally :
try : db.close()
except : pass
```
``` 2. 使用pymssql
from pymssql import connect
server = 'dbserver'
user = 'user'
password = 'password'
database = 'database'
sp = 'sp'
station = 'station'
sn = 'sn'
sql = ['set nocount on' ]
sql.append('declare @Msg varchar(500)' )
sql.append('declare @return_value varchar' )
sql.append("exec @return_value = %s @Station = '%s', @SN = '%s', @Info = '@Info', @Msg = @Msg output" )
sql.append('select @Msg, @return_value' )
sql = '/n' .join(sql) % (sp, station, sn)
def ffchk(server, user, password, database, sql):
try :
db = connect(host = server,
database = database,
user = user,
password = password,
login_timeout = 10 )
cur = db.cursor()
cur.execute(sql)
except Exception, e:
print e
else :
cur.nextset() # 要加上这句才能通过fetch函数取到值 注意:这语句在Python2.7对应的pymssql版本中是错误。。。
print cur.fetchone()
finally :
try : db.close()
except : pass
```