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.
| Author |
Topic |
|
oguzkaygun
Yak Posting Veteran
53 Posts |
Posted - 2010-03-13 : 13:49:07
|
| Hellomy query is below. But it doesnt work truely.i have a value for x_kod. First i order by x_kod. then i need next record from a value of x_kod. How can i do this ?SELECT x_id, x_kod INTO #CustContact FROM " + hangitable + " ORDER BY x_kod SELECT (x_id+1), x_kod FROM #CustContact Where x_kod = " + valueTB + " ORDER BY x_id |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-13 : 14:22:23
|
| can you show some sample data and then output you require out of them?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
oguzkaygun
Yak Posting Veteran
53 Posts |
Posted - 2010-03-13 : 14:33:24
|
| x_kod x_id100 1101 2102 3100.001 4100.002 5100.003 6abc001 7abc002 8when i enter 101, i need 102 (value of x_kod is 101)when i enter 102, i need 100.001 (value of x_kod is 102)when i enter abc001, i need abc002 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-13 : 14:36:56
|
| [code]DECLARE @xkod varchar(100)SELECT TOP 1 x_kod,x_idFROM TableWHERE x_id>(SELECT x_id FROM Table WHERE x_kod=@xkod)ORDER BY x_id ASC[/code]pass a suitable value for @xkod like 101,102,..------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
oguzkaygun
Yak Posting Veteran
53 Posts |
Posted - 2010-03-13 : 15:03:30
|
| what will happen if data is like below ?Id x_kod1 1022 abc0013 100.0014 1015 100.0026 abc002when i enter 101, i need 102when i enter 100.001, i need 100.002when i enter abc001, i need abc002 |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2010-03-13 : 17:39:30
|
Add a row number to your pre-sorted table is one way..probably a better way, but this works...create table foo (x_id int IDENTITY(1,1) Not Null,x_kod varchar(7) not null) goInsert into fooSELECT '102' UNION ALLSELECT 'abc001' UNION ALLSELECT '100.001' UNION ALLSELECT '101' UNION ALLSELECT '100.002' UNION ALLSELECT 'abc002' go Create proc get_next_xkod (@xkod varchar(7))asSelect ROW_NUMBER() OVER (ORDER BY x_kod asc) as ROW,x_kodinto #orderfooFROM foo Select x_kod as nextkodFROM #orderfooWhere ROW = (Select ROW +1 from #orderfoo where x_kod = @xkod)GoExec get_next_xkod 'abc001' ---returns abc002goExec get_next_xkod '100.001' ---returns 100.002godrop table foodrop proc get_next_xkodgo I assumed that "when I enter..." you are passing a variable to the procedure. Hope it helps. There are other ways to do this too...depending on the record count..this won't be terribly efficient but just trying to give you something to work with Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-14 : 00:23:03
|
quote: Originally posted by oguzkaygun what will happen if data is like below ?Id x_kod1 1022 abc0013 100.0014 1015 100.0026 abc002when i enter 101, i need 102when i enter 100.001, i need 100.002when i enter abc001, i need abc002
DECLARE @xkod varchar(100)SELECT TOP 1 x_kod,x_idFROM TableWHERE x_kod>@xkodORDER BY x_kod ASC ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|