Solved

Passing a parameter to sp_CrossTab

Posted on 2009-05-18
4
1,209 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now