473,412 Members | 4,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

MS sql server Problem

25
Hello My name is Oren and I'm new here,

I have a problem and i'm trying to solve it for a couple of days.

I have a table Called WORKERS and inside a column named Projects And in this column i have values like "1,5,7,12,15" (ID)

And i need to get all the Wrokers details that are connected to some project ID supose 12,

And then bring all the projects names that this worker was part of.

for example:
supose the project i'm searching is 12
I have a worker record:

Fname Lname Age Projects
Tony Mitz 25 1,5,7,12,15

The resault should be:

Tony Mits 25
Projects:

projectName1, projectName2 , projectName3 ,projectName4 ,projectName 5

How to Solve this??

I hope somebody will help me,

Thanks
Jan 23 '07 #1
9 1960
radcaesar
759 Expert 512MB
Make a project details in to a seperate table and wworker details in another table. Relate it with the project ID (Foreign Key)

Then get the values like

select * from workers w, projects p where w.id=p.id

got it ?

:)
Hello My name is Oren and I'm new here,

I have a problem and i'm trying to solve it for a couple of days.

I have a table Called WORKERS and inside a column named Projects And in this column i have values like "1,5,7,12,15" (ID)

And i need to get all the Wrokers details that are connected to some project ID supose 12,

And then bring all the projects names that this worker was part of.

for example:
supose the project i'm searching is 12
I have a worker record:

Fname Lname Age Projects
Tony Mitz 25 1,5,7,12,15

The resault should be:

Tony Mits 25
Projects:

projectName1, projectName2 , projectName3 ,projectName4 ,projectName 5

How to Solve this??

I hope somebody will help me,

Thanks
Jan 23 '07 #2
obarash
25
got It but it's a little bit tricky,
because every worker is connected to several prjects, and every project is connected to several workers, so in your example ( or answer) i will have many many records and quite a mess in the db.

let me explain myself again:

a worker have a field named Projects and it's values are : "5,7,12,16"

hope now it is more clarified

Or mybe I didn't understand you

Make a project details in to a seperate table and wworker details in another table. Relate it with the project ID (Foreign Key)

Then get the values like

select * from workers w, projects p where w.id=p.id

got it ?

:)
Jan 23 '07 #3
Motoma
3,237 Expert 2GB
You make it sound like you have a Workers table and a Projects table. I am working off this assumption.

What you will most likely want to do, is create a third table, I'll call it w_p_rel, which will hold a worker id wID, and a project id pID.

This table will contain the many to many relationship of projects to workers. For ever project/worker combination, there will be one entry in the w_p_rel table.

When you need to get all of the projects a worker is part of:
Expand|Select|Wrap|Line Numbers
  1. SELECT project.*
  2. FROM project, w_p_rel 
  3. WHERE w_p_rel.wID = <workerID>
  4. AND w_p_rel.pID = project.ID
  5.  
And when you need to get all of the workers for a specific project:
Expand|Select|Wrap|Line Numbers
  1. SELECT workers.*
  2. FROM workers, w_p_rel 
  3. WHERE w_p_rel.pID = <projectID>
  4. AND w_p_rel.wID = worker.ID
  5.  
Hope this helps,
Motoma
Jan 23 '07 #4
obarash
25
Thank you,

but there isn't way to solve without changing the db?

You make it sound like you have a Workers table and a Projects table. I am working off this assumption.

What you will most likely want to do, is create a third table, I'll call it w_p_rel, which will hold a worker id wID, and a project id pID.

This table will contain the many to many relationship of projects to workers. For ever project/worker combination, there will be one entry in the w_p_rel table.

When you need to get all of the projects a worker is part of:
Expand|Select|Wrap|Line Numbers
  1. SELECT project.*
  2. FROM project, w_p_rel 
  3. WHERE w_p_rel.wID = <workerID>
  4. AND w_p_rel.pID = project.ID
  5.  
And when you need to get all of the workers for a specific project:
Expand|Select|Wrap|Line Numbers
  1. SELECT workers.*
  2. FROM workers, w_p_rel 
  3. WHERE w_p_rel.pID = <projectID>
  4. AND w_p_rel.wID = worker.ID
  5.  
Hope this helps,
Motoma
Jan 23 '07 #5
almaz
168 Expert 100+
Thank you,

but there isn't way to solve without changing the db?
We can provide tricky SQL statements that may solve your current problem, but the main problem is a design problem.Current design doesn't confirm even to the first normal form. You described a simple many-to-many relationship between workers and projects, and it has a standard solution as Motoma described.
The best choice here is to review the DB structure, because later there may occur another challenges like "when particular project is deleted, delete all references to it" and so on.
Jan 23 '07 #6
Motoma
3,237 Expert 2GB
We can provide tricky SQL statements that may solve your current problem, but the main problem is a design problem.Current design doesn't confirm even to the first normal form. You described a simple many-to-many relationship between workers and projects, and it has a standard solution as Motoma described.
The best choice here is to review the DB structure, because later there may occur another challenges like "when particular project is deleted, delete all references to it" and so on.
Thanks for your support, as well as the comment on normalization. I completely forgot to mention this.

