Solved

SQL query help

Posted on 2013-01-11
5
459 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now