newbie asp - sql question

Ask a Question related to ASP, Design and Development.

  1. #1

    Default newbie asp - sql question

    i am trying to write a sql string that pulls a row from a dbase & the rows
    either side

    i cant use the primary key (autonumber) as some of the records have been
    deleted thus there are gaps in the numbering

    this is the sql string i have - but it doesnt work
    vid is a string passed from a form, NewsId is the primary key

    sql="SELECT * FROM tblNews t WHERE t.NewsId="& vid &" OR t.NewsId = (select
    max(t.NewsId) t.NewsId WHERE t.NewsId < "& vid &") OR t.NewsId = (SELECT
    min(t.NewsId) t.NewsId WHERE t.NewsId > "& vid &") ORDER by t.NewsId; "

    can anyone see what is wrong with the sql? i get :-
    Error Type:
    Microsoft JET Database Engine (0x80040E14)
    Syntax error. in query expression 't.NewsId=2 OR t.NewsId = (select
    max(t.NewsId) WHERE t.NewsId < 2) OR t.NewsId = (SELECT min(t.NewsId) WHERE
    t.NewsId > 2)'.
    /geoff/newsalltest.asp, line 23

    or is there a better way of doing this?

    thanks in advance

    j



    monki Guest

  2. Similar Questions and Discussions

    1. A newbie with a newbie question
      Good afternoon everyone, My Name is Dusty I am new to this forum and pretty new to Acrobat. I have Acrobat 9 running on an IMAC running 10.5.2 I...
    2. newbie question,,,
      I converted an AVI to FLV in the encoder. The resulsting file only opens a blank flash 8 player. I can't even get it to play within the flash app....
    3. Newbie Question: Biz Card Template Question
      Hi, I got the Pagemaker PlugIn - I am using one of the templates for Business Cards - the elements appear to be grouped (bound box all around when I...
    4. Pen Tool Use Question. (Embarrassingly Newbie Question)
      I'm currently using Flash MX and whenever I choose the Pen Tool instead of the pen nib with the small "x" beside it that supposed to show up on...
    5. Newbie OO question
      In article <EbkRa.331382$fC.2436421@news.easynews.com>, "Ed W" <dodgynewsgroups@ewildgoose.demon.co.uk> wrote: perldoc perltoot it's all you...
  3. #2

    Default Re: newbie asp - sql question

    "monki" <j@usura.org> wrote in message
    news:wmw0b.67$vv2.30@news-binary.blueyonder.co.uk...
    > i am trying to write a sql string that pulls a row from a dbase & the
    rows
    > either side
    >
    > i cant use the primary key (autonumber) as some of the records have
    been
    > deleted thus there are gaps in the numbering
    >
    > this is the sql string i have - but it doesnt work
    > vid is a string passed from a form, NewsId is the primary key
    >
    > sql="SELECT * FROM tblNews t WHERE t.NewsId="& vid &" OR t.NewsId =
    (select
    > max(t.NewsId) t.NewsId WHERE t.NewsId < "& vid &") OR t.NewsId =
    (SELECT
    > min(t.NewsId) t.NewsId WHERE t.NewsId > "& vid &") ORDER by t.NewsId;
    "
    >
    > can anyone see what is wrong with the sql? i get :-
    > Error Type:
    > Microsoft JET Database Engine (0x80040E14)
    > Syntax error. in query expression 't.NewsId=2 OR t.NewsId = (select
    > max(t.NewsId) WHERE t.NewsId < 2) OR t.NewsId = (SELECT min(t.NewsId)
    WHERE
    > t.NewsId > 2)'.
    > /geoff/newsalltest.asp, line 23
    >
    > or is there a better way of doing this?
    >
    > thanks in advance
    >
    > j
    [spPrevNext]
    PARAMETERS
    prmNewsId Long
    ;
    SELECT
    *
    FROM
    tblNews AS t
    WHERE
    t.NewsId = prmNewsId OR
    t.NewsID = (SELECT MAX(t.NewsId) FROM tblNews AS t WHERE t.NewsId <
    prmNewsId) OR
    t.NewsID = (SELECT MIN(t.NewsId) FROM tblNews AS t WHERE t.NewsId >
    prmNewsId)

    <%
    Dim cn,rs,arr,j,jMax
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    cn.Open "File Name=C:\SomeDirectoryOutsideTheAppRoot\MyConnectio n.UDL"
    cn.spPrevNext Request.QueryString("NewsId"), rs
    arr = rs.GetRows()
    rs.Close : Set rs = Nothing
    cn.Close : Set cn = Nothing
    jMax = UBound(arr,2)
    For j = 0 to jMax
    'Process each row here
    Next
    %>

    Notes:
    1. When possible, use parameterized queries instead of building the sql
    statement in ASP
    2. Don't use "SELECT *"
    3. Consider using a reference to a UDL file as your connection string












    Chris Hohmann Guest

  4. #3

    Default Re: newbie asp - sql question

    thanks

    "monki" <j@usura.org> wrote in message
    news:wmw0b.67$vv2.30@news-binary.blueyonder.co.uk...
    > i am trying to write a sql string that pulls a row from a dbase & the rows
    > either side
    >
    > i cant use the primary key (autonumber) as some of the records have been
    > deleted thus there are gaps in the numbering
    >
    > this is the sql string i have - but it doesnt work
    > vid is a string passed from a form, NewsId is the primary key
    >
    > sql="SELECT * FROM tblNews t WHERE t.NewsId="& vid &" OR t.NewsId =
    (select
    > max(t.NewsId) t.NewsId WHERE t.NewsId < "& vid &") OR t.NewsId = (SELECT
    > min(t.NewsId) t.NewsId WHERE t.NewsId > "& vid &") ORDER by t.NewsId; "
    >
    > can anyone see what is wrong with the sql? i get :-
    > Error Type:
    > Microsoft JET Database Engine (0x80040E14)
    > Syntax error. in query expression 't.NewsId=2 OR t.NewsId = (select
    > max(t.NewsId) WHERE t.NewsId < 2) OR t.NewsId = (SELECT min(t.NewsId)
    WHERE
    > t.NewsId > 2)'.
    > /geoff/newsalltest.asp, line 23
    >
    > or is there a better way of doing this?
    >
    > thanks in advance
    >
    > j
    >
    >
    >

    monki Guest

  5. #4

    Default Re: newbie asp - sql question

    thanks for your advice got that working cheers

    another page i am doing i am trying to acheive a similar thing but display
    one row & pull the id for links to previous / next pages. is modding the
    code you sent the best way of doing this

    ie displat the middle row pull the id's of the first and last into tnext &
    previous links

    the only problem i can see with this is catching the first & last records of
    the table & hiding the links.

    if there is a better way of doing this please just point me in the right
    direction & i will try and figure the code out myself (often frustraiting
    but the best way to learn)

    thanks in advance

    j


    "Chris Hohmann" <hohmannATyahooDOTcom> wrote in message
    news:#MhYippZDHA.2632@TK2MSFTNGP12.phx.gbl...
    > "monki" <j@usura.org> wrote in message
    > news:wmw0b.67$vv2.30@news-binary.blueyonder.co.uk...
    > > i am trying to write a sql string that pulls a row from a dbase & the
    > rows
    > > either side
    > >
    > > i cant use the primary key (autonumber) as some of the records have
    > been
    > > deleted thus there are gaps in the numbering
    > >
    > > this is the sql string i have - but it doesnt work
    > > vid is a string passed from a form, NewsId is the primary key
    > >
    > > sql="SELECT * FROM tblNews t WHERE t.NewsId="& vid &" OR t.NewsId =
    > (select
    > > max(t.NewsId) t.NewsId WHERE t.NewsId < "& vid &") OR t.NewsId =
    > (SELECT
    > > min(t.NewsId) t.NewsId WHERE t.NewsId > "& vid &") ORDER by t.NewsId;
    > "
    > >
    > > can anyone see what is wrong with the sql? i get :-
    > > Error Type:
    > > Microsoft JET Database Engine (0x80040E14)
    > > Syntax error. in query expression 't.NewsId=2 OR t.NewsId = (select
    > > max(t.NewsId) WHERE t.NewsId < 2) OR t.NewsId = (SELECT min(t.NewsId)
    > WHERE
    > > t.NewsId > 2)'.
    > > /geoff/newsalltest.asp, line 23
    > >
    > > or is there a better way of doing this?
    > >
    > > thanks in advance
    > >
    > > j
    >
    > [spPrevNext]
    > PARAMETERS
    > prmNewsId Long
    > ;
    > SELECT
    > *
    > FROM
    > tblNews AS t
    > WHERE
    > t.NewsId = prmNewsId OR
    > t.NewsID = (SELECT MAX(t.NewsId) FROM tblNews AS t WHERE t.NewsId <
    > prmNewsId) OR
    > t.NewsID = (SELECT MIN(t.NewsId) FROM tblNews AS t WHERE t.NewsId >
    > prmNewsId)
    >
    > <%
    > Dim cn,rs,arr,j,jMax
    > Set cn = CreateObject("ADODB.Connection")
    > Set rs = CreateObject("ADODB.Recordset")
    > cn.Open "File Name=C:\SomeDirectoryOutsideTheAppRoot\MyConnectio n.UDL"
    > cn.spPrevNext Request.QueryString("NewsId"), rs
    > arr = rs.GetRows()
    > rs.Close : Set rs = Nothing
    > cn.Close : Set cn = Nothing
    > jMax = UBound(arr,2)
    > For j = 0 to jMax
    > 'Process each row here
    > Next
    > %>
    >
    > Notes:
    > 1. When possible, use parameterized queries instead of building the sql
    > statement in ASP
    > 2. Don't use "SELECT *"
    > 3. Consider using a reference to a UDL file as your connection string
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >

    monki Guest

  6. #5

    Default Re: newbie asp - sql question

    thanks for your advice got that working cheers

    another page i am doing i am trying to acheive a similar thing but display
    one row & pull the id for links to previous / next pages. is modding the
    code you sent the best way of doing this

    ie displat the middle row pull the id's of the first and last into tnext &
    previous links

    the only problem i can see with this is catching the first & last records of
    the table & hiding the links.

    if there is a better way of doing this please just point me in the right
    direction & i will try and figure the code out myself (often frustraiting
    but the best way to learn)

    thanks in advance

    j


    "Chris Hohmann" <hohmannATyahooDOTcom> wrote in message
    news:#MhYippZDHA.2632@TK2MSFTNGP12.phx.gbl...
    > "monki" <j@usura.org> wrote in message
    > news:wmw0b.67$vv2.30@news-binary.blueyonder.co.uk...
    > > i am trying to write a sql string that pulls a row from a dbase & the
    > rows
    > > either side
    > >
    > > i cant use the primary key (autonumber) as some of the records have
    > been
    > > deleted thus there are gaps in the numbering
    > >
    > > this is the sql string i have - but it doesnt work
    > > vid is a string passed from a form, NewsId is the primary key
    > >
    > > sql="SELECT * FROM tblNews t WHERE t.NewsId="& vid &" OR t.NewsId =
    > (select
    > > max(t.NewsId) t.NewsId WHERE t.NewsId < "& vid &") OR t.NewsId =
    > (SELECT
    > > min(t.NewsId) t.NewsId WHERE t.NewsId > "& vid &") ORDER by t.NewsId;
    > "
    > >
    > > can anyone see what is wrong with the sql? i get :-
    > > Error Type:
    > > Microsoft JET Database Engine (0x80040E14)
    > > Syntax error. in query expression 't.NewsId=2 OR t.NewsId = (select
    > > max(t.NewsId) WHERE t.NewsId < 2) OR t.NewsId = (SELECT min(t.NewsId)
    > WHERE
    > > t.NewsId > 2)'.
    > > /geoff/newsalltest.asp, line 23
    > >
    > > or is there a better way of doing this?
    > >
    > > thanks in advance
    > >
    > > j
    >
    > [spPrevNext]
    > PARAMETERS
    > prmNewsId Long
    > ;
    > SELECT
    > *
    > FROM
    > tblNews AS t
    > WHERE
    > t.NewsId = prmNewsId OR
    > t.NewsID = (SELECT MAX(t.NewsId) FROM tblNews AS t WHERE t.NewsId <
    > prmNewsId) OR
    > t.NewsID = (SELECT MIN(t.NewsId) FROM tblNews AS t WHERE t.NewsId >
    > prmNewsId)
    >
    > <%
    > Dim cn,rs,arr,j,jMax
    > Set cn = CreateObject("ADODB.Connection")
    > Set rs = CreateObject("ADODB.Recordset")
    > cn.Open "File Name=C:\SomeDirectoryOutsideTheAppRoot\MyConnectio n.UDL"
    > cn.spPrevNext Request.QueryString("NewsId"), rs
    > arr = rs.GetRows()
    > rs.Close : Set rs = Nothing
    > cn.Close : Set cn = Nothing
    > jMax = UBound(arr,2)
    > For j = 0 to jMax
    > 'Process each row here
    > Next
    > %>
    >
    > Notes:
    > 1. When possible, use parameterized queries instead of building the sql
    > statement in ASP
    > 2. Don't use "SELECT *"
    > 3. Consider using a reference to a UDL file as your connection string
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >

    monki Guest

  7. #6

    Default Re: newbie asp - sql question

    "monki" <j@usura.org> wrote in message
    news:NNP0b.1$Ij.0@news-binary.blueyonder.co.uk...
    > thanks for your advice got that working cheers
    >
    > another page i am doing i am trying to acheive a similar thing but
    display
    > one row & pull the id for links to previous / next pages. is modding
    the
    > code you sent the best way of doing this
    >
    > ie displat the middle row pull the id's of the first and last into
    tnext &
    > previous links
    >
    > the only problem i can see with this is catching the first & last
    records of
    > the table & hiding the links.
    >
    > if there is a better way of doing this please just point me in the
    right
    > direction & i will try and figure the code out myself (often
    frustraiting
    > but the best way to learn)
    >
    > thanks in advance
    I'm glad to here you got it working. Modifying your existing code is
    certainly one way to achieve what you've described for your other page.
    You could also create two stored procedures. spArticleNext and
    spArticlePrev. spArticleNext would look something like this:

    PARAMETERS
    prmNewsId Long
    ;
    SELECT TOP 1
    *
    FROM
    tblNews AS T
    WHERE
    T.NewsId > prmNewsId
    ORDER BY
    T.NewsID ASC

    This has a number of benefits.
    1. You only return one (1) record at a time.
    2. You avoid concurrency issues. For example suppose you retrieve
    prev/current/next. While you view the page, someone else inserts a
    record in between current and next. When you click on next, you end up
    jumping right over the new record. By retrieving one record at a time,
    you can avoid this.
    3. The logic of the query in a lot more straightforward and
    consequently, more efficient.

    As for detecting the first/last record, simple check for BOF/EOF in the
    recordset. The spArticlePrev is left as an exercise for the reader. :)

    HTH
    -Chris


    Chris Hohmann Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139