Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL statement help

Posted on 2008-11-17
9
Medium Priority
?
258 Views
Last Modified: 2012-05-05
Hello experts.

I have a table storing user updates and another table storing icons for those updates. At the moment it works where it gets all updates for a user based on User id. I am now wanting to join another table "aspnet_Membership" and to say.

only show user updates where aspnet_Membership.CreateDate > tbl_latestupdates.DateAdded

records from the aspnet_Membership table are retrieved from a variable called strUserId so I'd imagine it would be WHERE aspnet_Membership = " + strUserId

Can anyone help?

Thanks


sb.Append("SELECT TOP 8 ");
        sb.Append("tbl_latestupdates.LULinkText_" + up.UserLanguage + " LULinkText, ");
        sb.Append("tbl_latestupdates.LULinkURL, ");
        sb.Append("tbl_latestupdates.DateAdded, ");
        sb.Append("tbl_latestupdates.LUUserId, ");
        sb.Append("tbl_latestupdatesicons.LUIconPath ");
        sb.Append("FROM tbl_latestupdates ");
        sb.Append("INNER JOIN tbl_latestupdatesicons ");
        sb.Append("ON tbl_latestupdates.LUImageId = tbl_latestupdatesicons.LUIconId ");
        sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = - 1) ");
        sb.Append("ORDER BY tbl_latestupdates.DateAdded DESC");

Open in new window

0
Comment
Question by:thomasmutton
  • 4
  • 3
  • 2
9 Comments
 
LVL 27

Accepted Solution

by:
sujith80 earned 1000 total points
ID: 22974252
Assuming that strUserId returns a single row from the aspNetMembership table.
	sb.Append("SELECT TOP 8 ");
        sb.Append("tbl_latestupdates.LULinkText_" + up.UserLanguage + " LULinkText, ");
        sb.Append("tbl_latestupdates.LULinkURL, ");
        sb.Append("tbl_latestupdates.DateAdded, ");
        sb.Append("tbl_latestupdates.LUUserId, ");
        sb.Append("tbl_latestupdatesicons.LUIconPath ");
        sb.Append("FROM tbl_latestupdates ");
        sb.Append("INNER JOIN tbl_latestupdatesicons ");
        sb.Append("ON tbl_latestupdates.LUImageId = tbl_latestupdatesicons.LUIconId ");
	sb.Append("CROSS JOIN aspnet_Membership ");
        sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = - 1) ");
        sb.Append("AND aspnet_Membership = " + strUserId + " ");
        sb.Append("ORDER BY tbl_latestupdates.DateAdded DESC");
 

Open in new window

0
 

Author Comment

by:thomasmutton
ID: 22974273
Hi, I tried this but it came up with this error.

Incorrect syntax near 'fa'.

on

GridView1.DataBind();
sb.Append("SELECT TOP 8 ");
        sb.Append("tbl_latestupdates.LULinkText_" + up.UserLanguage + " LULinkText, ");
        sb.Append("tbl_latestupdates.LULinkURL, ");
        sb.Append("tbl_latestupdates.DateAdded, ");
        sb.Append("tbl_latestupdates.LUUserId, ");
        sb.Append("tbl_latestupdatesicons.LUIconPath ");
        sb.Append("FROM tbl_latestupdates ");
        sb.Append("INNER JOIN tbl_latestupdatesicons ");
        sb.Append("ON tbl_latestupdates.LUImageId = tbl_latestupdatesicons.LUIconId ");
        sb.Append("CROSS JOIN aspnet_Membership ");
        sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = - 1) ");
        sb.Append("AND aspnet_Membership.UserId = " + strUserId + " ");
        sb.Append("ORDER BY tbl_latestupdates.DateAdded DESC");

Open in new window

0
 

Author Comment

by:thomasmutton
ID: 22974281
and yes, every record in the aspnet_Membership table has its own unique UserId
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:aflockhart
ID: 22974341
If you are comparing string values you need to enclose them in single quotes.

e.g.  (notice the extra quote marks before and after strUserId) ...

sb.Append("AND aspnet_Membership.UserId = '" + strUserId + "' ");
0
 
LVL 27

Expert Comment

by:sujith80
ID: 22974402
yes the single quotes should do.
0
 

Author Comment

by:thomasmutton
ID: 22974477
Hi guys, putting the single quotes worked fine. I am still having problems with this tho.

Basically if we wanted to have the update sent to everyone, we would put the latest update User Id as -1

I think there is something wrong with the logic as it says WHERE the user id = 13 OR latest update UserId = -1 AND Membership User Id = whatever

should the latest update User Id be before the cross join?
StringBuilder sb = new StringBuilder();
        sb.Append("SELECT TOP 8 ");
        sb.Append("tbl_latestupdates.LULinkText_" + up.UserLanguage + " LULinkText, ");
        sb.Append("tbl_latestupdates.LULinkURL, ");
        sb.Append("tbl_latestupdates.DateAdded, ");
        sb.Append("tbl_latestupdates.LUUserId, ");
        sb.Append("tbl_latestupdatesicons.LUIconPath ");
        sb.Append("FROM tbl_latestupdates ");
        sb.Append("INNER JOIN tbl_latestupdatesicons ");
        sb.Append("ON tbl_latestupdates.LUImageId = tbl_latestupdatesicons.LUIconId ");
        sb.Append("CROSS JOIN aspnet_Membership ");
        sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = -1) ");
        sb.Append("AND aspnet_Membership.UserId = '" + strUserId + "' ");
        sb.Append("AND tbl_latestupdates.DateAdded > aspnet_Membership.CreateDate ");
        sb.Append("ORDER BY tbl_latestupdates.DateAdded DESC");

Open in new window

0
 

Author Comment

by:thomasmutton
ID: 22974636
I know what is wrong with this but i dont know how to solve it.

if tbl_latestupdates.LUUserId = -1 then the aspnet_Membership record will not exist.
sb.Append("WHERE (tbl_latestupdates.LUUserId = @UserProfileId) ");
        sb.Append("OR (tbl_latestupdates.LUUserId = -1) ");
        sb.Append("AND aspnet_Membership.UserId = '" + strUserId + "' ");
        sb.Append("AND tbl_latestupdates.DateAdded > aspnet_Membership.CreateDate ");

Open in new window

0
 
LVL 17

Expert Comment

by:aflockhart
ID: 22974736
Depends why you need to link to the aspnet_Membership  table in the first place, and how you want to handle the situation where there is no membership record.

I would look at bracketing your conditions, so you only check the records from aspnet_Membership  when the value of tbl_latestupdates.LUUserId  is not -1  .



0
 
LVL 17

Assisted Solution

by:aflockhart
aflockhart earned 1000 total points
ID: 22974749
e.g.

WHERE ( (userID =@userprofileID) and (membership.userid=@strUnserID) and (dateadded > membership.createdate )  )
OR
(userid=-1)

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question