Solved

Passing a parameter to sp_CrossTab

Posted on 2009-05-18
4
1,264 Views
Last Modified: 2012-05-07
Hello Experts,

We're using the sp_CrossTab proc that is described here
http://searchsqlserver.techtarget.com/tip/0,289483,sid87_gci1131829_mem1,00.html

Here is how we have it set up.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



ALTER PROCEDURE [dbo].[usp_PivotTable_PM_Cal_Detail_Schedule]
AS
SET NOCOUNT ON
Execute sp_crosstab
'SELECT * FROM tblFlexGridColVal',
'FlexGridName = ^ToolDetailActivity^ ',
'Activity|Performed By|Performed On|Results|Cert|Technician',
'FieldName',
'FieldValue',
'Max'


We need to pass a dynamic parmeter (@EventID) from a calling VB 6.0 (using ADO) client and use this @EventID as part of the WHERE clause portion of the main parameter passed into sp_CrossTab. For example, the intent is to acheive this:

ALTER PROCEDURE [dbo].[usp_PivotTable_PM_Cal_Detail_Schedule]

(
@EventID as nvarchar(50)
)

AS
SET NOCOUNT ON
Execute sp_crosstab
'SELECT * FROM tblFlexGridColVal',
'FlexGridName = ^ToolDetailActivity^  AND EventID='+ @EventID +',
'Activity|Performed By|Performed On|Results|Cert|Technician',
'FieldName',
'FieldValue',
'Max'


My question is, how do we embed this @EventID into the main parameter string and accomplish our objective?
0
Comment
Question by:Bleary-Eyed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:bljak
ID: 24413526
maybe you can make whole param to sp_crosstab as variable having something like

declare @sp_param nvarchar(2000)
set @sp_param = 'FlexGridName = ^ToolDetailActivity^  AND EventID='+ @EventID
 
Execute sp_crosstab
'SELECT * FROM tblFlexGridColVal',
@sp_param,
'Activity|Performed By|Performed On|Results|Cert|Technician',
'FieldName',
'FieldValue',
'Max'

Open in new window

0
 

Author Comment

by:Bleary-Eyed
ID: 24413579
Well, the way sp_CrossTab works is it takes one string paramter only, which is subsequently parsed (in sp_CrossTab) into several paramters. The paramter passed into sp_CrossTab is simply a string, delimited appropriatly to facilitate the internal parsing. So I don't think your suggestion would work.
0
 
LVL 4

Accepted Solution

by:
bljak earned 500 total points
ID: 24414349
How about putting everything inside as parameter?

declare @sp_param nvarchar(2000)
set @sp_param = 'SELECT * FROM tblFlexGridColVal'',
''FlexGridName = ^ToolDetailActivity^  AND EventID='+ @EventID =',
''Activity|Performed By|Performed On|Results|Cert|Technician'',
''FieldName'',
''FieldValue'',
''Max'''
 
 
Execute sp_crosstab @sp_param

Open in new window

0
 

Author Closing Comment

by:Bleary-Eyed
ID: 31582646
That did it.

Thanks
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question