[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

SQL Query Error

Asked by smyers051972 in SQL Query Syntax

Tags: MSSQL 2000 Dynamic Query Syntax

Hi all,

I have a dynamic query I put together and when I make seperate loops for @SQLA and @SQLB its fine, but when combined to the same query statement it bombs out with the following error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.


Any help would be appreciated in solving this! I believe its part of the second dynamic query (@sqlb) thats terminating, my goal would be to make it all 1 dynamic query, @sqla within the same loop only, but have been unsucessful.


Thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
IF EXISTS 	(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#slotreport]')) DROP TABLE #slotreport
--IF EXISTS 	(SELECT * FROM tempdb..sysobjects WHERE id = object_id(N'[tempdb]..[#slotsrev]')) DROP TABLE #slotsrev
SET NOCOUNT ON
SET QUOTED_IDENTIFIER ON
DECLARE	@Casino 	VARCHAR(20),
	@MonthCount	tinyint,
	@VarMonth	VARCHAR(3),
	@BEGINDATE	datetime,
	@ENDDATE	datetime,
	@DateDays	INT,
	@loopvar	tinyint,
	@sqla 		varchar(5000),
	@sqlb 		varchar(5000)
 
SET	@monthCount 	= 1 -- the depth of the calculations. 12 months max!
SET	@Casino 	= (SELECT LTRIM(RTRIM(RIGHT(DESCRIPTION, LEN(DESCRIPTION)-9))) FROM GEN_CASINO (nolock))
create table 	#slotreport
(
		Casino		varchar(20),
		slotnumber	int,
		slotmast_id	int,
		par		real,
		LocationString	varchar(40),
		description	varchar(254),
		denomination	money,
		OnFloor		smalldatetime,
		StyleDesc	varchar(30),
		Manufacturer	varchar(254)
)
 
insert into 	#slotreport
	(
	Casino,
	slotnumber,
	slotmast_id,	
	par,
	LocationString,
	description,
	denomination,
	OnFloor,
	StyleDesc,
	Manufacturer
	 )
SELECT	@Casino,
	a.slotnumber,
	a.slotmast_id,
	a.par,
	a.locationstring,
	a.description,
	a.denomination,
	a.OnFloor,
	IsNull(B.StyleDesc, '##Not Set##'),
	IsNull(C.Manufacturer, '##Not Set##')
FROM	CDS_SLOTMAST A (nolock)
LEFT JOIN BB_STYLE b (nolock) 	ON a.style_id = b.style_id
LEFT JOIN BB_mfr c (nolock) 	ON a.mfr_id = c.mfr_id
WHERE	a.active = 'Y' and a.Currentrevision = 'Y'
	and a.slotnumber not between 99000 and 99999
 
SET	@loopVar 	= 0
WHILE 	@loopVar < @monthCount
BEGIN
SET	@BEGINDATE	= (SELECT DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))-1),DATEADD(mm,-@loopvar-1,GETDATE())))
SET	@ENDDATE	= (SELECT DATEADD(dd, -DAY(DATEADD(m,1,GETDATE())), DATEADD(m,-@loopvar,GETDATE())))
SET 	@DateDays 	= (SELECT DATEDIFF(DAY, @BEGINDATE, @ENDDATE)+ 1)
SET 	@VarMonth	= (SELECT RTRIM(left(DATEADD(dd, DATEDIFF(dd,0,@BEGINDATE), 0),3)))
SET 	@sqla 	= 'ALTER TABLE #slotreport ADD '+@VarMonth+'TTLCI money NOT NULL DEFAULT 0.00; '
		+ 'ALTER TABLE #slotreport ADD '+@VarMonth+'TWPuPD money NOT NULL DEFAULT 0.00; '
		+ 'ALTER TABLE #slotreport ADD '+@VarMonth+'CIPuPD money NOT NULL DEFAULT 0.00; '
		+ 'ALTER TABLE #slotreport ADD M'+CAST(@loopVar AS VARCHAR)+'Name VARCHAR(3) NOT NULL DEFAULT ('''+@VarMonth+''');'
		+ 'ALTER TABLE #slotreport ADD M'+CAST(@loopVar AS VARCHAR)+'Days VARCHAR(2) NOT NULL DEFAULT ('+CAST (@DateDays as VARCHAR)+'); '
 
-- CI Pu PD 
SET @sqlb	= 'update #slotreport '
		+ 'SET '+CAST (@VarMonth as VARCHAR)+'TTLCI = amount.total '
		+ 'FROM (SELECT slotmast_id, isNull(SUM(ElecCoinIn), ''0'') as total '
		+ 'FROM BB_REVENUE '
		+ 'WHERE period_id = ''4'' and auditdate between '''+convert(varchar(11),@BeginDate,121)+''' and '''+convert(varchar(11),@EndDate,121)+''' ' 
		+ 'GROUP BY slotmast_id) as Amount '
		+ 'WHERE #slotreport.slotmast_id = amount.slotmast_id;' 
-- TW Pu PD 
		+ 'update #slotreport '
		+ 'SET '+CAST (@VarMonth as VARCHAR)+'TWPUPD = amount.total '
		+ '* (select Par from cds_slotmast where #slotreport.slotmast_id = cds_slotmast.slotmast_id) / 100  / '+CAST (@DateDays as VARCHAR)+''
		+ 'FROM (SELECT slotmast_id, isNull(SUM(ElecCoinIn), ''0'') as total '
		+ 'FROM BB_REVENUE '
		+ 'WHERE period_id = ''4'' and auditdate between '''+convert(varchar(11),@BeginDate,121)+''' and '''+convert(varchar(11),@EndDate,121)+''' ' 
		+ 'GROUP BY slotmast_id) as Amount '
		+ 'WHERE #slotreport.slotmast_id = amount.slotmast_id;'
-- CI Pu PD
		+ 'update #slotreport '
		+ 'SET '+CAST (@VarMonth as VARCHAR)+'CIPUPD = amount.total '
		+ '/ '+CAST (@DateDays as VARCHAR)+''
		+ 'FROM (SELECT slotmast_id, isNull(SUM(ElecCoinIn), ''0'') as total '
		+ 'FROM BB_REVENUE '
		+ 'WHERE period_id = ''4'' and auditdate between '''+convert(varchar(11),@BeginDate,121)+''' and '''+convert(varchar(11),@EndDate,121)+''' ' 
		+ 'GROUP BY slotmast_id) as Amount '
		+ 'WHERE #slotreport.slotmast_id = amount.slotmast_id;'
SET @loopVar 	= @loopVar + 1
EXEC (@sqla)
--SELECT (@sqlb)
EXEC (@sqlb)
END
 
select * from #slotreport (nolock)
[+][-]01/06/09 11:39 AM, ID: 23308214Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: SQL Query Syntax
Tags: MSSQL 2000 Dynamic Query Syntax
Sign Up Now!
Solution Provided By: BrandonGalderisi
Participating Experts: 1
Solution Grade: A
 
[+][-]01/06/09 11:28 AM, ID: 23308081Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/06/09 11:31 AM, ID: 23308122Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/06/09 11:32 AM, ID: 23308133Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/06/09 11:55 AM, ID: 23308397Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01/06/09 11:57 AM, ID: 23308421Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625