Friday, February 24, 2012

array?

Hello,
I was wondering if there is an alternative for an array in sql server.
What i would like (correct me if i approach this the wrong way) is an array
which i can use in a where clause based on a variable/parameter.
for example:
using a select statement to retrieve a list of titles or id's as integers.
titles:
book1
book2
book3
id's:
1
2
3
Pass that list into a variable/paramater of a second select statement like
select * from mytable where titles in @.variable/parameter. In this case the
values should be concated with quotes and commas like 'book1', 'book2'. If
the values are integer then 1, 2, 3.
Is this possible?
----
This mailbox protected from junk email by MailFrontier Desktop
from MailFrontier, Inc. http://info.mailfrontier.comhttp://www.sommarskog.se/arrays-in-sql.html
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Jason" <jlewis@.homail.com> wrote in message
news:ebwwjblHFHA.1476@.TK2MSFTNGP09.phx.gbl...
> Hello,
> I was wondering if there is an alternative for an array in sql server.
> What i would like (correct me if i approach this the wrong way) is an
> array which i can use in a where clause based on a variable/parameter.
> for example:
> using a select statement to retrieve a list of titles or id's as integers.
> titles:
> book1
> book2
> book3
> id's:
> 1
> 2
> 3
> Pass that list into a variable/paramater of a second select statement like
> select * from mytable where titles in @.variable/parameter. In this case
> the values should be concated with quotes and commas like 'book1',
> 'book2'. If the values are integer then 1, 2, 3.
> Is this possible?
> --
>
> ----
> This mailbox protected from junk email by MailFrontier Desktop
> from MailFrontier, Inc. http://info.mailfrontier.com
>|||Jason
CREATE PROCEDURE array_method_1
@.array nvarchar(4000)
AS
BEGIN
SET NOCOUNT ON
DECLARE @.nsql nvarchar(4000)
SET @.nsql = '
SELECT *
FROM sysobjects
WHERE name IN ( ' + @.array + ')'
PRINT @.nsql
EXEC sp_executesql @.nsql
END
GO
EXEC array_method_1
@.array = '''sysobjects'',''sysindexes'',''syscolu
mns'''
GO
"Jason" <jlewis@.homail.com> wrote in message
news:ebwwjblHFHA.1476@.TK2MSFTNGP09.phx.gbl...
> Hello,
> I was wondering if there is an alternative for an array in sql server.
> What i would like (correct me if i approach this the wrong way) is an
array
> which i can use in a where clause based on a variable/parameter.
> for example:
> using a select statement to retrieve a list of titles or id's as integers.
> titles:
> book1
> book2
> book3
> id's:
> 1
> 2
> 3
> Pass that list into a variable/paramater of a second select statement like
> select * from mytable where titles in @.variable/parameter. In this case
the
> values should be concated with quotes and commas like 'book1', 'book2'. If
> the values are integer then 1, 2, 3.
> Is this possible?
> --
>
> ----
> This mailbox protected from junk email by MailFrontier Desktop
> from MailFrontier, Inc. http://info.mailfrontier.com
>|||I have 6 different ways of doig this, at:
http://vyaskn.tripod.com/passing_ar..._procedures.htm
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Jason" <jlewis@.homail.com> wrote in message
news:ebwwjblHFHA.1476@.TK2MSFTNGP09.phx.gbl...
Hello,
I was wondering if there is an alternative for an array in sql server.
What i would like (correct me if i approach this the wrong way) is an array
which i can use in a where clause based on a variable/parameter.
for example:
using a select statement to retrieve a list of titles or id's as integers.
titles:
book1
book2
book3
id's:
1
2
3
Pass that list into a variable/paramater of a second select statement like
select * from mytable where titles in @.variable/parameter. In this case the
values should be concated with quotes and commas like 'book1', 'book2'. If
the values are integer then 1, 2, 3.
Is this possible?
----
This mailbox protected from junk email by MailFrontier Desktop
from MailFrontier, Inc. http://info.mailfrontier.com

No comments:

Post a Comment