Re: Problem with dynamic SQL syntax
From: Steen Persson (SPE_at_REMOVEdatea.dk)
Date: 07/20/04
- Next message: Ezekiël: "create login for existing database user"
- Previous message: Kaye: "Restore log failed SQL 7"
- In reply to: Aaron [SQL Server MVP]: "Re: Problem with dynamic SQL syntax"
- Next in thread: Steen Persson: "Re: Problem with dynamic SQL syntax"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 20 Jul 2004 15:29:09 +0200
Hi Araron
I think the ' was a remicense for my playing around with the statement, so
that's not the cause of the problem- sorry for the confusion.
I do know the difference between an empty field and NULL (or at least I hope
I know...:-)...) so I'm sorry if I've made some confusions about this. The
statement actually works when I'm running it without the variables and
without the dynamic SQL. It's not until I add the variable and dyn. SQL
part, I have problems getting it to accept the ' ' in the end. I'm not a all
experienced in using dynamic SQL so I thought that it was just something
simple I was missing in the syntax.
I'll take a closer look at your suggestion to see if that will do the trick.
Thanks
Steen
"Aaron [SQL Server MVP]" <ten.xoc@dnartreb.noraa> skrev i en meddelelse
news:Of83LqlbEHA.2660@TK2MSFTNGP12.phx.gbl...
> I think you're having a comprehension problem between an empty/blank
string
> (which is a valid value) and a NULL (which is an unknown/missing value).
>
> Anyway, your syntax is wrong. It ends at ' which is the closing of the
> string, so of course the EXEC call will break.
>
> DECLARE @sql NVARCHAR(2000)
> SET @sql = N'SELECT <column_list> INTO
'+@db_name_dest+'.dbo.'+@table_name+'
> FROM '+@servername_source+'.'+@db_name_source+'.dbo.'+@table_name+' WHERE
> Date_ >= ''20010101'' AND CompanyNo IS NOT NULL AND COALESCE(RTRIM(Type),
> '') != '''''
>
> You might try getting it working as a normal statement first, then putting
> it into dynamic SQL. Remember that anytime you have a literal ' you must
> escape it so it isn't interpreted as a string terminator.
>
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
>
>
> "Steen Persson" <SPE@REMOVEdatea.dk> wrote in message
> news:OJ1CDpkbEHA.3016@tk2msftngp13.phx.gbl...
> > I' m having a problem with the syntax when I'm trying to run a dynamic
SQL
> > statement.
> >
> > The code -
> >
> > set @sql = 'SELECT * INTO '+@db_name_dest+'.dbo.'+@table_name+'
> > FROM '+@servername_source+'.'+@db_name_source+'.dbo.'+@table_name+'
> Where
> > Date_ >='+'2001-01-01'+'
> > And CompanyNo Is Not NULL And Type <>'
> > exec sp_executesql @sql
> >
> > - gives me the error "Incorrect syntax near '>'." The purpose of the
last
> <>
> > is to find the records where this field is empty (that's my
understanding
> of
> > it...). The basic structure of the query is from a DTS Transform task,
but
> > I'm trying to "convert" this whole task to TSql.
> >
> > I've tried all sorts of different combinations of <> and ' but it still
> > won't do it. If I just prins the @sql var. it looks fine.
> > The CompanyNo field is int(4) and the Type field is varchar(30).
> > Is there any other ways to check for an empty varchar field of can some
of
> > you guide me to what it is I'm missing in my "set @sql....." statement?
> >
> > Best Regards
> > Steen
> >
> >
>
>
- Next message: Ezekiël: "create login for existing database user"
- Previous message: Kaye: "Restore log failed SQL 7"
- In reply to: Aaron [SQL Server MVP]: "Re: Problem with dynamic SQL syntax"
- Next in thread: Steen Persson: "Re: Problem with dynamic SQL syntax"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|