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 2005 Forums
 Transact-SQL (2005)
 Set Rule before Inserting

Author  Topic 

deepak_Dotnet
Starting Member

32 Posts

Posted - 2007-04-25 : 10:07:30
HI All,
I have Table "tblLicense" with column "ID" and "OwnerID"

ID OwnerID

ID01 PId_01
ID01 PId_02
ID01 PId_03
ID02 PId_02
ID02 PId_03

Assume that this values is already existing .

Each ID can have multiple Owners.But same ID CANNOT have same set of Owner(s) IDs.
Say for example in above table,
We have ownersID "PId_01,PId_02,PId_03" for ID "ID01" and owners IDs "PId_02,PId_03 " for ID "ID02"
so now say when user try to Add owner "PId_01" under "ID02" ,we should not allow user to insert,bcoz it makes two IDs having same set of OwnersID.So Before inserting we have check whether any ID exists which contain exact set of OwnersID.
If preset,don;t allow to insert,just return the ID which have same set of values.
IF doesn't exists ,then allow it insert and returns ID to which new Owner ID "PId_01" is allowed to added.

This set of owners to be added (like "PId_01" or "PId_01,PId_02,PId_05" )will passed from front end .

So how to achieve it .I thought of using Cursor.But finally got struck while doing it .
Any body give me idea or write query/function/cursor for this.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-04-25 : 10:14:05
2 things here.
1. you need CSVTable from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=CSVTable to split the string 'PId_01,PId_02,PId_05' to 3 rows

2. check for existence of Owner ID during insert

something like

insert into tblLicense (ID, OwnerID)
select @ID, stringval
from CSVTable(@OwnerID) t
where not exists (select * from tblLicense x where x.OwnerID = t.stringval)


@ID, @OwnerID are the variable pass in from front end

EDIT : You can also use fnParseString from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033 to parse the string 'PId_01,PId_02,PId_05'

KH

Go to Top of Page

deepak_Dotnet
Starting Member

32 Posts

Posted - 2007-04-26 : 02:17:51
Thanks for reply....
Now i don;t want to insert at this point..
I just want to retrieve ID value if any ID has/contain exact set of OwnersID ,which is pased from front end.
for example :
ID01 PId_01
ID01 PId_02
ID01 PId_03
ID02 PId_02
ID02 PId_03

When we pass this set of ownsers ID 'PId_01,PId_02,PId_03' ,then it should check for any ID which have exactly those set of value.IF any ID is having exactly those set of values ,then it should return the corresponding ID value.
If not, return null.
Lets put the return value to some OUTPUT parameter in Store procedure...
I am trying to alter the ur query...please let me know how to achieve it
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-04-26 : 02:46:01
sorry, don't quite understand.
What is pass in from front end ? only the OwnerID ('PId_01,PId_02,PId_03')or the ID also ?

For OwnerID 'PId_01,PId_02,PId_03' what is the expected result that you want ?


KH

Go to Top of Page

deepak_Dotnet
Starting Member

32 Posts

Posted - 2007-04-26 : 03:31:51
Thanks ..let me explain u again ....

From front end ,only Owner(s) ID will passed to Stored procedure.
So we have to check the exact set of Owners ID in table against the "OwnerID" column and if present retrieve corresponding ID.
IF not present ,return null.

Setting the return value in some Output parameter...

for example :
ID OwnerID

ID01 PId_01
ID01 PId_02
ID01 PId_03
ID02 PId_02
ID02 PId_03
ID03 PId_04
ID03 PId_06

In above example
IF uses pass 'PId_01,PId_02,PId_03' as set of owner's ID(s) ,then Sp should return "ID01" as output."ID01" is expected result...Bocz only "ID01" has the exact set of OwnersID ('PId_01,PId_02,PId_03')....
Plz let me the query


Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-04-26 : 03:43:33
This is what you want ?


declare @table table
(
ID varchar(4),
OwnerID varchar(6)
)

insert into @table
select 'ID01', 'PId_01' union all
select 'ID01', 'PId_02' union all
select 'ID01', 'PId_03' union all
select 'ID02', 'PId_02' union all
select 'ID02', 'PId_03' union all
select 'ID03', 'PId_04' union all
select 'ID03', 'PId_06'

