Solved

SQL query help

Posted on 2013-01-11
5
463 Views
Last Modified: 2013-01-11
Hello there,

I have this query which when i try to run i get syntax error. can somebody help me figure it out.

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'SELECT'.

SELECT reportusercredential.id, reportusercredential.username, 
reportuserpermission.id as reportuserpermissionid, reportusercredential.password, 
reportusertype.id as usertypeid, Section.id as sectionid,  reportusertype.name as usertypename, 
Branch.name AS branchname,  Section.name as sectionname, 
SELECT STUFF((SELECT ',' + name FROM Branch b where ru.branchid = b.id FOR XML PATH ('')), 1, 1, '') 
as branchname1
 FROM reportusercredential,reportuserpermission ru, Section, Branch b, reportusertype 
 WHERE reportuserpermission.reportuserid=reportusercredential.id 
AND reportuserpermission.sectionid=Section.id AND 
reportuserpermission.branchid = Branch.id AND reportusertype.id = reportusercredential.usertypeid 
AND (('1'='1')) GROUP BY reportusercredential.id

Open in new window

0
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Accepted Solution

by:
krtyknmsql earned 170 total points
ID: 38766625
Just remove the SELECT statement before the STUFF.
SELECT reportusercredential.id, reportusercredential.username, 
reportuserpermission.id as reportuserpermissionid, reportusercredential.password, 
reportusertype.id as usertypeid, Section.id as sectionid,  reportusertype.name as usertypename, 
Branch.name AS branchname,  Section.name as sectionname, 
STUFF((SELECT ',' + name FROM Branch b where ru.branchid = b.id FOR XML PATH ('')), 1, 1, '') 
as branchname1
 FROM reportusercredential,reportuserpermission ru, Section, Branch b, reportusertype 
 WHERE reportuserpermission.reportuserid=reportusercredential.id 
AND reportuserpermission.sectionid=Section.id AND 
reportuserpermission.branchid = Branch.id AND reportusertype.id = reportusercredential.usertypeid 
AND (('1'='1')) GROUP BY reportusercredential.id

Open in new window

0
 

Author Comment

by:zolf
ID: 38766758
thanks now i get this error

Msg 8120, Level 16, State 1, Line 3
Column 'reportusercredential.username' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


SELECT reportusercredential.id, reportusercredential.username,
ru.id as reportuserpermissionid, reportusercredential.password,
reportusertype.id as usertypeid, Section.id as sectionid,  reportusertype.name as usertypename,
b.name AS branchname,  Section.name as sectionname,
STUFF((SELECT ',' + name FROM Branch b where ru.branchid = b.id FOR XML PATH ('')), 1, 1, '')
as branchname1
 FROM reportusercredential,reportuserpermission ru, Section, Branch b, reportusertype
 WHERE ru.reportuserid=reportusercredential.id
AND ru.sectionid=Section.id AND
ru.branchid = b.id AND reportusertype.id = reportusercredential.usertypeid
AND (('1'='1')) GROUP BY reportusercredential.id
0
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 165 total points
ID: 38766994
Try
SELECT distinct -- the distinct might not be required, but I can't tell since I'm not familiar with your data, try both ways
	reportusercredential.id, 
	reportusercredential.username, 
	reportuserpermission.id as reportuserpermissionid, 
	reportusercredential.password, 
	reportusertype.id as usertypeid, 
	Section.id as sectionid,  
	reportusertype.name as usertypename, 
	Section.name as sectionname, 
	STUFF((SELECT ',' + name FROM Branch b where ru.branchid = b.id FOR XML PATH ('')), 1, 1, '') as branchname1
FROM 	reportusercredential,
	reportuserpermission ru, 
	Section, 
	reportusertype 
WHERE 	reportuserpermission.reportuserid=reportusercredential.id 
	AND reportuserpermission.sectionid=Section.id 
	AND reportusertype.id = reportusercredential.usertypeid 

Open in new window

0
 
LVL 5

Assisted Solution

by:RehanYousaf
RehanYousaf earned 165 total points
ID: 38767165
your code is
SELECT 
	reportusercredential.id, 
	reportusercredential.username, 
	reportuserpermission.id AS reportuserpermissionid, 
	reportusercredential.password, 
	reportusertype.id AS usertypeid, 
	Section.id AS sectionid,  
	reportusertype.name AS usertypename, 
	Branch.name AS branchname,  
	Section.name AS sectionname, 
	STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id FOR XML PATH ('')), 1, 1, '') AS branchname1
FROM 
	reportusercredential,
	reportuserpermission ru, 
	SECTION, 
	Branch b, 
	reportusertype 
WHERE 
	reportuserpermission.reportuserid = reportusercredential.id 
	AND reportuserpermission.sectionid=Section.id 
	AND reportuserpermission.branchid = Branch.id 
	AND reportusertype.id = reportusercredential.usertypeid 
	AND (('1'='1'))
GROUP BY 
	reportusercredential.id
	

Open in new window


question is why do you need
(('1'='1'))
and
GROUP BY reportusercredential.id


your query will work if you do

SELECT 
	reportusercredential.id, 
	reportusercredential.username, 
	reportuserpermission.id AS reportuserpermissionid, 
	reportusercredential.password, 
	reportusertype.id AS usertypeid, 
	Section.id AS sectionid,  
	reportusertype.name AS usertypename, 
	Branch.name AS branchname,  
	Section.name AS sectionname, 
	STUFF((SELECT ',' + name FROM Branch b WHERE ru.branchid = b.id FOR XML PATH ('')), 1, 1, '') AS branchname1
FROM 
	reportusercredential,
	reportuserpermission ru, 
	SECTION, 
	Branch b, 
	reportusertype 
WHERE 
	reportuserpermission.reportuserid = reportusercredential.id 
	AND reportuserpermission.sectionid=Section.id 
	AND reportuserpermission.branchid = Branch.id 
	AND reportusertype.id = reportusercredential.usertypeid 
	--AND (('1'='1'))
--GROUP BY 
--	reportusercredential.id
	

Open in new window

0
 

Author Closing Comment

by:zolf
ID: 38767341
thanks
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

733 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