We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,376 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to store the ROW_number into the variable in SQL 2008?

Hi,

Trying this Query,

[B]DECLARE @SR VARCHAR(8),@SC CHAR(15),@ST CHAR(15),@SNO int;
 SELECT * FROM (SELECT DISTINCT ROW_NUMBER() OVER (ORDER BY a.rno) AS rowid,
@SR=a.rno,@SC=a.kat,
@ST=a.type FROM SMas a,
 SDet b WHERE 
 b.id='X' AND
 a.rno=b.rno) AS C WHERE C.rowid=4

[/B]

but getting this following error:

[B][I][U]Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '='. (In this area,@SR=a.rno,@SC=a.kat,
@ST=a.type)
[/U][/I][/B]

Please help me out. Thanks in advance
[/I][/B]

3
Contributors
3
Replies
12 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
jacksantho
Junior Poster
169 posts since Feb 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

I don't have an SQL 2008 to test, but I'm guessing that your query fails for a number of reasons.
The 1st one is that you are assigning values to variables inside the nested query.
The 2nd one would be that distinct and row_number don't mix very well as row_number will make every record unique.

Have you tried:

DECLARE @SR VARCHAR(8),@SC CHAR(15),@ST CHAR(15),@SNO int;
select @SR=c.rno,@SC=c.kat,@ST=c.type, @SNO = c.rowid --I'm guessing @SNO is for rowid
from (SELECT ROW_NUMBER() OVER (ORDER BY a.rno) AS rowid,
a.rno,a.kat,a.type 
FROM SMas a, SDet b
WHERE  b.id='X' AND
 a.rno=b.rno ) c  
WHERE C.rowid=4
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

I came up with nearly the same query as @adam_k, but he beat me to posting. Only difference between his and mine is that I included the DISTINCT clause. And I did test it with fake tables/data and it worked fine both ways.

BitBlt
Practically a Posting Shark
894 posts since Feb 2011
Reputation Points: 482
Solved Threads: 148
Skill Endorsements: 14

HI,

Adam k and BitBit . Really Thanks for your guidance and for your help. It's too working for me.

jacksantho
Junior Poster
169 posts since Feb 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by adam_k and BitBlt

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0758 seconds using 2.68MB