asp.net - OLEDB query to SQL Server fails -


i have 2 sql queries:

a.

select (upper(rtrim(ltrim(lastname))) + upper(rtrim(ltrim(firstname))) +       upper(rtrim(ltrim(middlename))) + rtrim(ltrim(v)) ) usercomparestr  atable ; 

and

b.

select (upper(rtrim(ltrim(lastname))) + upper(rtrim(ltrim(firstname))) +        upper(rtrim(ltrim(middlename))) + rtrim(ltrim(v)) ) usercomparestr   atable  usercomparestr='gapyle1111' ; 

i have following code:

    dim sql string     dim conn oledbconnection     dim cmd oledbdataadapter     dim ds dataset     dim tbl datatable      conn = " here "     cmd = new oledbdataadapter(sql, conn)     ds = new dataset     cmd.fill(ds)     tbl = new datatable     tbl = ds.tables(0) 

near can tell seems work when sql set string a, not when it's set string b.

this leads me suspect there wrong clause usercomparestr='gapyle1111'

can not use alias usercomparestr in way? can't find examples of kind of use, find analogous use when alias used table name -- , don't see against kind of us.

you have 3 options.

1) repeat did in select in where

select (upper(rtrim(ltrim(lastname))) + upper(rtrim(ltrim(firstname))) +        upper(rtrim(ltrim(middlename))) + rtrim(ltrim(v)) ) usercomparestr  atable     (upper(rtrim(ltrim(lastname))) + upper(rtrim(ltrim(firstname))) +        upper(rtrim(ltrim(middlename))) + rtrim(ltrim(v)) ) ='gapyle1111' ; 

2) use common table expression

with cte  (select (upper(rtrim(ltrim(lastname))) + upper(rtrim(ltrim(firstname))) +        upper(rtrim(ltrim(middlename))) + rtrim(ltrim(v)) ) usercomparestr  atable  ) select usercomparestr cte usercomparestr = 'gapyle1111'; 

3) inline query see maziar taheri's answer

as aside hope 'gapyle1111' doesn't come user input, otherwise you're exposing sql injection attacks. use parameterized queries instead


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -