Really?
For me it works perfectly see below(MsSQL database Northwind):
Code:
CREATE TABLE Table1
(
ID INT PRIMARY KEY
,Name VARCHAR(50)
)
GO
CREATE TABLE Table2
(
ID INT PRIMARY KEY
,Name VARCHAR(50)
)
GO
CREATE TABLE Table3
(
ID INT PRIMARY KEY
,ID_Table1 INT REFERENCES Table1(ID)
,ID_Table2 INT REFERENCES Table2(ID)
)
GO
INSERT INTO Table1 VALUES( 1,'aaa')
INSERT INTO Table1 VALUES( 2, 'bbb')
INSERT INTO Table1 VALUES( 3 ,'ccc')
INSERT INTO Table2 VALUES( 1,'pro1')
INSERT INTO Table2 VALUES( 2,'pro2')
INSERT INTO Table2 VALUES( 3,'pro3')
INSERT INTO Table3 VALUES(1 , 1 , 2)
INSERT INTO Table3 VALUES(2 , 2 , 2)
INSERT INTO Table3 VALUES(3 , 3 , 1)
GO
SELECT T1.Name, T2.Name FROM Table1 as T1, Table2 as T2, Table3 as T3
WHERE T3.ID_Table1 = T1.ID and T3.ID_Table2 = T2.ID
Output:
Code:
Name Name
aaa pro2
bbb pro2
ccc pro1
(3 row(s) affected)
CREATE FUNCTION dbo.FN_GetConcatedNames(@i_ID INT) RETURNS VARCHAR(4000)
AS
BEGIN
DECLARE
@_Str VARCHAR(4000)
SELECT @_Str=''
SELECT @_Str=@_Str+IsNull(','+t2.[Name],'')
FROM Table3 t3,Table2 t2
WHERE t3.ID_Table2=t2.ID AND T3.ID_Table1=@i_ID
RETURN SUBSTRING(@_Str,2,4000)
END
GO
SELECT T1.Name,dbo.FN_GetConcatedNames(T1.ID) Names2
FROM Table1 T1
CREATE FUNCTION dbo.FN_GetConcatedNames(@i_ID INT) RETURNS VARCHAR(4000)
AS
BEGIN
DECLARE
@_Str VARCHAR(4000)
SELECT @_Str=''
SELECT @_Str=@_Str+IsNull(','+t2.[Name],'')
FROM Table3 t3,Table2 t2
WHERE t3.ID_Table2=t2.ID AND T3.ID_Table1=@i_ID
RETURN SUBSTRING(@_Str,2,4000)
END
GO
SELECT T1.Name,dbo.FN_GetConcatedNames(T1.ID) Names2
FROM Table1 T1
Hope it helps.
I'm not sure... I mean, I was hoping for something easier. I have this app in VC++ and I'm using CDaoDatabase and CDaoRecordset and I'm not sure how can use your code.
Are you talking about a cross tab (or maybe its called pivot) query?
Sorry, I don't remember what a cross table is...
I have three tables, one with movies, one with actors and one that links actors to movies, since more than one actor plays in a movie. So basically what I want is select a movie name from table 1 and all the actors that plays in it from table 2 but the result be a single record:
I set up a small Access database, with three tables, one has an example similar to an entry earlier, where we have one table, with the name listed multiple times, and we wanted to count it. This can be seen using Query 1.
I think though the main question was in relation to having a table of films, a table of actors and then a table which linked the two together. I set these up, and then set up query 2, which will give you something close to what you're looking for, or maybe not.
Right now I can't test it, because I have Access '97 on this computer and it doesn't recognize the format, but I'll test it tonight and let you know about it tomorrow.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.