473,405 Members | 2,261 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,405 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 8645
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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)...
4
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...
2
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...
2
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...
4
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(); ...
5
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
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
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...
1
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...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...
0
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,...
0
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...

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.