?
Solved

SQL Stored Procedure - have the option to enter 1 parameter or many

Posted on 2011-03-06
6
Medium Priority
?
226 Views
Last Modified: 2012-08-13
using SQL server 2000

stored procedure name is dbo.spCRAIA
this sp has 3 parameters
i) dateStart - 1 value
ii) dateEnd - 1 value
iii) regionCode - 1 or many value

there are more than 20 regionCodes sometimes will need to input 1 sometimes might need to input 2 or more how to achieve this?

there are 2 ways I can execute this SP

first way to execute sp
CREATE Procedure dbo.spCaptureRateARIA(@DateStart Datetime, @DateEnd Datetime, @RegionCode VarChar(50))

exec dbo.spCRAIA '01 july 2010', '31 january 2011', 'OfficeName';


second way to execute sp
declare @DateStart Datetime
declare @DateEnd Datetime
declare @RegionCode VarChar(50)

set @DateStart = '01 july 2010'
set @DateEnd = '31 january 2011'
set @RegionCode = 'OfficeName1'

drop table #a

select sum(chequeamount) as ROInc_amt, 0 as SSFSInc_amt
into #a
from payregion_vw
where paymentdate >= @DateStart and paymentdate <= @DateEnd
and regioncode in (@RegionCode)
union
select 0 as ROInc_amt, sum(chequeamount) as SSFSInc_Amt
from payregion_vw
where paymentdate >= @DateStart and paymentdate <= @DateEnd
and regioncode in (@RegionCode)

-- get rate

select @RegionCode as 'Rate',
sum(Inc_amt)/ sum(ROInc_amt)*100 as 'Inc%',
sum(ROInc_amt)/1000000 as 'AmtROInc'
from #a
0
Comment
Question by:JCTDD
[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
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:lalitgada
ID: 35053430
Default set to null is that is optional
e.g
declare @RegionCode VarChar(50) =null;
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35053434
Create this table-valued function that can split the comma-separated values passed to the stored procedure parameter
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
go


CREATE FUNCTION [dbo].[TsqlSplit] ( @List AS VARCHAR(8000) )
RETURNS @Items TABLE
    (
      Item VARCHAR(8000) NOT NULL
    )
AS 
    BEGIN
        DECLARE @Item AS VARCHAR(8000) ,
            @Pos AS INT
        WHILE DATALENGTH(@List) > 0 
            BEGIN
                SET @Pos = CHARINDEX(',', @List)
                IF @Pos = 0 
                    SET @Pos = DATALENGTH(@List) + 1
                SET @Item = LTRIM(RTRIM(LEFT(@List, @Pos - 1)))
                IF @Item <> '' 
                    INSERT  INTO @Items
                            SELECT  @Item
                SET @List = SUBSTRING(@List, @Pos + DATALENGTH(','), 8000)
            END
        RETURN
    END

Open in new window


Now you can pass the values for @RegionCode  as 'OfficeName1,OfficeName2'
comma-separated and wrapped by single-quotes as an entire parameter

Inside your stored procedure, you can split it like
SELECT * from [dbo].[TsqlSplit]('OfficeName1,OfficeName2')

Raj


0
 

Author Comment

by:JCTDD
ID: 35062018
thanks Raj! I will have to test this and let you know the outcome
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:JCTDD
ID: 35062725
Hi Raj I have created the function but not sure where to put the split below?
SELECT * from [dbo].[TsqlSplit]('OfficeName1,OfficeName2')

Open in new window


running SELECT * from [dbo].[TsqlSplit]('OfficeName1,OfficeName2') will produce:
ITEM
OfficeName1
OfficeName2

but how to use these in the parameter @RegionCode?

this works: exec dbo.spCRAIA '01 july 2010', '31 january 2011', 'OfficeName';

what is the syntax or steps to execute the SP with more than one office e.g.
exec dbo.spCRAIA '01 july 2010', '31 january 2011', 'OfficeName1, OfficeName2';


0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 2000 total points
ID: 35067913
Just modify the WHERE condition from
...and regioncode in (@RegionCode)

Open in new window

to
...and regioncode in (SELECT Item FROM [dbo].[TsqlSplit](@RegionCode))

Open in new window


Raj
0
 

Author Comment

by:JCTDD
ID: 35077402
thanks Raj I get this error now:

"Cannot resolve collation conflict for equal to operation."

this error happens after I modified the where condition to:
and regioncode in (SELECT Item FROM [dbo].[TsqlSplit](@RegionCode))
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

801 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