SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Arrays in Stroed Prcoedure


Arrays in Stroed Prcoedure

Author
Message
doss.tychicus
doss.tychicus
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 18
I'm trying to get the data from the table
between two dates and for the List of Doctors

select * from tbl_test
where convert(char(10),createdate,121) between '2008-01-01'
and '2008-12-31' and id_doctor in (1,2,3,4,5)

I want to use two variables for the dates and one array for id_doctors
Pl Help w00t
krayknot
krayknot
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3128 Visits: 534
doss.tychicus (11/10/2008)
I'm trying to get the data from the table
between two dates and for the List of Doctors

select * from tbl_test
where convert(char(10),createdate,121) between '2008-01-01'
and '2008-12-31' and id_doctor in (1,2,3,4,5)

I want to use two variables for the dates and one array for id_doctors
Pl Help w00t


use temporary tables

kshitij kumar
kshitij@krayknot.com
www.krayknot.com
Kishore.P
Kishore.P
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3705 Visits: 619
If you are using SQL 2008, you can pass table to stored procedure as a variable.
For more info see SQL 2008 BOL.



winash
winash
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4558 Visits: 1883
Check this article for lots of information on how to use arrays in SQL Server: http://www.sommarskog.se/arrays-in-sql-2005.html



Goldie Lesser
Goldie Lesser
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2574 Visits: 1501
You have two options.
The first is to construct your query using dynamic SQL.
The second option is to convert the array to a table and then JOIN to that table.
The following article explains how to split an array into a table:
http://www.sqlservercentral.com/articles/TSQL/62867/
steve dassin
steve dassin
SSC Eights!
SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)

Group: General Forum Members
Points: 940 Visits: 2077
An array is a 'type', something the archaic computer science of sql knows nothing about. You have to move to a 'real' relational system to find a list/array type. You'll find such adult computer science in Dataphor.

www.dataphor.org
www.beyondsql.blogspot.com
Steve Jones
Steve Jones
SSC Guru
SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)SSC Guru (225K reputation)

Group: Administrators
Points: 225202 Visits: 19641
How are you getting the list of doctors? Is it passed in or is it in a table.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
MikeM7
MikeM7
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 93
If your list of doctors is being passed into the stored procedure as a delimited string then you can write a table-valued function to parse the string and return it as a table and use those results as sub-select for the 'IN' clause.

Here's an example:
This is the code for the split function:
CREATE FUNCTION [dbo].[tvf_Split]
(
@String VARCHAR(4000),
@Delimiter VARCHAR(5)
)
RETURNS @SplittedValues TABLE
(
OccurenceId SMALLINT IDENTITY(1,1),
SplitValue VARCHAR(200)
)
AS
BEGIN
DECLARE @SplitLength INT

WHILE LEN(@String) > 0
BEGIN
SELECT @SplitLength = (CASE CHARINDEX(@Delimiter,@String)
WHEN 0 THEN LEN(@String)
ELSE CHARINDEX(@Delimiter,@String) - 1
END)

INSERT INTO @SplittedValues
SELECT SUBSTRING(@String,1,@SplitLength)

SELECT @String = (CASE (LEN(@String) - @SplitLength)
WHEN 0 THEN ''
ELSE RIGHT(@String, LEN(@String) - @SplitLength - 1)
END)
END
RETURN

Here's how to use it:
declare @inValues varchar(255)
SET @inValues = '(1,2,3,4,5)'

select * from tbl_test
where convert(char(10),createdate,121) between '2008-01-01' and '2008-12-31'
and id_doctor in (select SplitValue FROM tvf_Split(@inValues, ','))

good luck!

-Mike
Goldie Lesser
Goldie Lesser
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2574 Visits: 1501
Mike, a WHILE loop is not the best way to split a CSV.
Take a look at this article. It explains how a table of numbers replaces a loop.
http://www.sqlservercentral.com/articles/TSQL/62867/
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)

Group: General Forum Members
Points: 340864 Visits: 42650
krayknot (11/10/2008)
doss.tychicus (11/10/2008)
I'm trying to get the data from the table
between two dates and for the List of Doctors

select * from tbl_test
where convert(char(10),createdate,121) between '2008-01-01'
and '2008-12-31' and id_doctor in (1,2,3,4,5)

I want to use two variables for the dates and one array for id_doctors
Pl Help w00t


use temporary tables


Sure! How? Got code? Wink

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search