declare @OwnerList varchar(100),
@OwnerCnt int

select @OwnerList = 'PId_01,PId_02,PId_03'

select @OwnerCnt = count(*)
from dbo.CSVTable(@OwnerList)

select t.ID
from @table t inner join dbo.CSVTable(@OwnerList) o
on t.OwnerID = o.stringval
group by t.ID
having count(*) = @OwnerCnt



KH

Go to Top of Page

deepak_Dotnet
Starting Member

32 Posts

Posted - 2007-04-26 : 05:54:46
Sorry ,That is not working for my criteria...
If user search for this set of owners 'PId_02,PId_03' ,then it will retrieve both :
ID01
ID02

as result..Which is not desire result...BCoz
ID02 is having this set of owners 'PId_02,PId_03' ...but ID01 is not having that exact set of onwers.It is having one more that the exact set of onwers i.e PId_01.

I think i didn't explain the query properly...
Lets forget that previous queries..i will explain the same thing in different way which will be clearly understandable..

I have table exisiting which following columns :
ID and OwnersID.

ID OwnersID
ID_01 PId_01
ID_01 PId_02
ID_01 PId_03
ID_02 PId_01
ID_02 PId_02

So now i need a SP which accept set of owners ID as input parameter
and return ID as output parameter.
When a set of OwnersID is passed ,query should check ID which is having the exact set of owners ID(s) passed.IF any ID is having same exact set of owners,then it should return corrrespondingID as output parameter.

example :
1)when user pass "PId_01,PId_02,PId_03" as input ,then SP should return only ID_01 (not ID_02)
2)when user pass "PId_01,PId_02" as input ,then SP should return only ID_02 (not ID_01).

It means ID should have exact match of owners passed,not subset of values

LIke this we can have many scenario....I Hope i explained my query clealry..plz let me the Sp to do this ...
Go to Top of Page

NeilG
Aged Yak Warrior

530 Posts

Posted - 2007-04-26 : 07:05:23
So to clarify on this you are looking for something simlar to an 'in' operator in a where clause to only output the ID if all of the Owners_id are passed to the sp
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-26 : 07:11:09
IF NOT EXISTS (SELECT * FROM Table WHERE ID = @ID AND OwnerID = @OwnerID)
INSERT Table (ID, OwnerID) VALUES (@ID, @OwnerID)



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-26 : 07:12:11
You would be much better off with a contraint on the two tables.
Then you can handle the error message returned when trying to insert a duplicate value.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

deepak_Dotnet
Starting Member

32 Posts

Posted - 2007-04-26 : 07:26:26
quote:
Originally posted by NeilG

So to clarify on this you are looking for something simlar to an 'in' operator in a where clause to only output the ID if all of the Owners_id are passed to the sp



Yes Exactly...
I just want Sp to return ID who is having those set of all OwnersID which is passed from front end.
It should check for all OwnersID.

for example :
ID OwnersID
ID_01 PId_01
ID_01 PId_02
ID_01 PId_03
ID_02 PId_01
ID_02 PId_02


When a set of OwnersID is passed ,query should check ID which is having the exact set of owners ID(s) passed.IF any ID is having same exact set of owners,then it should return corrrespondingID as output parameter.

example :
1)when user pass "PId_01,PId_02,PId_03" as input ,then SP should return only ID_01 (not ID_02)
2)when user pass "PId_01,PId_02" as input ,then SP should return only ID_02 (not ID_01).

It means ID should have exact match of owners passed,not subset of values

Go to Top of Page

deepak_Dotnet
Starting Member

32 Posts

Posted - 2007-04-26 : 07:31:20
quote:
Originally posted by Peso

IF NOT EXISTS (SELECT * FROM Table WHERE ID = @ID AND OwnerID = @OwnerID)
INSERT Table (ID, OwnerID) VALUES (@ID, @OwnerID)



Peter Larsson
Helsingborg, Sweden



