473,788 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.nvchIn centiveId = 'MPW' AND tbl_Data.nvchPe riodId =
'W27P'
AND tbl_Levels.nvch IncentiveId = 'MPW' AND
tbl_LevelDetail .nvchIncentiveI d = 'MPW'
AND tbl_LevelDetail _Report.nvchInc entiveId = 'MPW' AND
tbl_Data.nvchDa taLevelId = tbl_Levels.nvch DataLevelId
AND tbl_Levels.nvch DataLevelId = tbl_LevelDetail .nvchLevelId
AND tbl_Levels.nvch ReportingLevelI d =
tbl_LevelDetail _Report.nvchLev elId
AND tbl_LevelDetail .nvchLevelTypeI d = 2
AND tbl_LevelDetail _Report.nvchLev elTypeId = 1
AND tbl_Levels.nvch ReportingLevelI d IN ('a')
GROUP BY tbl_Levels.nvch ReportingLevelI d, tbl_Levels.nvch DataLevelId,
tbl_LevelDetail .nvchLevelName, tbl_LevelDetail _Report.nvchLev elName
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_incmpwfilte r_rs
(
@strPeriodID varchar ,
@intLevelDetail ID varchar,
@intLevelReport ID 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.nvchIn centiveId = 'MPW' AND tbl_Data.nvchPe riodId =
@strPeriodID
AND tbl_Levels.nvch IncentiveId = 'MPW' AND
tbl_LevelDetail .nvchIncentiveI d = 'MPW'
AND tbl_LevelDetail _Report.nvchInc entiveId = 'MPW' AND
tbl_Data.nvchDa taLevelId = tbl_Levels.nvch DataLevelId
AND tbl_Levels.nvch DataLevelId = tbl_LevelDetail .nvchLevelId
AND tbl_Levels.nvch ReportingLevelI d =
tbl_LevelDetail _Report.nvchLev elId
AND tbl_LevelDetail .nvchLevelTypeI d = @intLevelDetail ID
AND tbl_LevelDetail _Report.nvchLev elTypeId = @intLevelReport ID
AND tbl_Levels.nvch ReportingLevelI d IN (@strFilters )
GROUP BY tbl_Levels.nvch ReportingLevelI d, tbl_Levels.nvch DataLevelId,
tbl_LevelDetail .nvchLevelName, tbl_LevelDetail _Report.nvchLev elName
ORDER BY Pos, DataLevelName
then call it by SQL statement:

EXEC usp_incmpwfilte r_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.nvch ReportingLevelI d 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 6385
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.nvchIn centiveId = 'MPW' AND tbl_Data.nvchPe riodId =
'W27P'
AND tbl_Levels.nvch IncentiveId = 'MPW' AND
tbl_LevelDetail .nvchIncentiveI d = 'MPW'
AND tbl_LevelDetail _Report.nvchInc entiveId = 'MPW' AND
tbl_Data.nvchDa taLevelId = tbl_Levels.nvch DataLevelId
AND tbl_Levels.nvch DataLevelId = tbl_LevelDetail .nvchLevelId
AND tbl_Levels.nvch ReportingLevelI d =
tbl_LevelDetail _Report.nvchLev elId
AND tbl_LevelDetail .nvchLevelTypeI d = 2
AND tbl_LevelDetail _Report.nvchLev elTypeId = 1
AND tbl_Levels.nvch ReportingLevelI d IN ('a')
GROUP BY tbl_Levels.nvch ReportingLevelI d, tbl_Levels.nvch DataLevelId,
tbl_LevelDetail .nvchLevelName, tbl_LevelDetail _Report.nvchLev elName
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_incmpwfilte r_rs
(
@strPeriodID varchar ,
@intLevelDetail ID varchar,
@intLevelReport ID 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.nvchIn centiveId = 'MPW' AND tbl_Data.nvchPe riodId =
@strPeriodID
AND tbl_Levels.nvch IncentiveId = 'MPW' AND
tbl_LevelDetail .nvchIncentiveI d = 'MPW'
AND tbl_LevelDetail _Report.nvchInc entiveId = 'MPW' AND
tbl_Data.nvchDa taLevelId = tbl_Levels.nvch DataLevelId
AND tbl_Levels.nvch DataLevelId = tbl_LevelDetail .nvchLevelId
AND tbl_Levels.nvch ReportingLevelI d =
tbl_LevelDetail _Report.nvchLev elId
AND tbl_LevelDetail .nvchLevelTypeI d = @intLevelDetail ID
AND tbl_LevelDetail _Report.nvchLev elTypeId = @intLevelReport ID
AND tbl_Levels.nvch ReportingLevelI d IN (@strFilters )
GROUP BY tbl_Levels.nvch ReportingLevelI d, tbl_Levels.nvch DataLevelId,
tbl_LevelDetail .nvchLevelName, tbl_LevelDetail _Report.nvchLev elName
ORDER BY Pos, DataLevelName

then call it by SQL statement:

EXEC usp_incmpwfilte r_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.nvch ReportingLevelI d 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_incmpwfilte r_rs
(
@strPeriodID varchar ,
@intLevelDetail ID varchar,
@intLevelReport ID varchar,
@strFilters varchar
)
AS
As Gert-Jan said, you need to specify the length, or else you only
get varchar(1).
AND tbl_Levels.nvch ReportingLevelI d 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.nvch ReportingLevelI d 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
4706
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 all the software installed on each piece of hardware by referencing the primary keys of each table. So now, I have a request that retrieve information from those 3 tables giving a list of all the hardware with their details + the software...
10
2313
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, delete, use, etc.. stored procedures soley from asp (i.e., no GUI or console- like being hosted on Brinkster)? The tutorial on aspfaq.com mentions that stored procedures can be
12
8353
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 will work on preparing SQL statements that will create the tables, insert sample record and run the SP. I would hope people will look at my SP and give me any hints on how I can better write the SP.
1
9910
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 already does not exists. if it exists SP should pass 0 as output else if insert is successfull it should pass 1 to calling sp. But following SP raising an ERRor" Incorrect Syntax near 'GO' help
1
3724
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, sequential date ranges. I.E. from 9/1/2000 - 10/1/2000 then 10/1/2000 - 11/1/2000 etc etc etc. It calls SPs using the 2 dates and an integer used for companyid reference.
7
3220
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, …? Thanks, Jim.
45
3415
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
8139
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 ( ANSI "SERIALIZABLE" ), not the default; default is CS (ANSI "Read Committed")). The procedure language is SQL. According to the documentation, I can adjust procedure *run*-time isolation level by setting *compile*-time dataserver-wide option
14
2375
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 tables, my tables being infected again and again. I already checked all stored procedures but couldn't find anything suspicious. Any help how to get rid of this f* malware is highly appreciated!!!
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.