472,119 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 8525
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...then 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!frmCriteria_rpt!BeginDate, @dteEnd DateTime
= Forms!frmCriteria_rpt!EndDate

HTH,

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

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

iQA/AwUBQDkkToechKqOuFEgEQKJ9QCfcFf4APZGcowAxg6Tapc/qyL3CyUAoNy0
o5f4jGtB7L7VPPuj1aMluICK
=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**********@SBCGlobal.Net> wrote in message
news:8c**************************@posting.google.c om...
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ecohouse | last post: by
2 posts views Thread by Rhino | last post: by
reply views Thread by leo001 | last post: by

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.