Re: Datumsformat im SQL String

From: Elmar Boye (ElmarB_at_gmx.net)
Date: 01/12/05


Date: Wed, 12 Jan 2005 11:39:21 +0100

Hallo Gerald,

Gerald Aichholzer <gary@sbox.tugraz.at> schrieb ...
> Christa Kurschat wrote:
>>
>> Gerald Aichholzer <gary@sbox.tugraz.at> schrieb:
>>
>>> ist es nicht so, dass beim Verwenden von stored procedures das
>>> Datumsformat egal ist?
>>>
>>> z.B.
>>>
>>> CREATE PROCEDURE spListe ( @pDatum AS SMALLDATETIME )
>>> AS
>>> SELECT * FROM Tabelle WHERE DatumFeld < @pDATUM
>>>
>>>
>>> Und Aufruf in VBA:
>>>
>>> Dim cmd As ADODB.Command
>>> Dim rs as ADODB.Recordset
>>>
>>> Set cmd = New ADODB.Command
>>> Set cmd.ActiveConnection = CurrentProject.Connection
>>>
>>> cmd.CommandText = "spListe"
>>> cmd.CommandType = ... stored procedure ...
>>> cmd.Parameters.Add cmd.CreateParameter("pDatum", ... ,
>>> Now())
>>>
>>> rs.Open cmd
>>>
>>> Ist nicht voll funktionsfähig, soll aber als Anregung
>>> dienen.
>>>
>>
>>
>> das mag in dem speziellen Fall funktionieren, weil beides Datümmer
>> sind. Angenommen, Du nimmst nicht now(), sondern den Inhalt
>> irgendeines (ungebundenen) Eingabefeldes, dann klappts schon nicht
>> mehr, weil das Datum im Feld als String interpretiert wird und dann
>> ist es wichtig, daß der String im richtigen Format kommt. Oder Du
>> darfst als Parameter kein datetime verwenden sondern mußt in der SP
>> den String in datetime konvertieren.
>
> auf diesen Hinweis habe ich gewartet. In diesem Fall würde ich aber so
> vorgehen:
>
> Dim dtDatum As Date
>
> ...
> If Not IsDate(Me.txtDatum.Value) Then
> dtDatum = Date() ' oder Fehlermeldung an den Benutzer
> Else
> dtDatum = CDate(Me.txtDatum.Value)
> End If
>
> ...
> cmd.Parameters.Add cmd.CreateParameter("pDatum", ..., dtDatum)
>

Die von mir immer gepredigte Variante.

>
> Vorteile meiner Variante:
>
> - der Benutzer kann das Datum im lokal eingestellten Format
> eingeben (IsDate() und CDate() erkennen das)
> - das am SQL Server eingestellte Datumsformat ist egal

eben.

>
> Nachteile meiner Variante:
>
> - es muss eine SP erstellt werden

Nein, muss nicht. Parameter können überall verwendet werden.
Entweder positionell via "?", z. B.
    SELECT * FROM Tabelle WHERE Datum = ?

Beim SQL Server und MDAC 2.6 (und höher) geht es sogar mit
benannten Parametern. Dazu für das Command-Objekt
    cmd.NamedParameters = True
einstellen und
    SELECT * FROM Tabelle WHERE Datum = @pDatum
verwenden, wobei der Parameter dann auch mit "@" Zeichen
eingeleitet werden muss.

> - mehr VBA-Code (in Zeilen)

Für Fehlertoleranz ein geringer Preis...

> - "komplexer" als einen SQL-String zusammenzubauen

... naja, wenn die SQL Anweisungen komplexer werden, ist
eine String-Orgie ;-) auch nicht das Wahre.

Und ein Vorteil soll noch genannt werden: Beim Verwenden von
Parametern kann der SQL Server die Befehle wiederverwenden,
da Parameter über sp_executesql übergeben werden.

Gruss
Elmar



Relevant Pages

  • Re: Problems with DAO and SQL Server
    ... String, Long or Date/Time. ... Will look at what is possible in the SQL string ... SQL or using TRIMin your query (if available, not familiar with SQL Server) ... Dim cnn As DAO.Connection ...
    (microsoft.public.excel.programming)
  • Re: How to look at parameter string
    ... I am tracing, but the problem I can't trace (at least, I haven't figured out ... string it is putting together to send to Sql. ... >> The problem is not coming from Sql Server but from my page. ...
    (microsoft.public.dotnet.framework.adonet)
  • RE: Invalid cursor state when using PRINT in MSSQL
    ... I'm an Oracle not a Sybase or SQL Server person, ... The server and all the SP's I am running this on is MSSQL and not Sybase. ... The PRINT statement takes either one character or a Unicode string ...
    (perl.dbi.users)
  • Re: ASP .Net connection to Sql Server
    ... check that the port number the sql box is using is the opened port on the ... It is connecting via a connection string similiar to: ... | string to our production SQL server and everything runs fine. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Problems with Delete Command
    ... The SQL could get fairly messy if you need to construct it in code, ... ContactID, and WebComID, and create your on-the-fly SQL on that saved query, ... to find the list of ContactIDs from the junction table, ... This is a style/readability thing: if you are going to use string ...
    (microsoft.public.access.tablesdbdesign)

Quantcast