Solved

SQL query help

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tempdb log keep growing 7 33
Location of Dynamics AX Service accounts in SQL 3 16
SQL USE DATABASE VARIABLE 5 28
First Max value 3 29
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

861 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