if------then ----else in select statement

  • Not enough information to really help. Looks like there may be several things wrong with the code.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ and follow the instructions on what information to post and how to post it. The more information you provide, the better answers you will get.

  • where should i put fetch_next in body

  • riya_dave (4/18/2012)


    where should i put fetch_next in body

    This will help you with the code http://remidian.com/mssql/t-sql/cursors-in-tsql.html however if you post the full code of your SP and the table definitions (DDL) and some sample data we may be able to write your SP with out the cursor. cursors are row by row and SQL works best in almost every situation with a set based solution.

    and since you are not passing any thing to pbdashboard as your code is written now you dont need the cursor.


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • thanks for the link,i got it

  • ok.simple question.my cursor return one value that i need to take in temp table

    simply i can insert into temp table(cursor return value using local variable)

  • riya_dave (4/18/2012)


    ok.simple question.my cursor return one value that i need to take in temp table

    simply i can insert into temp table(cursor return value using local variable)

    Not enough information to really help.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ and follow the instructions on what information to post and how to post it. The more information you provide, the better answers you will get.

  • OK. I PUT FETCH_NEXT LIKE THIS

    STILL NOT WORKING GOING ENDLESS.

    declare @P1 nvarchar(32)

    declare @mtd2 float

    declare c1 CURSOR FOR

    select account, MTD from #temp2

    open c1

    FETCH NEXT FROM c1

    INTO @P1,@mtd2

    WHILE @@FETCH_STATUS = 0

    begin

    if(@mtd2 IS NULL )

    BEGIN

    declare @IRR2 float

    EXEC pdashboard

    select @IRR2 = IRR from pdashboard

    SELECT @IRR2 as Last1month

    fetch next from c1 into @P1,@MTD2

    END

    ELSE

    BEGIN

    select @mtd2 as last2month

    fetch next from c1 into @P1,@MTD2

    END

    END

    Any help?

  • Look closely at where you put your fetch next. It is inside an else block.

    _______________________________________________________________

    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/

  • riya_dave (4/18/2012)


    OK. I PUT FETCH_NEXT LIKE THIS

    STILL NOT WORKING GOING ENDLESS.

    declare @P1 nvarchar(32)

    declare @mtd2 float

    declare c1 CURSOR FOR

    select account, MTD from #temp2

    open c1

    FETCH NEXT FROM c1

    INTO @P1,@mtd2

    WHILE @@FETCH_STATUS = 0

    begin

    if(@mtd2 IS NULL )

    BEGIN

    declare @IRR2 float

    EXEC pdashboard

    select @IRR2 = IRR from pdashboard

    SELECT @IRR2 as Last1month

    fetch next from c1 into @P1,@MTD2

    END

    ELSE

    BEGIN

    select @mtd2 as last2month

    fetch next from c1 into @P1,@MTD2

    END

    END

    Any help?

    Not enough information to really help.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ and follow the instructions on what information to post and how to post it. The more information you provide, the better answers you will get.

  • i am also putting in if statement and after end of if,

    nothing is working out.

    where should i put

  • Basic cursor structure.

    fetch_next

    while fetch_status

    begin

    --do some stuff here

    fetch_next

    end

    _______________________________________________________________

    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/

  • declare @P1 nvarchar(32)

    declare @mtd2 float

    declare c1 CURSOR FOR

    select account, MTD from #temp2

    open c1

    FETCH NEXT FROM c1

    INTO @P1,@mtd2

    WHILE @@FETCH_STATUS = 0

    begin

    if(@mtd2 IS NULL )

    BEGIN

    declare @IRR2 float

    EXEC pdashboard

    select @IRR2 = IRR from pdashboard

    SELECT @IRR2 as Last1month

    END

    ELSE

    BEGIN

    select @mtd2 as last2month

    END

    fetch next from c1 into @P1,@MTD2

    END

    this is fine ???

  • riya_dave (4/18/2012)


    declare @P1 nvarchar(32)

    declare @mtd2 float

    declare c1 CURSOR FOR

    select account, MTD from #temp2

    open c1

    FETCH NEXT FROM c1

    INTO @P1,@mtd2

    WHILE @@FETCH_STATUS = 0

    begin

    if(@mtd2 IS NULL )

    BEGIN

    declare @IRR2 float

    EXEC pdashboard

    select @IRR2 = IRR from pdashboard

    SELECT @IRR2 as Last1month

    END

    ELSE

    BEGIN

    select @mtd2 as last2month

    END

    fetch next from c1 into @P1,@MTD2

    END

    this is fine ???

    again not enough information. what does EXEC pdashboard do and can you post your ddl and table definitions so we can better help you.


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • ok. i tried to put everwhere nothing is working

    plz help

    where should i put it

  • Okay, I have tried.

    You want help, you have to give us more than the little tidbit pieces you continue to post. I really want you to look at your posts and tell me if you knew nothing about what was being done, could you help that person with what is there.

    We can't. We can't see what you are doing, we can't see all of your code, we can't see your data, we don't even know what it is you are trying to accomplish.

    You want help, you have to tell use everything we need to know to understand your environment and what it is you are trying to accomplish.

    You know that link to that article I kept posting. Go read it before you post anything more on this thread.

Viewing 15 posts - 16 through 30 (of 67 total)

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