Solved

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

Posted on 2011-03-06
6
225 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 500 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

690 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