WorkaHolic/MSSQL

Cursors and dynamic SQL

2008. 9. 4. 14:21
반응형

[출처] http://bytes.com/forum/thread142335.html

Cursors and dynamic SQL

Question posted by: Joško Šugar (Guest) on July 23rd, 2005 07:15 AM
On this site:
http://www.sommarskog.se/dynamic_sql.html

I have found an example how to use cursor with dynamic SQL:
DECLARE @my_cur CURSOR
EXEC sp_executesql
N'SET @my_cur = CURSOR FOR SELECT name FROM dbo.sysobjects; OPEN
@my_cur',
N'@my_cur cursor OUTPUT', @my_cur OUTPUT
FETCH NEXT FROM @my_cur


But when I tried to do this:

IF (@Naziv <> '')
SET @sql_where = @sql_where + N' AND Naziv LIKE ' + @Naziv

IF (@Funk <> '')
SET @sql_where = @sql_where + N' AND Funkcija LIKE ' + @Funk

IF (@Mj <> '')
SET @sql_where = @sql_where + N' AND NazivMjesta LIKE ' + @Mj

IF (@Drz <> '')
SET @sql_where = @sql_where + N' AND (drzava1 LIKE ' + @Drz +
' OR drzava2 like ' + @Drz + ' OR drzava3 LIKE ' + @Drz + ')'


DECLARE @CursSearch CURSOR

SET @sql = N'SET @CursSearch = CURSOR FOR
SELECT CvorID, NadCvorID,
IzvorisniCvorID, Naziv, TipCvora,
NasljednaLinija, Funkcija, NazivMjesta,
drzava1, drzava2, drzava3
FROM dbo.Pretrazivanje
WHERE NasljednaLinija LIKE @NasljednaLinija'
+ @sql_where + N'; OPEN @CursSearch'

EXEC sp_executesql @sql, N'@CursSearch CURSOR OUTPUT',
@CursSearch OUTPUT



....by fetching cursor i got this message:
The variable '@CursSearch' does not currently have a cursor allocated to it.


Can anybody tell me what i did wrong?
DB2 SQL tool, DB2 Database tools Download Now! Windows, Linux, OSX
SQL Server coding, T-SQL and query optimization tips - ALL FREE!
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
John Bell's Avatar
John Bell
Guest
n/a Posts
July 23rd, 2005
07:15 AM
#2

Re: Cursors and dynamic SQL
Hi

I suggest you print out the statement @sql and debug it in Query Analyser.

"Joško Šugar" <josko@netgen_makniOvo.hr> wrote in message
news:7512.41bdcba7.c66fd@hubble...[color=blue]
> On this site:
> http://www.sommarskog.se/dynamic_sql.html[/color]
And what an excellent site it is!
[color=blue]
>
> I have found an example how to use cursor with dynamic SQL:
> DECLARE @my_cur CURSOR
> EXEC sp_executesql
> N'SET @my_cur = CURSOR FOR SELECT name FROM dbo.sysobjects; OPEN
> @my_cur',
> N'@my_cur cursor OUTPUT', @my_cur OUTPUT
> FETCH NEXT FROM @my_cur
>
>
> But when I tried to do this:
>
> IF (@Naziv <> '')
> SET @sql_where = @sql_where + N' AND Naziv LIKE ' + @Naziv
>[/color]
I would expect quotes around your strings say:

SET @sql_where = @sql_where + N' AND Naziv LIKE ''' + @Naziv + '%'''

[color=blue]
> IF (@Funk <> '')
> SET @sql_where = @sql_where + N' AND Funkcija LIKE ' + @Funk
>
> IF (@Mj <> '')
> SET @sql_where = @sql_where + N' AND NazivMjesta LIKE ' + @Mj
>
> IF (@Drz <> '')
> SET @sql_where = @sql_where + N' AND (drzava1 LIKE ' + @Drz +
> ' OR drzava2 like ' + @Drz + ' OR drzava3 LIKE ' + @Drz + ')'
>
>
> DECLARE @CursSearch CURSOR
>
> SET @sql = N'SET @CursSearch = CURSOR FOR
> SELECT CvorID, NadCvorID,
> IzvorisniCvorID, Naziv, TipCvora,
> NasljednaLinija, Funkcija, NazivMjesta,
> drzava1, drzava2, drzava3
> FROM dbo.Pretrazivanje
> WHERE NasljednaLinija LIKE @NasljednaLinija'
> + @sql_where + N'; OPEN @CursSearch'
>
> EXEC sp_executesql @sql, N'@CursSearch CURSOR OUTPUT',
> @CursSearch OUTPUT
>
>
>
> ...by fetching cursor i got this message:
> The variable '@CursSearch' does not currently have a cursor allocated to
> it.
>
>
> Can anybody tell me what i did wrong?[/color]