Hi,
The fist reply i got from khtan will give me solution of inserting record after checking whether the set of ownersID is present or not ...
But now I just need query to check ownersID(s) and return the Correspodning matched ID ..
No insertion is required now ....

I already posted how check query should perform..
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-04-26 : 07:48:36
I think you are confusing everybody with what you want.

Your initial requirement is for inserting record.
quote:
Say for example in above table,
We have ownersID "PId_01,PId_02,PId_03" for ID "ID01" and owners IDs "PId_02,PId_03 " for ID "ID02"
so now say when user try to Add owner "PId_01" under "ID02" ,we should not allow user to insert,bcoz it makes two IDs having same set of OwnersID.So Before inserting we have check whether any ID exists which contain exact set of OwnersID.
If preset,don;t allow to insert,just return the ID which have same set of values.
IF doesn't exists ,then allow it insert and returns ID to which new Owner ID "PId_01" is allowed to added.


But later, you change that and wanted query to return the IDs.
quote:
Now i don;t want to insert at this point..
I just want to retrieve ID value if any ID has/contain exact set of OwnersID ,which is pased from front end.


I am not sure what do you want now.

Maybe you should rephrase your question and requirement on what do you need NOW. If any solution posted does meet your requirement, please says so.

And when posting your requirement, please also post the table DDL, some sample data and the expected result. Also explain any business logic etc.


KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-26 : 08:14:53
Horrible design...
-- Prepare sample data
DECLARE @License TABLE (ID VARCHAR(5), OwnersID VARCHAR(6))

INSERT @License
SELECT 'ID_01', 'PId_01' UNION ALL
SELECT 'ID_01', 'PId_02' UNION ALL
SELECT 'ID_01', 'PId_03' UNION ALL
SELECT 'ID_02', 'PId_01' UNION ALL
SELECT 'ID_02', 'PId_02'

DECLARE @UserPass VARCHAR(100)

-- Show the first expected output
SET @UserPass = 'PId_01,PId_02,PId_03'

SELECT l.ID
FROM @License AS l
LEFT JOIN @License AS up ON ',' + @UserPass + ',' LIKE '%,' + l.OwnersID + ',%'
GROUP BY l.ID
HAVING COUNT(DISTINCT l.OwnersID) = 1 + DATALENGTH(@UserPass) - DATALENGTH(REPLACE(@UserPass, ',', ''))

-- Show the second expected output
SET @UserPass = 'PId_01,PId_02'

SELECT l.ID
FROM @License AS l
LEFT JOIN @License AS up ON ',' + @UserPass + ',' LIKE '%,' + l.OwnersID + ',%'
GROUP BY l.ID
HAVING COUNT(DISTINCT l.OwnersID) = 1 + DATALENGTH(@UserPass) - DATALENGTH(REPLACE(@UserPass, ',', ''))
This is basiclly the same as khtan posted 04/26/2007 : 03:43:33
It is only the LEFT JOIN and DISTINCT that differs.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

deepak_Dotnet
Starting Member

32 Posts

Posted - 2007-04-26 : 08:23:08
quote:
Originally posted by khtan

I think you are confusing everybody with what you want.

Your initial requirement is for inserting record.
quote:
Say for example in above table,
We have ownersID "PId_01,PId_02,PId_03" for ID "ID01" and owners IDs "PId_02,PId_03 " for ID "ID02"
so now say when user try to Add owner "PId_01" under "ID02" ,we should not allow user to insert,bcoz it makes two IDs having same set of OwnersID.So Before inserting we have check whether any ID exists which contain exact set of OwnersID.
If preset,don;t allow to insert,just return the ID which have same set of values.
IF doesn't exists ,then allow it insert and returns ID to which new Owner ID "PId_01" is allowed to added.


But later, you change that and wanted query to return the IDs.
quote:
Now i don;t want to insert at this point..
I just want to retrieve ID value if any ID has/contain exact set of OwnersID ,which is pased from front end.


I am not sure what do you want now.

Maybe you should rephrase your question and requirement on what do you need NOW. If any solution posted does meet your requirement, please says so.

