Re: Using variables in Dynamic SQL

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 08/31/04


Date: Tue, 31 Aug 2004 16:56:57 +0200

On 31 Aug 2004 06:36:05 -0700, rzirpolo wrote:

>When attempting to run the following query to generate and then
>execute dynamic SQL I am getting the error message "Syntax error
>converting datetime from character string."
>
(snip code)

>I realise what is causing this are the lines,
>
> and (b.departureDateTime >= '''+ @startdate +''')
> and (b.departureDateTime < '''+ dateadd(day,1,@endDate) +''')

Wrong. This is caused by "SELECT @EndDate = '31/12/04'". I don't know why
you insist on using this format when I've already told you twice that it
doesn't work.

Another thing that has already been told you is that you also need to use
the CONVERT function in this piece of the code to format the date back to
YYYYMMDD format. I honestly don't know why you ask here for advise, then
choose to ignore the advise you get and simply repost the problem.

>Can anybody tell me what the correct format should be ?
>
>But if I simply remove these clauses and run the statement I get the
>error "Line 2: Incorrect syntax near '31/12/2004'."
>
>Which means it doesn't like the format of the select statement,
>
>select
> ''Departing from ''+ '''+convert(char(10),@startdate,103)+''' '' to
>'' '''+convert(char(10),@enddate,103)+''' as ''ReportString''
>
>So my 2nd query is can anybody advise me of the correct format here.

If you print the resulting string in @sql, you'll see that you have some
unmatched single quotes and some missing + signs. See below for a greatly
simplified version that won't cause errors.
The dates don't cause any problem here - if you have corrected the initial
assignment to @EndDate. If that assignment is wrong, it won't be executed,
@EndDate will remain NULL and will cause extra errors in this line.

>I need to use dynamic SQL for this so I am only looking for a
>assistance in regards to amending the text populated in the @sql
>variable.

I'm not so sure that you really need dynamic SQL. Have a look at Erland's
site that gives some alternatives AND warns you about the danger of SQL
injection attacks. http://www.sommarskog.se/dynamic_sql.html

That being said - the following seems to work (though I don't have a
bookings table on my system to test it - but it does pass the syntax
check).

DECLARE @sql varchar(5000)
DECLARE @StartDate datetime
DECLARE @EndDate datetime
DECLARE @Media nvarchar(500)
DECLARE @Analysis nvarchar(500)

SELECT @StartDate = '20000701' --'01/07/00'
SELECT @EndDate = '20041231' --'31/12/04'
SELECT @Media = '1, 2, 3'
SELECT @Analysis = '%'

set @sql =
        'select
        ''Departing from '+convert(char(10),@StartDate,103)+' to
'+convert(char(10),@EndDate,103)+''' as ''ReportString''

        from booking as b

        where (b.disabled = 0)
        and (b.leadsector = 1)
        and (b.mediaid in ('+ @Media +'))
        and (b.analysiscodeid like '''+ @Analysis +''')
        and (b.departureDateTime >= '''+ convert(char(8),@StartDate,112)
+''')
        and (b.departureDateTime < '''+
convert(char(8),dateadd(day,1,@EndDate),112) +''')'

print(@sql)
exec(@sql)

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • Re: Date Problems - ASP/SQL
    ... a DATETIME column does NOT store DD/MM/YYYY format. ... DECLARE @dt SMALLDATETIME ... data> type to a datetime data type resulted in an out-of-range datetime value. ...
    (microsoft.public.inetserver.asp.general)
  • Re: convert a date to a string?
    ... converting it to a CHARACTER string. ... Also, don't convert it back to a datetime, or put it in a datetime variable, ... or you'll lose the format. ... >>> Declare @m int ...
    (microsoft.public.sqlserver.programming)
  • RE: SqlCeEngine.CreateDataBase() - LCID - Datepart format problem
    ... Background info on why insert in your format doesn't work: ... datetime as string, SQL CE does an implicit convert from string to ... datetime formats with their style IDs are listed in CONVERT function in SQL ...
    (microsoft.public.sqlserver.ce)
  • Re: VB & SAL Date/Time Problem?
    ... you don't need to be concerned with datetime ... SQL Server MVP ... > I've written an ASP app that requires users to login - normal stuff. ... > So I believe that what I need to do is to convert the date time format ...
    (microsoft.public.sqlserver.programming)
  • Re: Saving to a DateTime field in Sql Server 2000 using ADO.Net
    ... If you really must pass a DateTime as a string, ... of SQL server's culture netural formats. ... >I have a Datetime field in a Sql Server 2000 database, ... as the 'm/d/y' date format appears to be used when Sql ...
    (microsoft.public.dotnet.framework.adonet)