John



Joško Šugar's Avatar
Joško Šugar
Guest
n/a Posts
July 23rd, 2005
07:15 AM
#3

Re: Cursors and dynamic SQL
John Bell wrote:[color=blue]
> Hi
>
> I suggest you print out the statement @sql and debug it in Query Analyser.
>[color=green]
>>
>>IF (@Naziv <> '')
>>SET @sql_where = @sql_where + N' AND Naziv LIKE ' + @Naziv
>>[/color]
>
> I would expect quotes around your strings say:
>
> SET @sql_where = @sql_where + N' AND Naziv LIKE ''' + @Naziv + '%'''
>
>[/color]

Thanks a lot!

[color=blue]
>[color=green]
>>
>>DECLARE @CursSearch CURSOR
>>
>>SET @sql = N'SET @CursSearch = CURSOR FOR
>>SELECT CvorID, NadCvorID,
>>IzvorisniCvorID, Naziv, TipCvora,
>>NasljednaLinija, Funkcija, NazivMjesta,
>>drzava1, drzava2, drzava3
>>FROM dbo.Pretrazivanje
>>WHERE NasljednaLinija LIKE @NasljednaLinija'[/color][/color]

Another error was here. This variable was not declared.
[color=blue][color=green]
>>+ @sql_where + N'; OPEN @CursSearch'
>>
>>EXEC sp_executesql @sql, N'@CursSearch CURSOR OUTPUT',
>>@CursSearch OUTPUT
>>[/color][/color]

Is there any kind of performance issue in using cursors like this,
compared to named cursors?
I've managed to solve this problem by using named cursors and it is more
than 2x faster.


John Bell's Avatar
John Bell
Guest
n/a Posts
July 23rd, 2005
07:15 AM
#4

Re: Cursors and dynamic SQL
Hi
[color=blue]
> Is there any kind of performance issue in using cursors like this,
> compared to named cursors?
> I've managed to solve this problem by using named cursors and it is more
> than 2x faster.
>[/color]

I don't know why using a cursor variable may be significantly slower than a
specifically declared one. It could be that you are declaring different
types of cursor.

The fastest solution will almost certainly be a set based one.

John



Erland Sommarskog's Avatar
Erland Sommarskog
Guest
n/a Posts
July 23rd, 2005
07:25 AM
#5

Re: Cursors and dynamic SQL
Jo¨ko ¦ugar (josko@netgen_makniOvo.hr) writes:[color=blue][color=green][color=darkred]
>>>EXEC sp_executesql @sql, N'@CursSearch CURSOR OUTPUT',
>>>@CursSearch OUTPUT
>>>[/color][/color]
>
> Is there any kind of performance issue in using cursors like this,
> compared to named cursors?
> I've managed to solve this problem by using named cursors and it is more
> than 2x faster.[/color]

I have never used cursor variables, so I don't really know. But I find
it somewhat difficult to believe that the overhead would be of that
magnitude. As John said, the cursor type may matter more.


--
Erland Sommarskog, SQL Server MVP, Join Bytes!

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Jo¨ko ¦ugar's Avatar
Jo¨ko ¦ugar
Guest
n/a Posts
July 23rd, 2005
07:25 AM
#6

Re: Cursors and dynamic SQL
Erland Sommarskog wrote:[color=blue]
> Jo¨ko ¦ugar (josko@netgen_makniOvo.hr) writes:
>[color=green][color=darkred]
>>>>EXEC sp_executesql @sql, N'@CursSearch CURSOR OUTPUT',
>>>>@CursSearch OUTPUT
>>>>[/color]
>>
>>Is there any kind of performance issue in using cursors like this,
>>compared to named cursors?
>>I've managed to solve this problem by using named cursors and it is more
>>than 2x faster.[/color]
>
>
> I have never used cursor variables, so I don't really know. But I find
> it somewhat difficult to believe that the overhead would be of that
> magnitude. As John said, the cursor type may matter more.
>
>[/color]

Right again! You guys are my idols!
Anyway, with cursor and dynamic sql I managed to speed up the original
query by 10x.
Thanks


Josko

 

반응형