Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 (Solve)Hi, need helps

Author  Topic 

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-14 : 22:33:51
i try to create a store pro, something like

DECLARE @aaa char(13)

DECLARE CURSORA CURSOR FOR
SELECT column1
FROM TABLE_1
WHERE @aaa=(SELECT min(@aaa)
FROM TABLE_1

...
PRINT CAST(@aaa as char(15))

the result is not the result i wan...how come??can anyone please help me...millionx thx...bless you...muaks

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-14 : 22:56:31
help...quite urgent...
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-05-14 : 23:07:20
Please clearify your question. Also your query does not look accurate. Post the table structure, sample data and desired results.


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-14 : 23:08:06
maybe i was not clear....the result of the cursor will not show the minimun but it show the 1st row of the table
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-14 : 23:08:21
You have to tell us more what are you trying to do before we can help you.

Please provide your table's DDL, sample data and the expected result


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-14 : 23:12:32
TABLE_1
column1| A | B | C |
A | 0 | 2 | 4 |
B | 2 | 0 | 5 |
C | 4 | 5 | 0 |

CREARE PROCEDURE testing (@getA CHAR(1))
AS
DECLARE @resultA VARCHAR(30)
DECLARE CURSORA CURSOR FOR
SELECT column1
FROM TABLE_1
WHERE @getA=(SELECT min(@getA)
FROM TABLE_1 WHERE @get <> 0)
BEGIN
OPEN CURSORA
FETCH NEXT FROM CURSORA
INTO @resultA
PRINT CAST(@resultA as char(30))
CLOSE CURSORA
DEALLOCATE CURSORA
END
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-14 : 23:14:04
result will be
2
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-14 : 23:23:03
not very sure what is the purpose of your cursor . .

try this

select *
from TABLE_1
where A = (select min(A) from TABLE_1 WHERE A <> 0)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-14 : 23:25:00
the purpose of my cursor is that when the user
EXEC testing 'B'
it will show the result 2
EXEC testing 'C'
it will show the result 4
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-14 : 23:26:54
how many of such columns you need to "test" ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-14 : 23:39:08
erm....around 20...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-14 : 23:44:17
[code]
CREATE PROCEDURE testing (@getA CHAR(1))
AS
BEGIN
DECLARE @sql nvarchar(1000)

SELECT @sql = 'select column1 from TABLE_1 where '
+ @getA + ' = (select min(' + @getA + ') from TABLE_1 WHERE ' + @getA + ' <> 0)'

exec (@sql)
END
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-14 : 23:58:57
>"< it prompt out 'variable assignment is not allowed in a cursor declaration'
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-15 : 00:07:48
quote:
Originally posted by waterduck

>"< it prompt out 'variable assignment is not allowed in a cursor declaration'


I don't use cursor at all in the Stored Procedure that i posted. Just copy and paste the code in your Query Window and run.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-05-15 : 00:20:49
OMG thx alot...really really thx...muaks ^^
Go to Top of Page
   

- Advertisement -