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 |
|
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 OwnerIDID01 PId_01ID01 PId_02ID01 PId_03ID02 PId_02ID02 PId_03Assume 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 |
|
|
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_01ID01 PId_02ID01 PId_03ID02 PId_02ID02 PId_03When 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 |
 |
|
|
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 |
 |
|
|
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 OwnerIDID01 PId_01ID01 PId_02ID01 PId_03ID02 PId_02ID02 PId_03ID03 PId_04ID03 PId_06In above exampleIF 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 |
 |
|
|
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 @tableselect 'ID01', 'PId_01' union allselect 'ID01', 'PId_02' union allselect 'ID01', 'PId_03' union allselect 'ID02', 'PId_02' union allselect 'ID02', 'PId_03' union allselect 'ID03', 'PId_04' union allselect 'ID03', 'PId_06'declare @OwnerList varchar(100), @OwnerCnt intselect @OwnerList = 'PId_01,PId_02,PId_03'select @OwnerCnt = count(*)from dbo.CSVTable(@OwnerList)select t.IDfrom @table t inner join dbo.CSVTable(@OwnerList) o on t.OwnerID = o.stringvalgroup by t.IDhaving count(*) = @OwnerCnt KH |
 |
|
|
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 : ID01ID02 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 OwnersIDID_01 PId_01ID_01 PId_02ID_01 PId_03ID_02 PId_01ID_02 PId_02So now i need a SP which accept set of owners ID as input parameterand 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 valuesLIke this we can have many scenario....I Hope i explained my query clealry..plz let me the Sp to do this ... |
 |
|
|
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 |
 |
|
|
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 LarssonHelsingborg, Sweden |
 |
|
|
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 LarssonHelsingborg, Sweden |
 |
|
|
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 OwnersIDID_01 PId_01ID_01 PId_02ID_01 PId_03ID_02 PId_01ID_02 PId_02When 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 |
 |
|
|
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 LarssonHelsingborg, 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.. |
 |
|
|
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 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 08:14:53
|
Horrible design...-- Prepare sample dataDECLARE @License TABLE (ID VARCHAR(5), OwnersID VARCHAR(6))INSERT @LicenseSELECT 'ID_01', 'PId_01' UNION ALLSELECT 'ID_01', 'PId_02' UNION ALLSELECT 'ID_01', 'PId_03' UNION ALLSELECT 'ID_02', 'PId_01' UNION ALLSELECT 'ID_02', 'PId_02'DECLARE @UserPass VARCHAR(100)-- Show the first expected outputSET @UserPass = 'PId_01,PId_02,PId_03'SELECT l.IDFROM @License AS lLEFT JOIN @License AS up ON ',' + @UserPass + ',' LIKE '%,' + l.OwnersID + ',%'GROUP BY l.IDHAVING COUNT(DISTINCT l.OwnersID) = 1 + DATALENGTH(@UserPass) - DATALENGTH(REPLACE(@UserPass, ',', ''))-- Show the second expected outputSET @UserPass = 'PId_01,PId_02'SELECT l.IDFROM @License AS lLEFT JOIN @License AS up ON ',' + @UserPass + ',' LIKE '%,' + l.OwnersID + ',%'GROUP BY l.IDHAVING COUNT(DISTINCT l.OwnersID) = 1 + DATALENGTH(@UserPass) - DATALENGTH(REPLACE(@UserPass, ',', '')) This is basiclly the same as khtan posted 04/26/2007 : 03:43:33It is only the LEFT JOIN and DISTINCT that differs.Peter LarssonHelsingborg, Sweden |
 |
|
|
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 againI 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 OwnersIDID_01 PId_01ID_01 PId_02ID_01 PId_03ID_02 PId_01ID_02 PId_02example : 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 valuesI hope my question is clearly and not confused anybody.... |
 |
|
|
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 LarssonHelsingborg, Sweden |
 |
|
|
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 rows2. check for existence of Owner ID during insertsomething likeinsert into tblLicense (ID, OwnerID)select @ID, stringvalfrom CSVTable(@OwnerID) twhere not exists (select * from tblLicense x where x.OwnerID = t.stringval) @ID, @OwnerID are the variable pass in from front endEDIT : 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_01ID02 PId_02ID01 PId_03So 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... |
 |
|
|
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 LarssonHelsingborg, Sweden |
 |
|
|
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 |
 |
|
|
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 OwnersIDID_01 PId_01ID_01 PId_02ID_01 PId_03ID_02 PId_01ID_02 PId_02example : 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 valuesPLEASE GIVE ME SOLUTION IF ANY BODY KNOWS..... |
 |
|
|
nduggan23
Starting Member
42 Posts |
Posted - 2007-04-26 : 10:38:11
|
| Peso answered your question. DECLARE @UserPass VARCHAR(100)-- Show the first expected outputSET @UserPass = 'PId_01,PId_02,PId_03'SELECT l.IDFROM Table AS lLEFT JOIN Table AS up ON ',' + @UserPass + ',' LIKE '%,' + l.OwnersID + ',%'GROUP BY l.IDHAVING COUNT(DISTINCT l.OwnersID) = 1 + DATALENGTH(@UserPass) - DATALENGTH(REPLACE(@UserPass, ',', '')) |
 |
|
|
Next Page
|
|
|
|
|