Need help please...

Last post 05-19-2006 2:47 PM by Stue. 9 replies.

Sort Posts:

  • Need help please...

    05-19-2006, 11:52 AM
    • Member
      635 point Member
    • Stue
    • Member since 08-13-2002, 10:28 AM
    • Posts 145

    OK, I have a page that contains a repeater. I figure creating and then calling a stored proc to fill the repeater is the way to go.(please let me know if there is a better way) This is what im looking to do:

    My current select statement reads as follows:
    SELECT ID, Title, Topic, Summary, Dt_Article, Link FROM CMS_PRData WHERE Active=1 AND Dt_GoLive <= GETDATE() ORDER BY DT_Article DESC

    What I need to do is define Link if the value is "" or NULL. If it has data fine, if not I need to set Link to a format similar to this:
    "http://www.myserver.com/Article/article.aspx?content=2&ArticleID="ID.value"
    ID.Value will equal the value of ID from the row returned.

    This is what I have so far but its not working. Im a complete newb to the stored proc world so any help would be appreciated.:

    <code>
    CREATE proc dbo.DefineLink
    (@Link varchar(250) output)
    as

    declare @TempID int
    set nocount on

    select @TempID = (SELECT ID from  CMS_PRData WHERE Active=1 AND Dt_GoLive <= GETDATE())-- ORDER BY DT_Article DESC)
    select @Link = (SELECT Link from  CMS_PRData WHERE Active=1 AND Dt_GoLive <= GETDATE())-- ORDER BY DT_Article DESC)

    If @Link = NULL
     set @Link = 'http://www.myserver.com/Article/article.aspx?content=2&ArticleID=' & @TempID
    GO
    </code>

    TIA,
    Stue

  • Re: Need help please...

    05-19-2006, 12:56 PM
    • Contributor
      3,329 point Contributor
    • billrob458
    • Member since 07-25-2003, 2:44 AM
    • New York, NY
    • Posts 671

    This might not be the 'best' solution, but you could use a case/when statement in your select query.

    SELECT
     ID
     , Title
     , Topic
     , Summary
     , Dt_Article
     , case when Link IS NULL OR Link = '' THEN
      'http://www.myserver.com/Article/article.aspx?content=2&ArticleID=' + Cast( ID as varchar )
       else
      Link
       END as Link
    FROM CMS_PRData
    WHERE
     Active=1
     AND Dt_GoLive <= GETDATE()
    ORDER BY
     DT_Article DESC


     

     

  • Re: Need help please...

    05-19-2006, 1:04 PM
    • All-Star
      86,470 point All-Star
    • limno
    • Member since 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 4,917
    • Moderator
      TrustedFriends-MVPs

    You can use an inline SQL with the powerful CASE  OR COALESCE function here.

    Here I am using the COALESCE function to show how we can do. If a record value in  LINK column is NULL in the database, the it pick up the one defined here:

    SELECT ID, Title, Topic, Summary, Dt_Article, Link = COALESCE(LINK,'http://www.myserver.com/Article/article.aspx?content=2&ArticleID='+ID) as LINK FROM CMS_PRData WHERE Active=1 AND Dt_GoLive <= GETDATE() ORDER BY DT_Article DESC

     

    If you have issues with this query, please post back.

    Limno

    Format your SQL query with instant sql formatter:
    http://www.dpriver.com/pp/sqlformat.htm
  • Re: Need help please...

    05-19-2006, 1:22 PM
    • Member
      635 point Member
    • Stue
    • Member since 08-13-2002, 10:28 AM
    • Posts 145
    limno:

    You can use an inline SQL with the powerful CASE  OR COALESCE function here.

    Here I am using the COALESCE function to show how we can do. If a record value in  LINK column is NULL in the database, the it pick up the one defined here:

    SELECT ID, Title, Topic, Summary, Dt_Article, Link = COALESCE(LINK,'http://www.myserver.com/Article/article.aspx?content=2&ArticleID='+ID) as LINK FROM CMS_PRData WHERE Active=1 AND Dt_GoLive <= GETDATE() ORDER BY DT_Article DESC

     

    If you have issues with this query, please post back.

    Limno, I tried yours first and got "Incorrect syntax near the keyword 'as'."
    Billrob Im gunna give yours a shot.

    BTW thanks a ton for both your fast replies!

  • Re: Need help please...

    05-19-2006, 1:27 PM
    • Member
      635 point Member
    • Stue
    • Member since 08-13-2002, 10:28 AM
    • Posts 145

    BTW, heres the string (no stored proc for the time being):

    strQuery =

    "SELECT ID, Title, Topic, Summary, Dt_Article, Link = " _
    &
    "COALESCE(LINK,'http://www.myserver.com/Article/Article.aspx?Category=1&ArticleID='+ID) " _
    &
    "as LINK FROM CMS_PRData WHERE Active=1 AND Dt_GoLive <= GETDATE() ORDER BY DT_Article DESC"

     

  • Re: Need help please...

    05-19-2006, 1:51 PM
    • Contributor
      3,329 point Contributor
    • billrob458
    • Member since 07-25-2003, 2:44 AM
    • New York, NY
    • Posts 671

    Be aware that coalesce will return the first NON-NULL parameter.  In your initial requirements you stated LINK could either be null or "".  If LINK is "", then coalsce will not work as you intended.

     

  • Re: Need help please...

    05-19-2006, 2:01 PM
    • Member
      635 point Member
    • Stue
    • Member since 08-13-2002, 10:28 AM
    • Posts 145

    HA! right when you posted that billrob I figured out the problem with limnos code:

    SELECT ID, Title, Topic, Summary, Dt_Article, Link = COALESCE(LINK, 'http://www.myserver.com/Article/article.aspx?content=2&ArticleID='+ Cast( ID as varchar)) FROM CMS_PRData WHERE Active=1 AND Dt_GoLive <= GETDATE() ORDER BY DT_Article DESC

    Your code works and if what you state is true I'd better go your route. One last question though. How do I get the repeater to display the results. I ran your code in query analyzer confirmed results tried the form w the repeater and got nothing??? Am I missing anything?:

    Sub FillRepeater()

    Dim strConn As String = ConfigurationManager.AppSettings("AnyDocCMS")

    Dim strQuery As String

    strQuery = "SELECT ID, Title, Topic, Summary, Dt_Article, case when Link IS NULL OR " _
    &
    "Link = '' THEN 'http://www.anydocsoftware.com/Article/Article.aspx?Category=3&ArticleID=' + " _
    &
    "Cast(ID as char) else Link END as Link FROM CMS_PRData " _
    &
    "ORDER BY DT_Article DESC"
    pnlPR.Visible = True
    End If

    Using sqlDAdapter As Data.SqlClient.SqlDataAdapter = New Data.SqlClient.SqlDataAdapter(strQuery, strConn)
    Dim dtTable As Data.DataTable = New Data.DataTable()
    sqlDAdapter.Fill(dtTable)
    rptA.DataSource = dtTable
    rptA.DataBind()
    End Using

  • Re: Need help please...

    05-19-2006, 2:22 PM
    • Member
      635 point Member
    • Stue
    • Member since 08-13-2002, 10:28 AM
    • Posts 145
    Nevermind im a idgit. I was specifying the wrong repeater....Doh!
  • Re: Need help please...

    05-19-2006, 2:30 PM
    • Contributor
      3,329 point Contributor
    • billrob458
    • Member since 07-25-2003, 2:44 AM
    • New York, NY
    • Posts 671

    I figured the repeater was really rptB. :)

    Glad it worked out for you.  You should also add COALSCE to your sql bag of tricks.  It comes in VERY handy on occasion. 

  • Re: Need help please...

    05-19-2006, 2:47 PM
    • Member
      635 point Member
    • Stue
    • Member since 08-13-2002, 10:28 AM
    • Posts 145

    Ha! rptB.

    Yeah once upon a time a long time ago when I wrote my first stored proc I had a need for COALSCE and used it. Worked for me back then but the need for anythin above basic SELECT statements around here has been pretty low since then unfortunately. So I dont get to hone my SQL skills as much as I should :(

    Anyhow, thanks again.. this woulda taken me forever to figure out! great way to cap a Friday. Have a great weekend to both you guys for the help!

    Thanks again,
    Stue

Page 1 of 1 (10 items)
Microsoft Communities