473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

help with stored procedures using IN / GROUP BY statements

trying to get to the bottom of this for some time...... eventually to
be used with asp.

heres the problem

the following rather complex SQL statement works fine via query
analyser:

SELECT TOP 100 tbl_LevelDetail.nvchLevelName AS DataLevelName,
MAX(CASE tintDataFieldId WHEN '1' THEN CAST(nvchData AS int) ELSE 0
END) AS 'Pos',
MAX(CASE tintDataFieldId WHEN '2' THEN CAST(nvchData AS char) ELSE '
' END) AS 'AreaName',
MAX(CASE tintDataFieldId WHEN '3' THEN CAST(nvchData AS char) ELSE '
' END) AS 'BDGName',
MAX(CASE tintDataFieldId WHEN '4' THEN CAST(nvchData AS char) else '
' END) AS 'Performance',
MAX(CASE tintDataFieldId WHEN '5' THEN CAST(nvchData AS int) ELSE 0
END) AS 'Qualifier'
FROM tbl_Data, tbl_Levels, tbl_LevelDetail, tbl_LevelDetail AS
tbl_LevelDetail_Report
WHERE tbl_Data.nvchIncentiveId = 'MPW' AND tbl_Data.nvchPeriodId =
'W27P'
AND tbl_Levels.nvchIncentiveId = 'MPW' AND
tbl_LevelDetail.nvchIncentiveId = 'MPW'
AND tbl_LevelDetail_Report.nvchIncentiveId = 'MPW' AND
tbl_Data.nvchDataLevelId = tbl_Levels.nvchDataLevelId
AND tbl_Levels.nvchDataLevelId = tbl_LevelDetail.nvchLevelId
AND tbl_Levels.nvchReportingLevelId =
tbl_LevelDetail_Report.nvchLevelId
AND tbl_LevelDetail.nvchLevelTypeId = 2
AND tbl_LevelDetail_Report.nvchLevelTypeId = 1
AND tbl_Levels.nvchReportingLevelId IN ('a')
GROUP BY tbl_Levels.nvchReportingLevelId, tbl_Levels.nvchDataLevelId,
tbl_LevelDetail.nvchLevelName, tbl_LevelDetail_Report.nvchLevelName
ORDER BY Pos, DataLevelName

returns rows ok no problem
but when trying to convert to a stored procedure i dont get any
results:

CREATE PROCEDURE usp_incmpwfilter_rs
(
@strPeriodID varchar ,
@intLevelDetailID varchar,
@intLevelReportID varchar,
@strFilters varchar
)
AS

set nocount on

SELECT TOP 100 tbl_LevelDetail.nvchLevelName AS DataLevelName,
MAX(CASE tintDataFieldId WHEN '1' THEN CAST(nvchData AS int) ELSE 0
END) AS 'Pos',
MAX(CASE tintDataFieldId WHEN '2' THEN CAST(nvchData AS char) ELSE '
' END) AS 'AreaName',
MAX(CASE tintDataFieldId WHEN '3' THEN CAST(nvchData AS char) ELSE '
' END) AS 'BDGName',
MAX(CASE tintDataFieldId WHEN '4' THEN CAST(nvchData AS char) else '
' END) AS 'Performance',
MAX(CASE tintDataFieldId WHEN '5' THEN CAST(nvchData AS int) ELSE 0
END) AS 'Qualifier'
FROM tbl_Data, tbl_Levels, tbl_LevelDetail, tbl_LevelDetail AS
tbl_LevelDetail_Report
WHERE tbl_Data.nvchIncentiveId = 'MPW' AND tbl_Data.nvchPeriodId =
@strPeriodID
AND tbl_Levels.nvchIncentiveId = 'MPW' AND
tbl_LevelDetail.nvchIncentiveId = 'MPW'
AND tbl_LevelDetail_Report.nvchIncentiveId = 'MPW' AND
tbl_Data.nvchDataLevelId = tbl_Levels.nvchDataLevelId
AND tbl_Levels.nvchDataLevelId = tbl_LevelDetail.nvchLevelId
AND tbl_Levels.nvchReportingLevelId =
tbl_LevelDetail_Report.nvchLevelId
AND tbl_LevelDetail.nvchLevelTypeId = @intLevelDetailID
AND tbl_LevelDetail_Report.nvchLevelTypeId = @intLevelReportID
AND tbl_Levels.nvchReportingLevelId IN (@strFilters )
GROUP BY tbl_Levels.nvchReportingLevelId, tbl_Levels.nvchDataLevelId,
tbl_LevelDetail.nvchLevelName, tbl_LevelDetail_Report.nvchLevelName
ORDER BY Pos, DataLevelName
then call it by SQL statement:

EXEC usp_incmpwfilter_rs 'W27P',2,1,'a'

Returns no rows. This is the initial problem. Also there will be
another issue if i can get the above to work: the @strFilters can
contain multiple data, ie 'a','k'
this works fine in the 1st sql statement ie: AND
tbl_Levels.nvchReportingLevelId IN ('a','k') but I dont know how to
pass as a parameter to the stored procedure. I cannot create temporary
tables.

i had not created the intial SQL statement, i am just trying to
convert it to a stored procedure which accepts thos parameters. this
has been a real headache for me, any help as always appreciated
greatly.
Jul 20 '05 #1
3 6355
In all your char and varchar definition, you need to specify the size.

So in your CASE expression you could cast to char(3) instead of char.
The immediate problem is probably that currently your parameters are
also non-sized, which (I believe) defaults to varchar(1).

Hope this helps,
Gert-Jan
chowda wrote:

trying to get to the bottom of this for some time...... eventually to
be used with asp.

heres the problem

the following rather complex SQL statement works fine via query
analyser:

SELECT TOP 100 tbl_LevelDetail.nvchLevelName AS DataLevelName,
MAX(CASE tintDataFieldId WHEN '1' THEN CAST(nvchData AS int) ELSE 0
END) AS 'Pos',
MAX(CASE tintDataFieldId WHEN '2' THEN CAST(nvchData AS char) ELSE '
' END) AS 'AreaName',
MAX(CASE tintDataFieldId WHEN '3' THEN CAST(nvchData AS char) ELSE '
' END) AS 'BDGName',
MAX(CASE tintDataFieldId WHEN '4' THEN CAST(nvchData AS char) else '
' END) AS 'Performance',
MAX(CASE tintDataFieldId WHEN '5' THEN CAST(nvchData AS int) ELSE 0
END) AS 'Qualifier'
FROM tbl_Data, tbl_Levels, tbl_LevelDetail, tbl_LevelDetail AS
tbl_LevelDetail_Report
WHERE tbl_Data.nvchIncentiveId = 'MPW' AND tbl_Data.nvchPeriodId =
'W27P'
AND tbl_Levels.nvchIncentiveId = 'MPW' AND
tbl_LevelDetail.nvchIncentiveId = 'MPW'
AND tbl_LevelDetail_Report.nvchIncentiveId = 'MPW' AND
tbl_Data.nvchDataLevelId = tbl_Levels.nvchDataLevelId
AND tbl_Levels.nvchDataLevelId = tbl_LevelDetail.nvchLevelId
AND tbl_Levels.nvchReportingLevelId =
tbl_LevelDetail_Report.nvchLevelId
AND tbl_LevelDetail.nvchLevelTypeId = 2
AND tbl_LevelDetail_Report.nvchLevelTypeId = 1
AND tbl_Levels.nvchReportingLevelId IN ('a')
GROUP BY tbl_Levels.nvchReportingLevelId, tbl_Levels.nvchDataLevelId,
tbl_LevelDetail.nvchLevelName, tbl_LevelDetail_Report.nvchLevelName
ORDER BY Pos, DataLevelName

returns rows ok no problem
but when trying to convert to a stored procedure i dont get any
results:

CREATE PROCEDURE usp_incmpwfilter_rs
(
@strPeriodID varchar ,
@intLevelDetailID varchar,
@intLevelReportID varchar,
@strFilters varchar
)
AS

set nocount on

SELECT TOP 100 tbl_LevelDetail.nvchLevelName AS DataLevelName,
MAX(CASE tintDataFieldId WHEN '1' THEN CAST(nvchData AS int) ELSE 0
END) AS 'Pos',
MAX(CASE tintDataFieldId WHEN '2' THEN CAST(nvchData AS char) ELSE '
' END) AS 'AreaName',
MAX(CASE tintDataFieldId WHEN '3' THEN CAST(nvchData AS char) ELSE '
' END) AS 'BDGName',
MAX(CASE tintDataFieldId WHEN '4' THEN CAST(nvchData AS char) else '
' END) AS 'Performance',
MAX(CASE tintDataFieldId WHEN '5' THEN CAST(nvchData AS int) ELSE 0
END) AS 'Qualifier'
FROM tbl_Data, tbl_Levels, tbl_LevelDetail, tbl_LevelDetail AS
tbl_LevelDetail_Report
WHERE tbl_Data.nvchIncentiveId = 'MPW' AND tbl_Data.nvchPeriodId =
@strPeriodID
AND tbl_Levels.nvchIncentiveId = 'MPW' AND
tbl_LevelDetail.nvchIncentiveId = 'MPW'
AND tbl_LevelDetail_Report.nvchIncentiveId = 'MPW' AND
tbl_Data.nvchDataLevelId = tbl_Levels.nvchDataLevelId
AND tbl_Levels.nvchDataLevelId = tbl_LevelDetail.nvchLevelId
AND tbl_Levels.nvchReportingLevelId =
tbl_LevelDetail_Report.nvchLevelId
AND tbl_LevelDetail.nvchLevelTypeId = @intLevelDetailID
AND tbl_LevelDetail_Report.nvchLevelTypeId = @intLevelReportID
AND tbl_Levels.nvchReportingLevelId IN (@strFilters )
GROUP BY tbl_Levels.nvchReportingLevelId, tbl_Levels.nvchDataLevelId,
tbl_LevelDetail.nvchLevelName, tbl_LevelDetail_Report.nvchLevelName
ORDER BY Pos, DataLevelName

then call it by SQL statement:

EXEC usp_incmpwfilter_rs 'W27P',2,1,'a'

Returns no rows. This is the initial problem. Also there will be
another issue if i can get the above to work: the @strFilters can
contain multiple data, ie 'a','k'
this works fine in the 1st sql statement ie: AND
tbl_Levels.nvchReportingLevelId IN ('a','k') but I dont know how to
pass as a parameter to the stored procedure. I cannot create temporary
tables.

i had not created the intial SQL statement, i am just trying to
convert it to a stored procedure which accepts thos parameters. this
has been a real headache for me, any help as always appreciated
greatly.

Jul 20 '05 #2
chowda (pa*****************@yahoo.co.uk) writes:
CREATE PROCEDURE usp_incmpwfilter_rs
(
@strPeriodID varchar ,
@intLevelDetailID varchar,
@intLevelReportID varchar,
@strFilters varchar
)
AS
As Gert-Jan said, you need to specify the length, or else you only
get varchar(1).
AND tbl_Levels.nvchReportingLevelId IN (@strFilters )

Returns no rows. This is the initial problem. Also there will be
another issue if i can get the above to work: the @strFilters can
contain multiple data, ie 'a','k'
this works fine in the 1st sql statement ie: AND
tbl_Levels.nvchReportingLevelId IN ('a','k') but I dont know how to
pass as a parameter to the stored procedure. I cannot create temporary
tables.


Look at
http://www.algonet.se/~sommar/arrays...ist-of-strings.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
thats great Gert-Jan, i should have picked up the missing size params
as I have placed them in my other stored procedures!, and many thanks
for your article Erland - i have the comma delimited string working
now! Now i've got to build my asp and i'm flying!
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: berthelot samuel | last post by:
Hi everyone, I am currently trying to write a report based on a View of SQL Server. Basically, I have 3 tables : Hardware, SoftwareInstalled and Software with SoftwareInstalled that keeps track of...
10
by: Dragonhunter | last post by:
Hello, The aspfaq.com seems to really push stored procedures, and I hear the same advice here all the time. So I want to take the advice. Is it possible to create and practically maintain,...
12
by: serge | last post by:
I have an SP that is big, huge, 700-800 lines. I am not an expert but I need to figure out every possible way that I can improve the performance speed of this SP. In the next couple of weeks I...
1
by: Amit D.Shinde | last post by:
Hi Experts, i am writting a stored procedure in sql server 7. Its a simple stored procedure It is my first stored procedure. I want insert a record in table if the primary key field user id...
1
by: Beau | last post by:
Hi all, thanks in advance. Ok, heres the story. What is happening...... -------------------------------- I've got an ASP page that loops. It loops in order to get data in different,...
7
by: JIM.H. | last post by:
Hello, Is there any difference to between SLQ string in the code and call execute query and call a stored procedure and execute the query that way concerning speed, effectiveness, reliability,...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
2
by: kanda | last post by:
Hello. I am developing the application (VBA&ODBC, to be exact) which periodically calls the stored procedures in the IBM DB2. A few of the procedures require executing with isolation level RR (...
14
by: morebeer | last post by:
I got the same problem, hundreds of SQL tables been infected with this malicious javascript code. But although closing the original injection leak and also having replaced all strings in all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.