Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts

Friday, February 24, 2012

arrays in tsql?

Hi;

I have been writing a lot of short tsql scripts to fix a lot of tiny
database issues.

I was wondering if a could make an array of strings in tsql that I
could process in a loop, something like

array arrayListOfTablesToProcess = { "orders", "phone",
"complaints"}

for( int i = 0; i < arrayListOfTablesToProcess.length; i++ )
delete from arrayListOfTablesToProcess[i]

Can you do something like this in tsql?

I should probably stop asking all of these questions.

Is there a good book on TSQL alone ( I'm not interested in wizards,
just scripting )...that is short?

SteveThere are no arrays in TSQL. You are thinking in terms of a procedural
solution rather than a set-based solution. In general, you should be able to
use a table and set-based, SELECT/UPDATE/DELETE statements to process
"lists" of data. If you define your problem more specifically someone here
may be able to help with the set-based solution.

--
David Portas
----
Please reply only to the newsgroup
--|||Hi

In addition to David's post check out:

http://www.algonet.se/~sommar/arrays-in-sql.html

John

"Steve" <stevesusenet@.yahoo.com> wrote in message
news:6f8cb8c9.0310040337.52427942@.posting.google.c om...
> Hi;
> I have been writing a lot of short tsql scripts to fix a lot of tiny
> database issues.
> I was wondering if a could make an array of strings in tsql that I
> could process in a loop, something like
>
> array arrayListOfTablesToProcess = { "orders", "phone",
> "complaints"}
> for( int i = 0; i < arrayListOfTablesToProcess.length; i++ )
> delete from arrayListOfTablesToProcess[i]
>
>
> Can you do something like this in tsql?
> I should probably stop asking all of these questions.
> Is there a good book on TSQL alone ( I'm not interested in wizards,
> just scripting )...that is short?
> Steve

Arrays in SQL server

I am creating an application for booking events. Each event has several dates and each date has a fixed amount of available seats.

Currently in the events table I have a field for the number of available seats and a field for the string of dates that are later parsed into a listbox control.

The problem with my current setup is each date is sharing the same number of available seats so if date Event A is decemented 10 so will Event B.

I need some way to associate each date with it's own number of available seats. What is the best way to do this? An array? If so how do I store an array in the DB?

Thanks, Justin.::I need some way to associate each date with it's own number of available seats. What is
::the best way to do this? An array?

By NO means.

The best way is to apply proper standard procedures for REALATIONAL databases. There is something called "normalization", which, btw., is explained in EVERY beginner book about databases.

Som ,y advice is: your best way is to shell out some money and read a ookabout how a database works. Something simple, like "SQL for Dummies".

You will end up with multiple tables, and an event being on multiple occations will have multiple entries, one for every date.

This:

::and a field for the string of dates that are later parsed into a listbox control.

is something that you better make sure your boss does not see. I would terminate everyone doing something like this immediately for gross incompetence in 99.99% of the cases. The relational model is VERY clear with how things like this should be modelled, and has been so for the last 30-sth years. Your model is denormalized and it is sure you run into trouble.

Really, get a beginner book about databases.|||As Thona states - Normalization is the key.
when you build a denormalized database, your asking for trouble.

With limited knowledge of what your trying to accomplish, I would envision a structure more like this...

tblEvent
--EventId (autonumber)
--EventDescription

tblEventInstance
--EventInstanceId (autonumber)
--EventId (integer)
--EventDate (date/time)
--EventSeats (integer)

tblEventSignUp
--EventInstanceId (integer)
--Person

etc...

anytime you're packing multiple pieces of data into a single column you gotta ask yourself "why?"

Arrays in SQL database

