Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-06
6
Medium Priority
?
229 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 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