VBA将日期参数传递给SQL Server存储过程

时间:2016-10-10 15:20:15

标签: excel sql-server-2008 parameter-passing

我遇到了从EXCEL VBA代码传递给SQL Server存储过程的日期参数的问题。

存储过程采用两个日期参数"dd/mm/yyyy""yyyy-mm-dd"。从Excel我想用从单元格中获取的日期值执行此过程。

问题在于,无论我在excel单元格中做什么格式更改,从单元格到过程的日期都以"yyyy-mon-dd"格式传递。 SQL过程接受格式"yyyy/mm/dd""dd-mon-yyyy"Private Sub CommandButton1_Click() Dim FromDate As Date Dim ToDate As Date FromDate = Format(Sheets("Sheet1").Range("B1").Value, "yyyy-mm-dd") 'Pass value from cell B1 to SellStartDate variable ToDate = Format(Sheets("Sheet1").Range("B2").Value, "yyyy-mm-dd") 'Pass value from cell B2 to SellEndDate variable MsgBox FromDate MsgBox ToDate 'Pass the Parameters values to the stored procedure used in the data connection With ActiveWorkbook.Connections("TestConnection").OLEDBConnection .CommandText = "EXEC dbo.spr_TestProcedure '" & FromDate & "','" & ToDate & "'" ActiveWorkbook.Connections("TestConnection").Refresh End With End Sub. android { compileSdkVersion 24 buildToolsVersion '24.0.2' defaultConfig { applicationId "com.software.praescient.socialdev" minSdkVersion 12 targetSdkVersion 24 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.2.1' testCompile 'junit:junit:4.12' compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8' compile 'com.android.support:percent:24.2.1' }

该程序引发了错误:

  

“从字符转换日期和/或时间时转换失败   字符串“表示从excel传递的日期(格式为”dd / mm / yyyy“

我使用的vba代码是(参考https://www.mssqltips.com/sqlservert...to-sql-server/

class ActivitySerializer(serializers.ModelSerializer):
    class Meta:
       model = Activity

class RouteOrderingSerializer(serializers.ModelSerializer):
    activity = ActivitySerializer()
    class Meta:
        model = RouteOrdering
        fields = ('id','activity','day','order','route','activity')

    def create(self, validated_data):
        routeordering = RouteOrdering.objects.create(**validated_data)
        return routeordering

有没有办法以SQL Server程序接受的格式传递日期?如果没有其他选项,那么我将必须选择将我的过程参数类型设置为varchars并将它们转换为日期。

感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

您可以将日期作为 char() 传递给 sp 并将其转换为日期。