Hi;
Can MS-SQL support Arrays? I need a float that occurs 72 times in a
table and I don't want to have 72 seperate fields but can't see a way of
creating an array?
Thanks in advance.
David
*** Sent via Developersdex http://www.examnotes.net ***Hi,
There is no array concept in SQL Server. But you could use Dynamic SQL's to
provide an equalent.
Look into the great article:-
http://www.sommarskog.se/arrays-in-sql.html
Thanks
Hari
SQL Server MVP
"DavidC" <Cervelli@.Adelphia.Net> wrote in message
news:OOBP485XFHA.1240@.TK2MSFTNGP14.phx.gbl...
> Hi;
> Can MS-SQL support Arrays? I need a float that occurs 72 times in a
> table and I don't want to have 72 seperate fields but can't see a way of
> creating an array?
> Thanks in advance.
> David
>
> *** Sent via Developersdex http://www.examnotes.net ***|||No, SQL Server does not support arrays
Yo can use either dynamic sql or udf
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
"DavidC" <Cervelli@.Adelphia.Net> wrote in message
news:OOBP485XFHA.1240@.TK2MSFTNGP14.phx.gbl...
> Hi;
> Can MS-SQL support Arrays? I need a float that occurs 72 times in a
> table and I don't want to have 72 seperate fields but can't see a way of
> creating an array?
> Thanks in advance.
> David
>
> *** Sent via Developersdex http://www.examnotes.net ***|||You can share them by (global) temporary tables, just follow the link Hari
presented.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"DavidC" <Cervelli@.Adelphia.Net> schrieb im Newsbeitrag
news:OOBP485XFHA.1240@.TK2MSFTNGP14.phx.gbl...
> Hi;
> Can MS-SQL support Arrays? I need a float that occurs 72 times in a
> table and I don't want to have 72 seperate fields but can't see a way of
> creating an array?
> Thanks in advance.
> David
>
> *** Sent via Developersdex http://www.examnotes.net ***|||
> I need a float that occurs 72 times in a
> table and I don't want to have 72 seperate fields but can't see a way of
> creating an array?
Having just spent 20+ hours days undoing an "Array" column (i.e., column
containing a string with comma separated values), I can assure you that
having 72 columns is much better than having a CSV-string.
An "array" is a terrible hack and a huge design flaw created by a lazy
programmer.
Yeah, it's more work. Yeah, it's not fun. But, this is why they pay you the
big bucks.
Just do it.
Perhaps if you share the problem domain we may be able to help with a
cleaner design for this (aparantly) complex data.
Alex Papadimoulis
http://weblogs.asp.net/Alex_Papadimoulis|||>> Can MS-SQL support Arrays?
No
Perhaps you can make your overall requirement clearer. Most likely your
logical design is flawed which forces you to come up with such complex table
structures. Are you sure these are truly 72 distinct attributes of the
entity type you are modeling? Or does the collective 72 repetitions form a
single attribute from a conceptual POV?
Anith|||Even if the number of float values will be constant at 72 per record, it
seems the logical place to store them in a relational database would be a
foreign key table. Or perhaps we are talking about a lookup table with 72
records. Loading the values into an array would be something you implement
on the presentation / GUI side. Am I understanding what it is you want to
do?
"DavidC" <Cervelli@.Adelphia.Net> wrote in message
news:OOBP485XFHA.1240@.TK2MSFTNGP14.phx.gbl...
> Hi;
> Can MS-SQL support Arrays? I need a float that occurs 72 times in a
> table and I don't want to have 72 seperate fields but can't see a way of
> creating an array?
> Thanks in advance.
> David
>
> *** Sent via Developersdex http://www.examnotes.net ***

Arrays in a Stored Procedure

Can any one help me with a sample code, which can take an array of

elements as one of it's parameters and get the value inserted into a table in a

stored procedure.

Thanks in advance

vnswathi.

SQL dosen't support array, so how do you want to passing an 'array' of elements as parameters to a stored procedure? Maybe you can use a string that delimits the elements of an array by some character(s) and then split the string into a set of values that can be inserted into a table. If so, the key point is how to split the string with specific delimitor, and you can take a look at this post:

http://forums.asp.net/thread/1300724.aspx

Arrays in a SQL2k DB

