| Author |
Topic |
|
JmaC
Starting Member
18 Posts |
Posted - 2010-08-25 : 02:07:13
|
| hi all...i have registration form with button DELETE,EDIT,INSERT..All working fine...example table:i have table with 5rows...1 | Peter2 | James3 | Helen4 | Nickles5 | Johnif i delete ID 3 (3|Helen) from table...1 | Peter2 | James4 | Nickles5 | Johnnow am going to register new person...what i need is when i click INSERT button in form ID:3 have to appear automatically in textbox/dropdownlist to register new person.(use deleted ID for register new person).i write coding for get ID:6 and it's working good...SELECT min(PANG) FROM (SELECT AccNo + 1 AS PANG FROM debtor EXCEPT SELECT AccNo FROM Debtor) AS boon WHERE PANG >300000000can anyone help me...Thanks & RegardsJmaC |
|
|
Devart
Posting Yak Master
102 Posts |
Posted - 2010-08-25 : 02:40:48
|
| Hello,You can try this:SELECT TOP 1 t1.AccNo, (SELECT AccNo FROM debtor WHERE id=t1.id+1) as new_id, t1.id+1FROM (SELECT 0 as AccNo UNION ALL SELECT AccNo FROM debtor) t1WHERE (SELECT count(*) FROM debtor WHERE AccNo=t1.AccNo+1)=0ORDER BY t1.AccNoBest regards,Devart,SQL Server Tools:dbForge Schema ComparedbForge Data ComparedbForge Query Builder |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-08-25 : 03:07:20
|
Would this work ok?drop table stestgocreate table stest( cid int,cname varchar(10))goinsert into stest values(1 , 'Peter')goinsert into stest values(2 , 'James')goinsert into stest values(4 , 'Nickles')goinsert into stest values(5 , 'John')goselect min(a.cid)+1 from stest a where not exists(select 1 from stest b where a.cid+1=b.cid) |
 |
|
|
JmaC
Starting Member
18 Posts |
Posted - 2010-08-25 : 03:26:15
|
quote: Originally posted by Devart Hello,You can try this:SELECT TOP 1 t1.AccNo, (SELECT AccNo FROM debtor WHERE id=t1.id+1) as new_id, t1.id+1FROM (SELECT 0 as AccNo UNION ALL SELECT AccNo FROM debtor) t1WHERE (SELECT count(*) FROM debtor WHERE AccNo=t1.AccNo+1)=0ORDER BY t1.AccNoBest regards,Devart,SQL Server Tools:dbForge Schema ComparedbForge Data ComparedbForge Query Builder
thanks sir...when i apply this codes to my project it show error "Conversion failed when converting the nvarchar value 'I00002756' to data type int."And the t1 is refer for what?Thanks & RegardsJmaC |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-08-25 : 04:19:19
|
| Did you try converting your sql like I said? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-25 : 05:12:27
|
This is so wrong on so many levels!1) What if there are two users clicking INSERT? Both will be suggested ID:32) In your sample code, you use ID, but your code handles AccNoHow can we now what AccNo looks like when you don't supply enough information? N 56°04'39.26"E 12°55'05.63" |
 |
|
|
Devart
Posting Yak Master
102 Posts |
Posted - 2010-08-25 : 06:25:01
|
| Sorry,SELECT TOP 1 t1.AccNo+1 as new_AccNoFROM (SELECT 0 as AccNo UNION ALL SELECT AccNo FROM deptor) AS t1WHERE (SELECT count(*) FROM deptor WHERE AccNo=t1.AccNo+1)=0ORDER BY t1.AccNoBest regards,Devart,SQL Server Tools:dbForge Schema ComparedbForge Data ComparedbForge Query Builder |
 |
|
|
JmaC
Starting Member
18 Posts |
Posted - 2010-08-25 : 22:00:11
|
quote: Originally posted by Peso This is so wrong on so many levels!1) What if there are two users clicking INSERT? Both will be suggested ID:3 N 56°04'39.26"E 12°55'05.63"
hi...What should i do to avoid this happen?when there are two users clicking INSERT?is TRIGGER in SQL manage this problem?how to manage this problem with trigger? Thanks & RegardsJmaC |
 |
|
|
JmaC
Starting Member
18 Posts |
Posted - 2010-08-26 : 23:04:20
|
| I create trigger to solve this problem...CREATE TRIGGER TGRDebtorON dbo.DebtorFOR UPDATEAS IF () END go can anyone help me out....Thanks & RegardsJmaC |
 |
|
|
Devart
Posting Yak Master
102 Posts |
Posted - 2010-08-27 : 02:17:10
|
| Hello,For example:CREATE TRIGGER TGRDebtor ON dbo.deptorFOR INSERTASset nocount onupdate dset d.accno=(SELECT TOP 1t1.AccNo+1 as new_AccNoFROM(SELECT 0 as AccNo UNION ALL SELECT AccNo FROM deptor) AS t1WHERE(SELECT count(*) FROM deptor WHERE AccNo=t1.AccNo+1)=0ORDER BYt1.AccNo)from dbo.deptor d inner join inserted d1 on d.accno=d1.accnoColumn AccNo must be DEFAULT 0;Best regards,Devart,SQL Server Tools:dbForge Schema ComparedbForge Data ComparedbForge Query Builder |
 |
|
|
JmaC
Starting Member
18 Posts |
Posted - 2010-09-01 : 22:20:45
|
| "Column AccNo must be DEFAULT 0;"If i have AccNo start with 300000000 and have data more than 2000 already.how i can make it?where i have to make changes..Thanks & RegardsJmaC |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-09-02 : 00:59:29
|
| You shouldn't even bother. If a userid gets deleted, so what, let it get deleted.As Peter said above, this is so wrong on so many levels.Instead of spending time filling in the holes, figure out why anyone cares that they exist.Basically you're trying to fix something that isn't broken |
 |
|
|
|