And when posting your requirement, please also post the table DDL, some sample data and the expected result. Also explain any business logic etc.


KH








I am sorry if i have confused anybody.I don't have any intension to confuse anybody.
I have explaining my question clearly..OK let me explain again

I have table called "license" with two column "ID" and "OnwersID"
and each ID can have multiple OwnersID.But two ID cann't have same set of OwnersID.
So now When a set of OwnersID is passed ,query should check ID which is having the exact set of owners ID(s) passed.
IF any ID is having same exact set of owners,then it should return corrresponding ID as output parameter,if not return
NULL...

Table value for example :

ID OwnersID

ID_01 PId_01
ID_01 PId_02
ID_01 PId_03
ID_02 PId_01
ID_02 PId_02




example :
1)when user pass "PId_01,PId_02,PId_03" as input ,then SP should return only ID_01 (not ID_02)
2)when user pass "PId_01,PId_02" as input ,then SP should return only ID_02 (not ID_01).

It means ID should have exact match of owners passed,not subset of values

I hope my question is clearly and not confused anybody....
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-26 : 08:26:55
You don't even bother to try my latest suggestion?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

deepak_Dotnet
Starting Member

32 Posts

Posted - 2007-04-26 : 09:05:25
quote:
Originally posted by khtan

2 things here.
1. you need CSVTable from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=CSVTable to split the string 'PId_01,PId_02,PId_05' to 3 rows

2. check for existence of Owner ID during insert

something like

insert into tblLicense (ID, OwnerID)
select @ID, stringval
from CSVTable(@OwnerID) t
where not exists (select * from tblLicense x where x.OwnerID = t.stringval)


@ID, @OwnerID are the variable pass in from front end

EDIT : You can also use fnParseString from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033 to parse the string 'PId_01,PId_02,PId_05'

KH





This Insertion query in most of scenario...
For example
ID01 PId_01
ID02 PId_02
ID01 PId_03

So now When user try to insert either "PId_01" OR PId_03" to new ID "ID03"
Then it will not do insertion,which should suppose to insert...


Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-26 : 09:14:08
I give up. Why try to help a person who don't even answer simple questions?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-04-26 : 09:49:48
This thread is hilarious. Who’s on first?




CODO ERGO SUM
Go to Top of Page

deepak_Dotnet
Starting Member

32 Posts

Posted - 2007-04-26 : 10:22:13
Hi guys,
i am sersiouly looking for solution for my question...IT is very urgent for me..Plz if any body know ,plz reply me back...

I have already explained the question as below :
have table called "license" with two column "ID" and "OnwersID"
and each ID can have multiple OwnersID.But two ID cann't have same set of OwnersID.
So now When a set of OwnersID is passed ,query should check ID which is having the exact set of owners ID(s) passed.
IF any ID is having same exact set of owners,then it should return corrresponding ID as output parameter,if not return
NULL...

Table value for example :

ID OwnersID

ID_01 PId_01
ID_01 PId_02
ID_01 PId_03
ID_02 PId_01
ID_02 PId_02




example :
1)when user pass "PId_01,PId_02,PId_03" as input ,then SP should return only ID_01 (not ID_02)
2)when user pass "PId_01,PId_02" as input ,then SP should return only ID_02 (not ID_01).

It means ID should have exact match of owners passed,not subset of values


PLEASE GIVE ME SOLUTION IF ANY BODY KNOWS.....
Go to Top of Page

nduggan23
Starting Member

42 Posts

Posted - 2007-04-26 : 10:38:11
Peso answered your question.

DECLARE @UserPass VARCHAR(100)

-- Show the first expected output
SET @UserPass = 'PId_01,PId_02,PId_03'

SELECT l.ID
FROM Table AS l
LEFT JOIN Table AS up ON ',' + @UserPass + ',' LIKE '%,' + l.OwnersID + ',%'
GROUP BY l.ID
HAVING COUNT(DISTINCT l.OwnersID) = 1 + DATALENGTH(@UserPass) - DATALENGTH(REPLACE(@UserPass, ',', ''))


Go to Top of Page
    Next Page

- Advertisement -