Hi,
I need to store a 2D Array in a Table.
What is the easiest way to to this. the array would only contain integer
values..
anyone a better idea than "implode" the Array in a string and "explode" it
again in the program code?
Nilshttp://www.aspfaq.com/2248
"Nils Wolf" <nisse@.gmx.at> wrote in message
news:e9bT6j4yFHA.1032@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I need to store a 2D Array in a Table.
> What is the easiest way to to this. the array would only contain integer
> values..
> anyone a better idea than "implode" the Array in a string and "explode" it
> again in the program code?
> Nils|||"Nils Wolf" <nisse@.gmx.at> wrote in message
news:e9bT6j4yFHA.1032@.TK2MSFTNGP12.phx.gbl...
> Hi,
> I need to store a 2D Array in a Table.
> What is the easiest way to to this. the array would only contain integer
> values..
> anyone a better idea than "implode" the Array in a string and "explode" it
> again in the program code?
>
Well, storing it in a binary IMAGE column, or VARBINARY if it's small is a
whole lot better than using a string. Most programming environments know
how to convert integer arrays back and forth to byte arrays, and SQL Server
knows how to store and retrieve byte arrays.
But consider whether it's really best to hide the structure of your data
from SQL Server. Sometimes there's really no alternative, but if you can
find a way to model your data with tables and rows, it might be easier to
store, retrieve and manipulate, and if your arrays are sparse it may even be
more efficient.
David|||the Array is not so small.
it would be at least (100 x 100) up to (400 x 300)
the point is, that i need one array per row in a table with about 1000
rows...
Nils
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> schrieb im
Newsbeitrag news:uqw%23Wq4yFHA.2424@.TK2MSFTNGP12.phx.gbl...
> "Nils Wolf" <nisse@.gmx.at> wrote in message
> news:e9bT6j4yFHA.1032@.TK2MSFTNGP12.phx.gbl...
> Well, storing it in a binary IMAGE column, or VARBINARY if it's small is a
> whole lot better than using a string. Most programming environments know
> how to convert integer arrays back and forth to byte arrays, and SQL
> Server knows how to store and retrieve byte arrays.
> But consider whether it's really best to hide the structure of your data
> from SQL Server. Sometimes there's really no alternative, but if you can
> find a way to model your data with tables and rows, it might be easier to
> store, retrieve and manipulate, and if your arrays are sparse it may even
> be more efficient.
> David
>|||Then IMAGE, not VARBINARY.
David
"Nils Wolf" <nisse@.gmx.at> wrote in message
news:uyqrCv4yFHA.3860@.TK2MSFTNGP09.phx.gbl...
> the Array is not so small.
> it would be at least (100 x 100) up to (400 x 300)
> the point is, that i need one array per row in a table with about 1000
> rows...
> Nils
>
> "David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> schrieb im
> Newsbeitrag news:uqw%23Wq4yFHA.2424@.TK2MSFTNGP12.phx.gbl...
>

Arrays and lists in SQL 2005

I am glad to announce that there is now a version of my article "Arrays and
Lists in SQL Server" for SQL 2005 available on my web site.

THe URL for the article is
http://www.sommarskog.se/arrays-in-sql-2005.html. If you are curious about
the performance numbers they are in an appendix at
http://www.sommarskog.se/arrays-in-sql-perftest.html.

The old version of the aritlce remains, as the new article covers SQL 2005
only. The old version is now at
http://www.sommarskog.se/arrays-in-sql-2000.html.

The old URL, http://www.sommarskog.se/arrays-in-sql.html leads to a page
that links to both articles.

And the reason that there are two articles is simply that SQL 2005 adds
so many new features: nvarchar(MAX), the CLR, the xml data type, CTE that
all can be used in the realm of arrays and lists.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspxGreat article Erland! The performance analysis is an excellent reading by
itself! Few people would take the time to implement such a detailed test.

Thank you for sharing!

Plamen Ratchev
http://www.SQLStudio.com

arrays

Thanks! I am aware that you can't pass an array to an sp. i was aware of tha
t
even when i asked that question. I simply did so, because I needed to prove
this to somebody.
"louise raisbeck" wrote:
> this post was a few days ago so no idea if you will all read this but just
> read this post as need to do a similar thing. this is an open forum to hel
p
> sql developers around the world and Joe your attitude was quite frankly ru
de
> and unnecessary and felt you needn't have bothered with the energy involve
d
> in replying.
> I am also writing a stored procedure and have to put an array of values in
to
> a stored procedure (from an asp.net drop down list). I may be wrong but I
> think that Preeta may have been asking how BEST to handle an array of valu
es,
> not how to declare an array in a stored procedure, which of course you can
t
> do. Preeta I have found that the best way to do this really is to pass a C
SV
> (comma delimited) list of values into a variable of nvarchar then split ou
t
> the values in the stored procedure.
> Or you could do what Joe suggested and go learn how to think and write
> proper sql code and dont ask a question until you do just that!!!!
> regards,
> "Joe Celko" wrote:
>you can't directly pass arrays, at least not in SQL Server.
However, there are some techniques that you can use, see:
http://www.sommarskog.se/arrays-in-sql.html

