kevin.upshaw Posted June 9, 2012 Share Posted June 9, 2012 Hi.Wonder if you can help, am quite new to SQL. In simple terms, I have a transaction table - and in that table are references to other related tables. In this simple query I am trying to obtain the first name and last name from the user ID for a transaction. SELECT transaction.transactionId, user.firstName, user.lastNameFROM [transaction]INNER JOIN [user]ON transaction.userId = user.userIdORDER BY user.userId However I get the response "incorrect syntax near the keyword transaction". I then try: SELECT 'transaction'.transactionId, user.firstName, user.lastNameFROM [transaction]INNER JOIN [user]ON 'transaction'.userId = user.userIdORDER BY user.userId I then receive "cannot call methods on varchar" - which I am assuming SQL thinks I am referring to text - rather than table names. I then tried: SELECT dbo.transaction.transactionId, user.firstName, user.lastNameFROM [transaction]INNER JOIN [user]ON dbo.transaction.userId = user.userIdORDER BY user.userId However no joy - I get the initial error message. Is someone able to point me in the right direction as to where I am going wrong? I have quite a few joins I need to do - but I thought if I could get this fixed first that would be a start.Cheers. Kev Link to comment Share on other sites More sharing options...
niche Posted June 9, 2012 Share Posted June 9, 2012 (edited) Your code doesn't seem to follow the tutorial. Please see: http://www.w3schools..._join_inner.asp Then check back. Edited June 10, 2012 by niche Link to comment Share on other sites More sharing options...
kevin.upshaw Posted June 10, 2012 Author Share Posted June 10, 2012 Hi, I tried the following: SELECT transaction.transactionId, user.firstName, user.lastNameFROM transactionINNER JOIN userON transaction.userId=user.userIdORDER BY user.userId No joy. It wouldn't perhaps be because my table names are reserved names? I know "user" is. Thanks. Kevin Link to comment Share on other sites More sharing options...
niche Posted June 10, 2012 Share Posted June 10, 2012 what error messages are you getting now? Your code looks right depending on the environment you're using. Are you using php, myphpadmin, terminal...? Link to comment Share on other sites More sharing options...
kevin.upshaw Posted June 10, 2012 Author Share Posted June 10, 2012 I receive the error: "Incorrect syntax near the keyword 'transaction'". I am using WebMatrix - Using ASP.NET Web Pages - Razor Syntax (C#). However I am trying to do the query ion the WebMatrix environment before trying to code it into a web page/ Am running SQL Express on my laptop. Could perhaps try installing Visual Studio 2010 and running the query from there? Link to comment Share on other sites More sharing options...
niche Posted June 10, 2012 Share Posted June 10, 2012 Can you run it from a php script? Link to comment Share on other sites More sharing options...
kevin.upshaw Posted June 10, 2012 Author Share Posted June 10, 2012 Cracked it!It took someone else to go through it with me to realise what it is. I thought "transaction" and "user" were displayed in a colour they shouldn't be in on the WebMatrix software. So I created a bogus "transaction1" and "user1" table and put some fake data in them - then created the relationship between them. The query works absolutely fine now - so I'm guessing it was because I was using reserved words as my table names - will make sure I use proper names next time, would have saved so much time! Thank's very much for your help. Much appreciated. Kevin Link to comment Share on other sites More sharing options...
niche Posted June 10, 2012 Share Posted June 10, 2012 You're welcome. Feel free to check back any time you have a code question or want to learn something new. There are plenty of people that are far more knowledgeable than me available to help you. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now