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 |
sqldbaa
Starting Member
32 Posts |
Posted - 2008-06-20 : 07:28:42
|
HiPlease help meI have to create a SP.The secenario is that, A application calls the SP with a parameter(login), and a string of datas ( Acctid level1 level2; Acctid level1 level2; .......)UserID AcctID Level1 level2test testee N Ythe SP have to get the first string of data and check if the Acctid exists or not. If yes then update else insert.Then get then the second string of data and check if the Acctid exists or not. If yes then update else insert.After checking all the strings ,it have to check if any Acctids other than acctid mentioned in the string exists in the table for that login, then delete those rowsThanks in advance |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 07:44:35
|
Will Acctid level1 level2 values be all of integer type without any special characters or spaces between them other than the space delimiter? |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2008-06-20 : 07:51:33
|
the string of value from application will be as(SVDB, N, Y; VGBY, Y, B; SCFV, Y, S; SRVD, Y, N; .....)The SP have to get the fist set of data i.e SVDB, N, Y , check if SVDB exists in the table for that login, if yes update else insert.Finally do acheck and delete the Acctid not available in above list |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 08:27:36
|
CREATE PROC YourProc@Login varchar(50),@AccntInfo varchar(8000)ASDECLARE @Temp table--table to hold temporary result from application(Accntid varchar(10),level1 char(1),level2 char(1))INSERT INTO @TempSELECT RTRIM(LTRIM(PARSENAME(REPLACE(val,',','.'),3))),RTRIM(LTRIM(PARSENAME(REPLACE(val,',','.'),2))),RTRIM(LTRIM(PARSENAME(REPLACE(val,',','.'),1)))FROM dbo.ParseValues(@AccntInfo,';')--this function can be found inside this forums itself.UPDATE m -- present in your table so updateSET m.level1=t.level1,m.level2=t.level2FROm YourTable mINNER JOIN @Temp tON m.AcctID=t.AccntidAND m.UserID=@LoginINSERT INTO YourTable -- not available in your table so insertSELECT @Login,t.Accntid,t.level1,t.level2FROM @temp tLEFT JOIN YourTable mON m.AcctID=t.AccntidAND m.UserID=@LoginWHERE m.UserID IS NULLDELETE m -- not available in string delete from your tableFROM YourTable mLEFT JOIN @Temp tON m.AcctID=t.AccntidAND m.UserID=@LoginWHERE t.Accntid IS NULL |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 09:33:40
|
this is function ParseValuesCREATE FUNCTION ParseValues (@String varchar(8000),@Delimiter char(1) ) RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(100) ) AS BEGIN DECLARE @Value varchar(100) WHILE @String is not null BEGIN SELECT @Value=CASE WHEN CHARINDEX(@Delimiter,@String) >0 THEN LEFT(@String,CHARINDEX(@Delimiter,@String)-1) ELSE @String END, @String=CASE WHEN CHARINDEX(@Delimiter,@String) >0 THEN SUBSTRING(@String,CHARINDEX(@Delimiter,@String)+1,LEN(@String)) ELSE NULL END INSERT INTO @RESULTS (Val) SELECT @Value END RETURN END |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2008-07-10 : 08:12:13
|
But i get an error, exectuing the store procedure. Please help me doing itServer: Msg 156, Level 15, State 1, Procedure UpdateOrd, Line 6Incorrect syntax near the keyword 'table'.Server: Msg 170, Level 15, State 1, Procedure UpdateOrd, Line 17Line 17: Incorrect syntax near '@AccntInfo'.Server: Msg 170, Level 15, State 1, Procedure UpdateOrd, Line 24Line 24: Incorrect syntax near '@Temp'.Server: Msg 170, Level 15, State 1, Procedure UpdateOrd, Line 30Line 30: Incorrect syntax near '@temp'.Server: Msg 170, Level 15, State 1, Procedure UpdateOrd, Line 39Line 39: Incorrect syntax near '@Temp'. |
 |
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2008-07-10 : 08:49:28
|
what version of SQL are you using? |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2008-07-10 : 08:53:22
|
SQl 7 version |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2008-07-11 : 05:05:52
|
Create Function is not supported in SQL 7.Could you please help me how to change ParseValue function to a store procedure.Thanks in advance |
 |
|
|
|
|
|
|