Sunday, February 12, 2012

Are there such a thing as arrays in TSQL?

I'm have a stored procedure that iterates through a list of numbers and adds an item for each number (user id) some of these ids are duplicates which is fine even necessary for the first part of my query but for the last I need to ensure that no duplicates id's are passed to the stored procedure, in this case called 'spInsertForBackupNote'. My thoughts here was to do something like this:

SET @.Note_Buffer = @.UserID -- @.Note_Buffer being some kind of array?

IF @.Note_Buffer = @.UserID -- If its been added to the buffer we dont execute sp
BEGIN
Do Nothing here
END

ELSE

BEGIN
EXECUTE spInsertForBackupNote @.FK_UserID, @.FK_NoteID
END

I know this would never work because it would always be false since I just added the same userid to the buffer that I want to add. But I think you see my problem. I know it should be an easy one but my TSQL is limited. I've posted the whole sp. Hope someone can help.

CREATE PROCEDURE spInsertAssignedNotesByList
@.FK_UserIDList NVARCHAR(4000) = NULL,
@.FK_NoteIDList NVARCHAR(4000) = NULL,
@.By_Who INT,
@.UserID INT

AS
SET NOCOUNT ON

DECLARE @.Length INT
DECLARE @.Note_Length INT
DECLARE @.Note_Buffer INT

DECLARE @.FirstUserIDWord NVARCHAR(4000)
DECLARE @.FirstNoteIDWord NVARCHAR(4000)

DECLARE @.FK_UserID INT
DECLARE @.FK_NoteID INT

SELECT @.Length = DATALENGTH(@.FK_UserIDList )
SELECT @.Note_Length = DATALENGTH(@.FK_NoteIDList )

DECLARE @.TempFK_NoteIDList NVARCHAR(4000) --= NULL
DECLARE @.Temp_NoteLength INT

SET @.TempFK_NoteIDList = @.FK_NoteIDList
SET @.Temp_NoteLength = DATALENGTH(@.FK_NoteIDList )

-- IF @.Length > @.Note_Length -- If we have more users than notes

BEGIN

WHILE @.Length > 0
BEGIN

IF @.Length > 0

EXECUTE @.Length = PopFirstWord @.FK_UserIDList OUTPUT, @.FirstUserIDWord OUTPUT
SELECT @.FK_UserID = CONVERT(INT, @.FirstUserIDWord)

IF @.Length > 0
BEGIN

SET @.FK_NoteIDList = @.TempFK_NoteIDList
SET @.Note_Length = @.Temp_NoteLength

WHILE @.Note_Length > 0
BEGIN
EXECUTE @.Note_Length = PopFirstWord @.FK_NoteIDList OUTPUT, @.FirstNoteIDWord OUTPUT
SELECT @.FK_NoteID = CONVERT(INT, @.FirstNoteIDWord)

IF @.Note_Length > 0
EXECUTE spInsertAssignedNoteDetail @.FK_UserID, @.FK_NoteID

SET @.Note_Buffer = @.UserID
EXECUTE spInsertForBackupNote @.FK_UserID, @.FK_NoteID, @.By_Who, @.UserID -- NEW HERE
END
END

END
END

----------------
GOThere are not arrays in TSQL (at least the current version).

You can simulate it by placing the values into a temp table (perhaps by passing in a delimited string, and then using a user defined function that returns a table variable). Then, once you have the table, you can do what TSQL is best at, Set operations.|||can you show an example of how to display the contents of a table variable?

Thanks|||What do you mean by "display"? SQL Server runs on the server, and as such really does not expose a user interface.|||the UDF returns a table variable and I need to bind to it and display the contents on a .net web page.|||At the end of the SP,

SELECT * FROM TableVariable

Do an ExecuteReader or similar and bind the datareader to the grid, whatever.|||I am using a strongly typed dataset and am using com.executenonquery to bind to a datagrid and the results are:
I get back the columns names from the function or sp (they both do the same) but the columns are empty.