A great place to read up on this is on this UTexas page.
A particularly good page to read is the one on Normalization as it provides numerous examples of both the how and the why of normalization.
This page talks about the process of resolving Many-to-Many relationships.
Jan 23 '07 #7
iburyak
1,017 Expert 512MB
If you still insist on doing select your way try this:

1. Create a function:

[PHP]CREATE FUNCTION SplitProjects(@String nvarchar(4000), @Delimiter char(1))
RETURNS varchar(4000)
AS

BEGIN
DECLARE @Results varchar(4000)
DECLARE @INDEX INT
DECLARE @SLICE nvarchar(4000)

SELECT @INDEX = 1, @Results = ''
WHILE @INDEX !=0


BEGIN
-- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER
SELECT @INDEX = CHARINDEX(@Delimiter,@STRING)
-- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE
IF @INDEX !=0
SELECT @SLICE = LEFT(@STRING,@INDEX - 1)
ELSE
SELECT @SLICE = @STRING
-- PUT THE ITEM INTO THE RESULTS
SELECT @Results = @Results + (select project from projects where id = @SLICE) + ','

-- CHOP THE ITEM REMOVED OFF THE MAIN STRING
SELECT @STRING = RIGHT(@STRING,LEN(@STRING) - @INDEX)
-- BREAK OUT IF WE ARE DONE
IF LEN(@STRING) = 0 BREAK
END
-- Remove last comma
Select @Results = SUBSTRING(@Results,1,len(@Results) - 1)
RETURN @Results
END [/PHP]

2. Execute select with function:

[PHP]select *, dbo.SplitProjects(Projects,',') from Workers[/PHP]
Jan 23 '07 #8
obarash
25
thank you,
I will try to get an access to the db, I'm not allowed to do changes so this is why i asked my question.

but now I see that I right and so are you.
the designing from the begining is defected.

thank you.


If you still insist on doing select your way try this:

1. Create a function:

[PHP]CREATE FUNCTION SplitProjects(@String nvarchar(4000), @Delimiter char(1))
RETURNS varchar(4000)
AS

BEGIN
DECLARE @Results varchar(4000)
DECLARE @INDEX INT
DECLARE @SLICE nvarchar(4000)

SELECT @INDEX = 1, @Results = ''
WHILE @INDEX !=0


BEGIN
-- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER
SELECT @INDEX = CHARINDEX(@Delimiter,@STRING)
-- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE
IF @INDEX !=0
SELECT @SLICE = LEFT(@STRING,@INDEX - 1)
ELSE
SELECT @SLICE = @STRING
-- PUT THE ITEM INTO THE RESULTS
SELECT @Results = @Results + (select project from projects where id = @SLICE) + ','

-- CHOP THE ITEM REMOVED OFF THE MAIN STRING
SELECT @STRING = RIGHT(@STRING,LEN(@STRING) - @INDEX)
-- BREAK OUT IF WE ARE DONE
IF LEN(@STRING) = 0 BREAK
END
-- Remove last comma
Select @Results = SUBSTRING(@Results,1,len(@Results) - 1)
RETURN @Results
END [/PHP]

2. Execute select with function:

[PHP]select *, dbo.SplitProjects(Projects,',') from Workers[/PHP]
Jan 24 '07 #9
hi,
here is a tricky SQL i am not sure it will give correct result set always, but u can use it for ur problem.

Select * from Workers inner join Projects
ON ProjectIds LIKE '%'+CAST(ProjectId AS VARCHAR)+'%'
where ProjectId = 12

it would be better if u can save the projectIds starting and ending with zeros like
0,1,4,2,12,0

but any way its better to change the DB structure
Jan 24 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
2
by: epaetz | last post by:
I'm getting Not associated with a trusted SQL Server connection errors on a .Net windows service I wrote, when it's running on my application server. It's not a problem with mixed mode...
0
by: AlessanBar | last post by:
Hello Friends !! I have a strange problem, and I need to know what would be the source of this. I have a laptop computer with the following configuration: Pentium III Brand : Toshiba Speed :...
3
by: Michael | last post by:
Dear All I have problem with my database server which running SQL server 2000. The server running very slow. The worst case, to save a record required more than 20-30 seconds. Since this...
2
by: Helge Kalnes | last post by:
We are running an ASP.NET application on a cluster of 3 web-servers, using the Network Load Balancing feature of Application Center. We have synchronized the machineKey in machine.config on the 3...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
22
by: EP | last post by:
When running my asp.net hosting service (asp.net without IIS), on server 2003 with IIS not installed, I get the following when trying to process a request. "System.DllNotFoundException: Unable to...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.