Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1) No matter how many times the photoblob appears in your result set it will be read(from Disk to memory in the server) only once, There are optimizations built in to make sure this is happening.</p> <p>2) However it can be transported(from server to client) multiple times, there are no optimization built in for that.</p> <p>3) The best solution would be to wrap this as a stored procedure that returns 2 record sets, and you do the join in the clinet code, this approach is different from running 2 queries which needs two round trip.</p> <p>4) if you dont want to do that you can get all the article ids of the user in a CSV format, and then you can easily split the csv into separate strings in the client code. </p> <p>Here is the sample output </p> <pre><code>UserId UserName UserPhoto CSV_ArticleId CSV_ArticleText ------- --------- ---------- ------------------------ ---------------------------- UserId1 UserName1 UserPhoto1 ",ArticleId1,ArticleId2" ",ArticleText1,ArticleText2" UserId2 UserName2 UserPhoto2 ",ArticleId3" ",ArticleText3" </code></pre> <p>here is how you can do it. Run the code verbatim on a test database and you can see the result</p> <pre><code>CREATE TABLE Users(UserId int , UserName nvarchar(256), UserPhoto nvarchar(256)) CREATE TABLE Articles (ArticleId int , UserId int , ArticleText nvarchar(256)) INSERT INTO Users(UserId,UserName,UserPhoto) VALUES (2,'2a','2pa') INSERT INTO Users(UserId,UserName,UserPhoto) VALUES (1,'a','pa') INSERt INTO Articles (ArticleId, UserId, ArticleText) VALUES (2,2,'text2') INSERt INTO Articles (ArticleId, UserId, ArticleText) VALUES (1,2,'text1') ;WITH tArticles AS (SELECT ArticleId, UserId, ArticleText FROM Articles) SELECT UserId, UserName, UserPhoto, (SELECT TOP 1 LTRIM( (SELECT ',' + CONVERT(nvarchar(256),A.ArticleId) FROM Articles A WHERE U.UserId = A.UserId ORDER BY A.ArticleId FOR XML PATH('')) )) as CSV_ArticleId, (SELECT TOP 1 LTRIM( (SELECT ',' + CONVERT(nvarchar(256),A.ArticleText) FROM Articles A WHERE U.UserId = A.UserId ORDER BY A.ArticleId FOR XML PATH('')) )) as CSV_ArticleText FROM Users U </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload