Showing posts with label returns. Show all posts
Showing posts with label returns. Show all posts

Sunday, March 25, 2012

Cursor for a result set returned by a stored proc

Hi all,

i got a question. I have a stored procedure sp1 calling another stored procedure sp2 using Exec sp2 cmd.

sp2 returns 2 rows in the result set.
Now I want to loop thru those 2 rows and get access to its column values
from sp1. How can this be acheived?

thanks for your help

omair-I came across this after reading your post:

One legitimate use for temp tables is to use them to pass recordsets from a nested stored procedure to a calling stored procedure. This is the only way to pass recordsets from one stored procedure to another. When you do this, follow the other tips on this Web site to maximize temp table performance. [6.5, 7.0, 2000] Updated 3-6-2006

Hope it helps.

Hannah

Thursday, March 22, 2012

Cursor @@rowcount returns 0 everytime

I everyone. I've got a very strange problem in my cursor. It's very strange because, if i execute my query without the cursor it returns one record, but if i use it in a cursor the @.@.rowcount returns 0.

Code Snippet

DECLARE CCursor CURSOR FAST_FORWARD

FOR

Select id_base, id_rc

From Terceiros_Tarifarios

Where codigo=@.terceiro

And data_fim>cast(cast(left(@.document_ini,4) as varchar)+'-'+Cast(right(@.documento_ini,2) as varchar)+'-01' as Datetime)

Open CCursor

set @.cnt_del = @.@.rowcount

if @.cnt_del=0

begin

print 'No records'

end

...

@.documento_ini (int) and @.terceiro (varchar(20)) are set before cursor declaration

Code Snippet

set @.documento_ini = 200607

set @.terceiro = '4170'

Please help my in this tip.

Thanks

@.@.Rowcount is not used for cursor row count. You have to use the @.@.FETCH_STATUS variable. The following logic can help you to achive your requirement..

Code Snippet

Declare CCursor Cursor FAST_FORWARD

For

Select * from Sysobjects;

Open CCursor

Declare @.flag as Bit

Set @.flag = 0 --Set the inital flag as OFF

Fetch next from CCursor Into .....

While @.@.FETCH_STATUS = 0

Begin

Set @.flag = 1 -- Reset the Flag

--do your regular action here..

Fetch next from CCursor Into .....

End

If @.flag = 0 --If no records on the Cursor the flag value is still ZERO

Begin

Print 'No records'

End

Close CCursor

Deallocate CCursor

|||

Ok, so i've changed @.@.rowcount for @.@.cursor_rows and the output is -1.

But how can i know how many rows are returned by cursor ?

I need to execute some code if cursor returns no records, and with @.@.FETCH_STATUS i can't confirm that.

edti: ok, i know u post a sample that uses @.@.FETCH_STATUS to know that, but i need to know it before a while @.@.fetch_status = 0

|||

@.@.CURSOR_ROWS is only used on the following STATIC & KEYSET cursors

Code Snippet

Declare @.cnt_del as int

Declare CCursor CURSOR STATIC –- or KEYSET

FOR

Select id_base, id_rc

From Terceiros_Tarifarios

Where codigo=@.terceiro

And data_fim>cast(cast(left(@.document_ini,4) as varchar)

+'-'+Cast(right(@.documento_ini,2) as varchar)+'-01' as Datetime)

Open CCursor

Set @.cnt_del = @.@.CURSOR_ROWS

If @.cnt_del=0

Begin

print 'No records'

End

Close CCursor

Deallocate CCursor

|||

Tiago Salgado,

Check function CURSOR_STATUS in BOL.

...

-- you are not explicitly declaring if your cursor is local or global, so

-- it will depend on your settings

open CCursor

if cursor_status('global', 'CCursor') = 0

print 'no records'

...

AMB

|||

cursor_status is not a valid function to verify for the record count, again if you use the dynamic cursor you always get the result as 1. (even there is no row).

Finally , if you use the dynamic cursor you can’t get the record count/status properly. (That’s what other providers like ADODB also says)

To achieve your desired output you have to use either STATIC or KEYSET cursor rather than DYNAMIC or FAST_FORWARD.

|||

Sorry, but for static and keyset cursors it is as valid as @.@.rowcount. My first sentence says "Check function cursor_status in BOL", so if he/she does, then will notice the "*" beside "cursor name" and "cursor variable" in the table describing "return types".

Anyway, thanks for your comment.

use northwind

go

declare c cursor local static

for

select *

from dbo.orders

where 1 = 2

open c

select cursor_status('local', 'c'), @.@.rowcount, @.@.cursor_rows

close c

deallocate c

go

AMB

|||Thanks to all your comments. I've solve my problem.

CURRENT_USER returns dbo instead of NT Username

Is there any way to avoid this?"David J Rose" <david.rose@.newsgroup.reply.only.com> wrote in message
news:IJGdnRrxGboYgsnfRVn-rQ@.mycybernet.net...
> Is there any way to avoid this?
>
I guess you could modify the calling application so that it logs in using
Windows authentication rather than SQL Server authentication. You can also
enforce "Windows Authentication only" at the server level.
Regards,
Simon|||SQL Server is using "Windows Authentication only". It works fine for normal
users, but for symins, I get "dbo". Any ideas?
"Simon Shearn" <simon@.sgurr.theredwire.co.uk> wrote in message
news:4254208f$0$38043$bed64819@.news.gradwell.net...
> "David J Rose" <david.rose@.newsgroup.reply.only.com> wrote in message
> news:IJGdnRrxGboYgsnfRVn-rQ@.mycybernet.net...
> I guess you could modify the calling application so that it logs in using
> Windows authentication rather than SQL Server authentication. You can also
> enforce "Windows Authentication only" at the server level.
> Regards,
> Simon
>|||Try SUSER_SNAME.
AMB
"David J Rose" wrote:

> Is there any way to avoid this?
>
>|||Take a look at USER_NAME() and SUSER_SNAME().
This is my signature. It is a general reminder.
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.
"David J Rose" <david.rose@.newsgroup.reply.only.com> wrote in message
news:iZqdnWK4aJwDvMnfRVn-3w@.mycybernet.net...
> SQL Server is using "Windows Authentication only". It works fine for
normal
> users, but for symins, I get "dbo". Any ideas?
> "Simon Shearn" <simon@.sgurr.theredwire.co.uk> wrote in message
> news:4254208f$0$38043$bed64819@.news.gradwell.net...
using
also
>sql