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 2000 Forums
 Transact-SQL (2000)
 If exists - SP

Author  Topic 

sqldbaa
Starting Member

32 Posts

Posted - 2008-06-20 : 07:28:42
Hi

Please help me

I 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 level2
test testee N Y

the 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 rows


Thanks 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?
Go to Top of Page

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
Go to Top of Page

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)
AS
DECLARE @Temp table--table to hold temporary result from application
(
Accntid varchar(10),
level1 char(1),
level2 char(1)
)

INSERT INTO @Temp
SELECT 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 update
SET m.level1=t.level1,
m.level2=t.level2
FROm YourTable m
INNER JOIN @Temp t
ON m.AcctID=t.Accntid
AND m.UserID=@Login

INSERT INTO YourTable -- not available in your table so insert
SELECT @Login,t.Accntid,t.level1,t.level2
FROM @temp t
LEFT JOIN YourTable m
ON m.AcctID=t.Accntid
AND m.UserID=@Login
WHERE m.UserID IS NULL


DELETE m -- not available in string delete from your table
FROM YourTable m
LEFT JOIN @Temp t
ON m.AcctID=t.Accntid
AND m.UserID=@Login
WHERE t.Accntid IS NULL
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-20 : 09:33:40
this is function ParseValues

CREATE 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
Go to Top of Page

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 it

Server: Msg 156, Level 15, State 1, Procedure UpdateOrd, Line 6
Incorrect syntax near the keyword 'table'.
Server: Msg 170, Level 15, State 1, Procedure UpdateOrd, Line 17
Line 17: Incorrect syntax near '@AccntInfo'.
Server: Msg 170, Level 15, State 1, Procedure UpdateOrd, Line 24
Line 24: Incorrect syntax near '@Temp'.
Server: Msg 170, Level 15, State 1, Procedure UpdateOrd, Line 30
Line 30: Incorrect syntax near '@temp'.
Server: Msg 170, Level 15, State 1, Procedure UpdateOrd, Line 39
Line 39: Incorrect syntax near '@Temp'.
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2008-07-10 : 08:49:28
what version of SQL are you using?
Go to Top of Page

sqldbaa
Starting Member

32 Posts

Posted - 2008-07-10 : 08:53:22
SQl 7 version
Go to Top of Page

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
Go to Top of Page
   

- Advertisement -