Solved

SQL query help

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 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

13 Experts available now in Live!

Get 1:1 Help Now