all groups > sql server programming > august 2007 >
You're in the

sql server programming

group:

Passing array of int into SQL Server stored procedure


Re: Passing array of int into SQL Server stored procedure Lit
8/15/2007 11:32:30 AM
sql server programming: I used the XML thingy...

Lit


[quoted text, click to view]

RE: Passing array of int into SQL Server stored procedure Alejandro Mesa
8/15/2007 11:42:03 AM
Arrays and Lists in SQL Server
http://www.sommarskog.se/arrays-in-sql.html

AMB

[quoted text, click to view]
Re: Passing array of int into SQL Server stored procedure --CELKO--
8/15/2007 1:05:45 PM
[quoted text, click to view]

Why not a long parameter list? EXEC Foobar (p1, p2,.. , pn) is a
simple call.

For my next book I have been going back and forth with a REALLY good
SQL Server programmer about doing a Sudoku in one statement; he passes
each cell in the 81 cell grid as parameters to get speed well under
one second on an home computer.

"Trust the Optimizer, Luke!" -- Obi Wan Celko
Re: Passing array of int into SQL Server stored procedure Anith Sen
8/15/2007 1:42:40 PM
There are a variety of ways you can do this, some here:
www.projectdmx.com/tsql/sqlarrays.aspx

--
Anith

Re: Passing array of int into SQL Server stored procedure TheSQLGuru
8/15/2007 5:00:43 PM
Always has for most entities.

--
TheSQLGuru
President
Indicium Resources, Inc.

[quoted text, click to view]

Re: Passing array of int into SQL Server stored procedure Mike C#
8/15/2007 5:10:30 PM

[quoted text, click to view]

How about one that solves 20 Soduku puzzles in parallel, requiring a mere
1,620 parameters.

Re: Passing array of int into SQL Server stored procedure Aaron Bertrand [SQL Server MVP]
8/15/2007 5:19:56 PM
[quoted text, click to view]

Because maintainability counts for a lot more than portability these days.

Passing array of int into SQL Server stored procedure guiroux22 NO[at]SPAM gmail.com
8/15/2007 6:17:33 PM
I want to pass array of int into a stored procedure to use it as a
list for a IN clause.
I already found this article which use an XML string parameter but i
think it's overkill
http://support.microsoft.com/?scid=kb%3Ben-us%3B555266&x=16&y=11

Maybe someone have something more simple ?

Thanks
Re: Passing array of int into SQL Server stored procedure Tony Rogerson
8/15/2007 10:21:31 PM
And I am still waiting for you to post the SQL so I can rip it to bits and
write it properly so you don't have to use FORCEPLAN.

When oh when are you going to post the code?

It's the 3rd time I've asked - 3rd time lucky?

--
Tony Rogerson, SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson
[Ramblings from the field from a SQL consultant]
http://sqlserverfaq.com
[UK SQL User Community]


[quoted text, click to view]
Re: Passing array of int into SQL Server stored procedure Madhivanan
8/16/2007 5:53:35 AM
Re: Passing array of int into SQL Server stored procedure Tony Rogerson
8/16/2007 7:23:20 AM
From my blog:
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2007/08/05/passing-an-array-csv-to-a-stored-procedure-with-data-validation-no-loops-no-self-joins-just-replace.aspx

exec csv_eg @csv = ',11,12,13,14,,,,,,,,,,,,,,12,',
@is_allow_nulls = 'Y',
@is_unique_values = 'N',
@is_debug = 'N'



CREATE PROC csv_eg
@csv varchar(500),
@is_allow_nulls char(1) = 'Y',
@is_unique_values char(1) = 'N',
@is_debug char(1) = 'N'
AS
BEGIN
/***
Convert a CSV to a TABLE with data validation

Tony Rogerson, Aug 2007

***/

SET @csv = ltrim(rtrim(@csv))

IF PATINDEX( '%[^,0-9]%', @csv ) > 0 -- Checks to make sure input is
digits or comma to prevent SQL injection
BEGIN
RAISERROR( 'Injection attempt or invalid data.', 16, 1 )
RETURN

END
-- Initial parse of input to make sure general syntax of the csv is
valid

-- check we have commas correct

IF LEFT( @csv, 1 ) <> ',' -- If not front comma then add one
SET @csv = ',' + @csv
IF RIGHT( @csv, 1 ) = ',' -- If last character is a comma then
add a NULL after it
SET @csv = @csv + 'NULL'

SET @csv = REPLACE( @csv, ',,', ',NULL,' ) -- Change ,, to NULL
SET @csv = REPLACE( @csv, ',,', ',NULL,' ) -- Need to do it twice for
rest of ,, not caught in first one

-- // initial parse is over.

-- Now the donkey work, convert it from a flat CSV structure to a TABLE
-- Simple string replacing, no need for self joins and loops

DECLARE @sql varchar(max)

SET @sql = REPLACE( @csv, ',', CHAR(13) + CHAR(10) + 'insert #csv_split
( csv_value ) values( ' )
SET @sql = REPLACE( @sql, CHAR(13), ' ); @V' + CHAR(13) )
SET @sql = RIGHT( @sql, LEN( @sql ) - 7 ) + ' ); @V'

SET @sql = REPLACE( @sql, '@V', '
if @@error <> 0 goto err;' )

-- We now have the SQL that will convert CSV to a TABLE, so exec it.
CREATE TABLE #csv_split (
val_position smallint not null IDENTITY,
csv_value int null CHECK( csv_value between 10 and 20 )
)

IF @is_allow_nulls = 'N'
EXEC( 'ALTER TABLE #csv_split ALTER COLUMN csv_value INT NOT NULL' )
IF @is_unique_values = 'Y'
EXEC( 'CREATE UNIQUE INDEX ncui ON #csv_split( csv_value )' )

SET @sql = 'SET NOCOUNT ON; ' + @sql + ';
goto done;
err:
DECLARE @msg varchar(120);
SET @msg = ''Data Error on value position '' + CAST( ( SELECT COUNT(*)
FROM #csv_split ) + 1 AS varchar(5) ) + '', processing terminated.'';
RAISERROR( @msg, 16, 1 );
done:'

IF @is_debug = 'Y'
PRINT @sql

EXEC( @sql )

IF @@ERROR <> 0
RETURN

SELECT *
FROM #csv_split

END

--
Tony Rogerson, SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson
[Ramblings from the field from a SQL consultant]
http://sqlserverfaq.com
[UK SQL User Community]


[quoted text, click to view]
AddThis Social Bookmark Button