SQL Query

  • Hello All,

    I am working on a procedure that just creates 2 temp tables and insert the records into those tables.

    This is the query

    alter PROC dbo.Test_rpt

    AS

    --drop table #podsid

    --drop table #podsid1

    CREATE TABLE DBO.#PodsID

    (

    ID INTEGER IDENTITY(1,1),

    Line_ID INTEGER

    )

    CREATE TABLE DBO.#PodsID1

    (

    ID INTEGER IDENTITY(1,1),

    LINE_ID INTEGER

    )

    INSERT INTO #PodsID

    select DISTINCT Line_ID FROM ASSET.DBO.Line ORDER BY Line_ID

    insert into #PodsID1

    SELECT ID AS ASMNT_ID from #PodsID

    where ID NOT IN (select DISTINCT Line_ID FROM ASSET.DBO.Line) order by ID

    SELECT TOP 1 Line_ID AS Pods_ID FROM #PodsID1

    When I execute this procedure I am getting strange result.When I execute once I am getting 1533 as the pods_id and when I execute again I am getting 11901. It is just toggling. I need to get only one value when I select Top 1. Please help me in figuring out what might be the issue.

    Thank You

  • Try adding an order by to your select top 1 query. Without an order by there is no way to know which row will be returned.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Yes. I got it. Thank You so much.

  • It really helps if you give your threads a more descriptive title. The title "SQL Query" applies to every single thread that belongs in this forum. (I can't guarantee that for threads that don't belong in this forum.)

    First, it helps people decide if they want to read your question. Would you be enticed to go to a restaurant called Restaurant with a menu that simply said Food? Would you be interested in a book called Book by the author Unknown?

    Second, it shows that you've actually given some thought to your question. One of the most frustrating things on this forum is someone who hasn't given thought to their question and posted too little information to actually even start to answer their question. Chances are pretty good that, if you haven't given thought to your title, you haven't given much thought to your question either.

    In short, it's about getting people interested in your question. If the title doesn't grab their attention, they're less likely to read your question. If they don't read your question, they won't be able to answer your question.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • I am sorry for not communicating properly. I was in urgent need situation and couldnt give proper subject name.Sorry for the inconvinience. I am new to sql prigramming. I am learning ways how to program sql. This is very helpful site. Once again sorry. Since I am new to sql I dont know so many things and I am self learning. I dont know thst I am using 1950s code.

    Thank you

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply