473,696 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return Values from a Crosstab Stored Procedure

I came across an article in SQL Mag about Crosstab Queries. It works
great in Query Analyzer, but I'm stuck on how to use it in an Access
ADP. I need to use it as a Recordsource in a form and report. Can
someone tell me how to use it, and please try to be as descriptive as
possible. I'm new to Stored Procedures.

Thanks

*************** *************** ***********

CREATE PROC sp_CrossTab
@table AS sysname, -- Table to crosstab
@onrows AS nvarchar(128), -- Grouping key values (on rows)
@onrowsalias AS sysname = NULL, -- Alias for grouping column
@oncols AS nvarchar(128), -- Destination columns (on columns)
@sumcol AS sysname = NULL -- Data cells
AS
DECLARE
@sql AS varchar(8000),
@NEWLINE AS char(1)

SET @NEWLINE = CHAR(10)

-- step 1: beginning of SQL string
SET @sql =
'SELECT' + @NEWLINE +
' ' + @onrows +
CASE
WHEN @onrowsalias IS NOT NULL THEN ' AS ' + @onrowsalias
ELSE ''
END
CREATE TABLE #keys(keyvalue nvarchar(100) NOT NULL PRIMARY KEY)

DECLARE @keyssql AS varchar(1000)
SET @keyssql =
'INSERT INTO #keys ' +
'SELECT DISTINCT CAST(' + @oncols + ' AS nvarchar(100)) ' +
'FROM ' + @table

EXEC (@keyssql)
DECLARE @key AS nvarchar(100)
SELECT @key = MIN(keyvalue) FROM #keys

WHILE @key IS NOT NULL
BEGIN
SET @sql = @sql + ',' + @NEWLINE +
' SUM(CASE CAST(' + @oncols +
' AS nvarchar(100))' + @NEWLINE +
' WHEN N''' + @key +
''' THEN ' + CASE
WHEN @sumcol IS NULL THEN '1'
ELSE @sumcol
END + @NEWLINE +
' ELSE 0' + @NEWLINE +
' END) AS c' + @key

SELECT @key = MIN(keyvalue) FROM #keys
WHERE keyvalue > @key
END
SET @sql = @sql + @NEWLINE +
'FROM ' + @table + @NEWLINE +
'GROUP BY ' + @onrows + @NEWLINE +
'ORDER BY ' + @onrows

-- PRINT @sql + @NEWLINE -- For debug
EXEC (@sql)
GO
Jul 20 '05 #1
6 8681
First of all, I hope that you know exactly what column headers you're going to
be using, or Access reports won't do you any good. That is, you must have a
couple "canned" crosstab reports ready to go, or you're headed down a rough
road.

For example, if you KNOW that your crosstab reports are always going to have
the MONTHS as their column grouping, and a fixed set of columns for row
grouping, then you can call that SQLMAG sp the same way every time...maybe
expand its funcationlity to include some criteria (probably YEAR).

If that's the case, then you may as well tell the SP to create a real table
every time, and bind your report to that table...this will speed up development
of the report, too, since the table will be there any time you need to change
the report layout.

However, if you're pipe-dreaming about giving user the ability to create their
own row/column crosstab parameters, and having that come out i na neat Access
report, fa-get-abowd-it! I recommend MS Excel Automation for that, using Pivot
Tables. (Maybe SQL Server's Pivot Table or Reporting Services would do well
here...I've never used them because I'm at an expert level with MS Excel.)

Let us know mor eabout your real intentions...th en we can help you further.
Jul 20 '05 #2
The Crosstab query will be used for Charts only.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
Unfortunately, that doesn't tell us anything about the scope of the possible
input parameters.
Jul 20 '05 #4
Many thanks to Itzik Ben-Gan, Author of the article I mentioned in my
first post. He suggested that I add SET NOCOUNT ON to the Stored
Procedure. It works great.

Thanks Itzik!

dc****@aol.comSPNOAM (DCM Fan) wrote in message news:<20******* *************** *****@mb-m07.aol.com>...
Unfortunately, that doesn't tell us anything about the scope of the possible
input parameters.

Jul 20 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Critique & suggestion:

Since the SP returns variable number of columns in its resultset you
can't design forms/reports on it unless you KNOW that you will always
be returning the same number of columns each time you run the SP. If
you know this you can create a report (without the report wizard) and
in design view provide the parameters in the report's Input Parameters
property that return that resultset. Then click the "Field List" item
in the View menu. This will run the SP, using the input parameters,
and give you a list of the report's fields. Drag & Drop the fields
from the Field List onto the report. Before entering the report into
production change the Input Parameters property so it gets the SP
parameters from a form. E.g.:

@dteBegin DateTime = Forms!frmCriter ia_rpt!BeginDat e, @dteEnd DateTime
= Forms!frmCriter ia_rpt!EndDate

HTH,

MGFoster:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQDkkToechKq OuFEgEQKJ9QCfcF f4APZGcowAxg6Ta pc/qyL3CyUAoNy0
o5f4jGtB7L7VPPu j1aMluICK
=y7bk
-----END PGP SIGNATURE-----
Dave wrote:
I came across an article in SQL Mag about Crosstab Queries. It works
great in Query Analyzer, but I'm stuck on how to use it in an Access
ADP. I need to use it as a Recordsource in a form and report. Can
someone tell me how to use it, and please try to be as descriptive as
possible. I'm new to Stored Procedures.

Thanks

*************** *************** ***********

CREATE PROC sp_CrossTab
@table AS sysname, -- Table to crosstab
@onrows AS nvarchar(128), -- Grouping key values (on rows)
@onrowsalias AS sysname = NULL, -- Alias for grouping column
@oncols AS nvarchar(128), -- Destination columns (on columns)
@sumcol AS sysname = NULL -- Data cells


< SNIP >

Jul 20 '05 #6
Dave,
It's maybe not as elegent, but CASE can be used to create cross-tab views.
I do something like this to create a cross-tab of a student's attendence for
each weekday. The resulting view can be bound to forms & reports in an
Access .adp.
"MAX(CASE WEEKDAY_NUM WHEN 2 THEN dbo.SESSION_TBL .ATTEND_STATUS ELSE NULL
END) AS MON" repeated seven times gets me my days of the week in columns.
Something similar would work for you.

"Dave" <Da**********@S BCGlobal.Net> wrote in message
news:8c******** *************** ***@posting.goo gle.com...
I came across an article in SQL Mag about Crosstab Queries. It works
great in Query Analyzer, but I'm stuck on how to use it in an Access
ADP. I need to use it as a Recordsource in a form and report. Can
someone tell me how to use it, and please try to be as descriptive as
possible. I'm new to Stored Procedures.

Thanks

*************** *************** ***********

CREATE PROC sp_CrossTab
@table AS sysname, -- Table to crosstab
@onrows AS nvarchar(128), -- Grouping key values (on rows)
@onrowsalias AS sysname = NULL, -- Alias for grouping column
@oncols AS nvarchar(128), -- Destination columns (on columns)
@sumcol AS sysname = NULL -- Data cells
AS
DECLARE
@sql AS varchar(8000),
@NEWLINE AS char(1)

SET @NEWLINE = CHAR(10)

-- step 1: beginning of SQL string
SET @sql =
'SELECT' + @NEWLINE +
' ' + @onrows +
CASE
WHEN @onrowsalias IS NOT NULL THEN ' AS ' + @onrowsalias
ELSE ''
END
CREATE TABLE #keys(keyvalue nvarchar(100) NOT NULL PRIMARY KEY)

DECLARE @keyssql AS varchar(1000)
SET @keyssql =
'INSERT INTO #keys ' +
'SELECT DISTINCT CAST(' + @oncols + ' AS nvarchar(100)) ' +
'FROM ' + @table

EXEC (@keyssql)
DECLARE @key AS nvarchar(100)
SELECT @key = MIN(keyvalue) FROM #keys

WHILE @key IS NOT NULL
BEGIN
SET @sql = @sql + ',' + @NEWLINE +
' SUM(CASE CAST(' + @oncols +
' AS nvarchar(100))' + @NEWLINE +
' WHEN N''' + @key +
''' THEN ' + CASE
WHEN @sumcol IS NULL THEN '1'
ELSE @sumcol
END + @NEWLINE +
' ELSE 0' + @NEWLINE +
' END) AS c' + @key

SELECT @key = MIN(keyvalue) FROM #keys
WHERE keyvalue > @key
END
SET @sql = @sql + @NEWLINE +
'FROM ' + @table + @NEWLINE +
'GROUP BY ' + @onrows + @NEWLINE +
'ORDER BY ' + @onrows

-- PRINT @sql + @NEWLINE -- For debug
EXEC (@sql)
GO

Jul 20 '05 #7

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

Similar topics

3
22925
by: Vipul Pathak | last post by:
Hello Friends ! I have the Following Code, that Executes a Stored Procedure and Attempt to read a Returned Integer Value from the StoredProc. But It gives Error ... ADODB.Command (0x800A0BB9) Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. /C4U/DBOutputParameterTest.asp, line 25
4
2068
by: Ecohouse | last post by:
I have a quick question for you about SQL stored procedures. If I'm in a stored procedure and want to call another stored procedure and return values from the second stored procedure what is the procedure? I know you do the following to run the second stored procedure and pass in any parameters: EXEC GetAuthorBooks @AuthorID So if I wanted the GetAuthorBooks to return all the books for an author and then populate a temp table in the...
2
4664
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info Center: To return a result set from a procedure to the originating application, use the WITH RETURN TO CLIENT clause. When WITH RETURN TO CLIENT is specified on a result set, no nested procedures can access the result set.
2
1775
by: Dave | last post by:
I came across an article in SQL Mag about Crosstab Queries. It works great in Query Analyzer, but I'm stuck on how to use it in an Access ADP. I need to use it as a Recordsource in a form and report. Can someone tell me how to use it, and please try to be as descriptive as possible. I'm new to Stored Procedures. Thanks *****************************************
4
2062
by: raghav | last post by:
Hi all I am having a SP which is returning a value......Now I have to store that value in session...I saw some examples in msdn lib ----> string Name=string.Empty; Session=Name.ToString(); But in my case return value is ID ...and it has not been declared in C#
5
1738
by: Fir5tSight | last post by:
Hi All, I have a C#.NET code as follows: private void ScanInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { try { Row lRow = this.Connection.InsertScannedFile(ID);
3
4143
by: gopi2ks | last post by:
Dear All, I have one stored procedure like sp_insertEmployee Employee Table Fileds Eno int pk, ename varchar(100), designation varchar
12
5134
by: Dooza | last post by:
I have a stored procedure that takes a number of inputs, does a bulk insert, and then outputs a recordset. When I run the stored procedure in Server Management Studio I also get a return value from the stored procedure which is an INT. I want to access this return value on my ASP/VBScript page, but do not know how to access it. Here is my code so far:
1
5657
by: psycho | last post by:
How do we return a single value from a stored procedure. Suppose I have a stored procedure like this: create proc dbo.spInsertGroup @ID uniqueidentifier @GroupName varchar(100), @IsActive bit AS BEGIN
0
8656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8590
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
9140
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
8845
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7693
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1988
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.