【问题标题】:datetime as a parameter of stored proceduredatetime 作为存储过程的参数
【发布时间】:2022-01-18 19:13:06
【问题描述】:

我有这个带有存储过程Read_records_from_to的代码sn-p

        cleaned_data = from_to_form.cleaned_data
        with connections["mssql_database"].cursor() as cursor:
            cursor.execute("Read_records_from_to '2021-12-01 07:55:39.000', '2021-12-14 07:55:39.000'")
            result = cursor.fetchall()


class FromToForm(Form):
    start_date = DateField(widget=AdminDateWidget())
    start_time = TimeField(widget=AdminTimeWidget())
    end_date = DateField(widget=AdminDateWidget())
    end_time = TimeField(widget=AdminTimeWidget())

存储过程接受参数from_datetimeto_datetime。我想分配取自FromtoForm 的值。我该怎么做?

我试过了

start = datetime.combine(from_to_form.cleaned_data['start_date'], from_to_form.cleaned_data['start_time']).utcnow().isoformat()
end = datetime.combine(from_to_form.cleaned_data['end_date'], from_to_form.cleaned_data['end_time']).utcnow().isoformat()
context['start'] = start
with connections["mssql_database"].cursor() as cursor:
   cursor.execute("EXEC Readrecords_from_to @dt_from='start' ,@dt_to='end'")
   result = cursor.fetchall()

根据this 的回答。但它以错误结束

Django Version: 2.2.4
Exception Type: DataError
Exception Value:    
('22007', '[22007] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string. (241) (SQLExecDirectW)')

【问题讨论】:

    标签: python python-3.x django datetime stored-procedures


    【解决方案1】:

    错误出现在execute 语句中。这是正确的代码。

    start = datetime.combine(from_to_form.cleaned_data['start_date'], from_to_form.cleaned_data['start_time']).isoformat()
    end = datetime.combine(from_to_form.cleaned_data['end_date'], from_to_form.cleaned_data['end_time']).isoformat()
    with connections["mssql_database"].cursor() as cursor:
      cursor.execute("EXEC Read_records_from_to @dt_od='%s', @dt_do='%s'" % (start, end))
      result = cursor.fetchall()
      context['result'] = result
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      相关资源
      最近更新 更多