»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 09-11-2003, 08:30 AM   #1 (permalink)
Registered User
 
Join Date: Aug 2003
Posts: 4
srinivas_mca2k is on a distinguished road
Outdoors how to use cursors with strings

hai,
I stored some sql statement in a query and trying to execute it under cursor. But it is giving some error.

declare @str as nvarchar(1000)
set @str = N'select [web-co-num],[inst-date] from [co-scheme]'

declare bill cursor for
exec sp_executesql @str

deallocate bill

I am trying in normal execute also.

declare @str as nvarchar(1000)
set @str = N'select [web-co-num],[inst-date] from [co-scheme]'

declare bill cursor for
exec (@str)

deallocate bill

In both cases i have failed. this is a simple example query. But i want a cursor in my sp, which the string variable contains big dynamic query.

So. please help me to solve this issue.

srinivas_mca2k is offline   Reply With Quote
Old 09-11-2003, 09:47 PM   #2 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
qball is on a distinguished road
Quote:
In both cases i have failed. this is a simple example query. But i want a cursor in my sp, which the string variable contains big dynamic query.
do you want a:

'simple example query'
OR a:
cursor, (result set)
???

what is your problem?
can you execute SQL manually? if that fails, DB will tell why.
if can execute, try programatically, if fails, determine EXACTLY what SQL stmt the program is trying to execute!

Code:
sql="SELECT * FROM user_tbl;";
print sql;
execute sql;
when/if "sql" fails, execute manually, DB will tell why...
qball is offline   Reply With Quote
Old 09-11-2003, 10:37 PM   #3 (permalink)
Registered User
 
Join Date: Aug 2003
Posts: 4
srinivas_mca2k is on a distinguished road
cursor strings

hi,
thanks for ur reply.
My actual problem is as follows:

declare @str as varchar(500)
set @str = 'select * from authors'

exec (@str) -------(1)

declare bill cursor for
exec (@str) -----(2)


-----------------------------------------------------------------------------

if we execute the above first three statements (at st(1)), its giving the records in the authors table.

if i execute the whole statements execpt (1), i.e. trying to execute the sql statement under cursor, its giving error. The error description is as follows:

Server: Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'exec'.

I want to execute a query string, under a cursor. so that i can process on record by record.
srinivas_mca2k is offline   Reply With Quote
Old 09-11-2003, 11:42 PM   #4 (permalink)
Registered User
 
Join Date: Sep 2002
Posts: 265
Creosote is on a distinguished road
Do the whole thing dynamically:

Code:
DECLARE @sql NVARCHAR(1024)
SET @sql = 

'DECLARE @SpeakerID INT
DECLARE @SpeakerDesc NVARCHAR(50)

DECLARE cur1 CURSOR LOCAL FOR SELECT * FROM Speakers
OPEN cur1
FETCH NEXT FROM cur1 INTO @SpeakerID, @SpeakerDesc
WHILE @@FETCH_STATUS = 0
BEGIN
	PRINT CONVERT(NVARCHAR(5),@SpeakerID) + '', '' + @SpeakerDesc
	FETCH NEXT FROM cur1 INTO @SpeakerID, @SpeakerDesc
END
CLOSE cur1
DEALLOCATE cur1'

EXEC(@sql)
Creosote is offline   Reply With Quote
Old 09-12-2003, 09:06 PM   #5 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
qball is on a distinguished road
a 'cursor' is DB speak for resultset.
sp is stored proc, is DB speak for stored procedure/logic/program.
usually works, if you can define how.

Quote:
Do the whole thing dynamically:
that may work.

can you do it 'dynamically' if can't do statically?
qball is offline   Reply With Quote
Old 09-13-2003, 06:45 PM   #6 (permalink)
Registered User
 
Join Date: Sep 2002
Posts: 265
Creosote is on a distinguished road
Judginig by his syntax, it appears he is using sql server. By saying doing it dynamically, I meant using dynamic sql : using transact-sql itself to feed a statement to execute.

Obviously, he wants a "cursor" to be able to loop through a result set from within transact-sql.
Creosote is offline   Reply With Quote
Old 09-13-2003, 08:43 PM   #7 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
qball is on a distinguished road
Quote:
Judginig by his syntax, it appears he is using sql server.
'he' has the problem, let em describe further, including DB and client info...if one describes problem, maybe understand better?

Quote:
Obviously, he wants a "cursor" to be able to loop through a result set from within transact-sql.
obviously you "assume".

a 'cursor' is DB speak for resultset.

If you can't execute a static SQL query, I've NEVER seen an example of doing it dynamically solving problem, obviously.
qball is offline   Reply With Quote
Old 09-14-2003, 11:58 AM   #8 (permalink)
Registered User
 
Join Date: Sep 2002
Posts: 265
Creosote is on a distinguished road
Then obviously you don't have experience with every DBMS.

He is using sp_executesql. I don't know of any other DBMS that uses that exact same stored procedure name.

He IS trying to get a cursor off of a resultset based on tell that sp_executesql procedure to execute a variable string. That part is easy to see. It however does not work.

My suggestion is to put the statement telling the DBMS to execute a variable string outside of the cursor declaration -- which does work.

Quote:
If you can't execute a static SQL query, I've NEVER seen an example of doing it dynamically solving problem, obviously.
Then read
Creosote is offline   Reply With Quote
Old 09-14-2003, 12:09 PM   #9 (permalink)
Registered User
 
Join Date: Oct 2001
Location: TOO close to Wash DC
Posts: 7,956
vass0922 is on a distinguished road
I had to write a dynamic SP once.. all the table names were variables.. can't do that in SQL (at least not in sql 7) so it made it a bit difficult

Worked fine beyond that though
__________________
<< Insert exceedingly large and overly verbose message of how 1337 you are here including full specs of every vehicle you've ever driven and PC you've owned >>
vass0922 is offline   Reply With Quote
Old 09-14-2003, 09:42 PM   #10 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
qball is on a distinguished road
Quote:
If you can't execute a static SQL query, I've NEVER seen an example of doing it dynamically solving problem, obviously.
Quote:
Then obviously you don't have experience with every DBMS.
CORRECT!

BUT all DBMS, me havum experienced use SQL!

before SQL, DOS/lotus123/dbase1-4?

anyone that knows SQL, knows:
Show me the SQL DBMS executing, OR NOT?

problem solved, period
qball is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 02:28 AM.