473,661 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array in TSQL?

Hello,

I have some code that adds a new user. The new user has a checkboxlist of
items which they can be associated with. I would like to send this list of
items to TSQL along with the new user information. I would guess to combine
the selected items like so: "6,4,8,19,2 ".

Kind of do the following:

INSERT into tblUser (fields) VALUES (data)
Declare @userID as integer
SET @UserID = @@IDENTITY

for each item in @Selected
INSERT into tblSelections (field) VALUE (item, @UserID)

I know above isn't exactly possible, but can something similiar be done? I
dont want to have to run a proc for each item from my asp.net pages....

Thanks,

--
David Lozzi
Web Applications Developer
dlozzi@(remove-this)delphi-ts.com


Nov 19 '05 #1
16 5080
It would definetely be easiest to call a proc each time. Othewise you
could pass in the comma seperated string into the proc, and then do a
while loop with that string.

While CharIndex(",",@ Selected) != 0
Begin
-- Get Value before first comma code

-- Insert value code

-- Delete first value and comma code
End

Nov 19 '05 #2
David Lozzi (Da********@nos pam.nospam) writes:
I have some code that adds a new user. The new user has a checkboxlist
of items which they can be associated with. I would like to send this
list of items to TSQL along with the new user information. I would guess
to combine the selected items like so: "6,4,8,19,2 ". >
Kind of do the following:

INSERT into tblUser (fields) VALUES (data)
Declare @userID as integer
SET @UserID = @@IDENTITY

for each item in @Selected
INSERT into tblSelections (field) VALUE (item, @UserID)

I know above isn't exactly possible, but can something similiar be done? I
dont want to have to run a proc for each item from my asp.net pages....


See http://www.sommarskog.se/arrays-in-sql.html#iterative for some
solutions.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Nov 19 '05 #3
You have missed the foundations of RDBMS and really need to get a book
or a class before you try to code anything.

You want to violate First Normal Form (1NF). All data values are
scalar; there are no arrays. Each of those attribures would be a
separate column.

Rows are not records; fields are not columns; tables are not files.

We do not put silly redundant prefixes like "tbl-" on table names.
Look up ISO-11179.

I hope you know that IDENTITY cannot be a relational key by definition.
But you are using a bad thing in the wrong way. It mimics a
sequential file record number counter without your intervention when
you declare it as part of the DDL.

Do you know about check digits, a Regular Expression or some other rule
to validate your user id?

SQL is a set-oriented language, so you can insert a query result into a
base table or updatable VIEW. You are writing SQL like it was a 3GL.
You need a lot more help thanyou can get in a Newsgroup.

Nov 19 '05 #4
I believe SQL Server 2005 supports arrays. I'm just getting into it, though.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"--CELKO--" <jc*******@eart hlink.net> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
You have missed the foundations of RDBMS and really need to get a book
or a class before you try to code anything.

You want to violate First Normal Form (1NF). All data values are
scalar; there are no arrays. Each of those attribures would be a
separate column.

Rows are not records; fields are not columns; tables are not files.

We do not put silly redundant prefixes like "tbl-" on table names.
Look up ISO-11179.

I hope you know that IDENTITY cannot be a relational key by definition.
But you are using a bad thing in the wrong way. It mimics a
sequential file record number counter without your intervention when
you declare it as part of the DDL.

Do you know about check digits, a Regular Expression or some other rule
to validate your user id?

SQL is a set-oriented language, so you can insert a query result into a
base table or updatable VIEW. You are writing SQL like it was a 3GL.
You need a lot more help thanyou can get in a Newsgroup.

Nov 19 '05 #5
trival, wrtite a user function that converts a comma seperated list into a
table (the sql equiv of an array)

create function dbo.parseList (@s varchar(2000))
returns @values table (value varchar(2000))
as begin
declare @v varchar(2000) ,@i int
set @i = patIndex('%,%', @s)
while @i > 0 begin
insert @values values (substring(@s,1 ,@i-1))
set @s = substring(@s,@i +1,len(@s) - @i)
set @i = patIndex('%,%', @s)
end
insert @values values (@s)
return
end

then call like:

INSERT into tblUser (fields) VALUES (data)
SET @UserID = scope_identity( )

INSERT tblSelections (field,userid)
select value, @UserID
from dbo.parseList(@ selected)
-- bruce (sqlwork.com)


"David Lozzi" <Da********@nos pam.nospam> wrote in message
news:e0******** ******@TK2MSFTN GP12.phx.gbl...
Hello,

I have some code that adds a new user. The new user has a checkboxlist of
items which they can be associated with. I would like to send this list of
items to TSQL along with the new user information. I would guess to
combine the selected items like so: "6,4,8,19,2 ".

Kind of do the following:

INSERT into tblUser (fields) VALUES (data)
Declare @userID as integer
SET @UserID = @@IDENTITY

for each item in @Selected
INSERT into tblSelections (field) VALUE (item, @UserID)

I know above isn't exactly possible, but can something similiar be done? I
dont want to have to run a proc for each item from my asp.net pages....

Thanks,

--
David Lozzi
Web Applications Developer
dlozzi@(remove-this)delphi-ts.com

Nov 19 '05 #6
Thanks for your help.

:-|

--
David Lozzi
Web Applications Developer
dlozzi@(remove-this)delphi-ts.com

"--CELKO--" <jc*******@eart hlink.net> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
You have missed the foundations of RDBMS and really need to get a book
or a class before you try to code anything.

You want to violate First Normal Form (1NF). All data values are
scalar; there are no arrays. Each of those attribures would be a
separate column.

Rows are not records; fields are not columns; tables are not files.

We do not put silly redundant prefixes like "tbl-" on table names.
Look up ISO-11179.

I hope you know that IDENTITY cannot be a relational key by definition.
But you are using a bad thing in the wrong way. It mimics a
sequential file record number counter without your intervention when
you declare it as part of the DDL.

Do you know about check digits, a Regular Expression or some other rule
to validate your user id?

SQL is a set-oriented language, so you can insert a query result into a
base table or updatable VIEW. You are writing SQL like it was a 3GL.
You need a lot more help thanyou can get in a Newsgroup.

Nov 19 '05 #7
Hi David,

If you really need to pass a combined string and let the TSQL perfom a loop
command execution (instead of constantly execute sqlcommand in .NET code) ,
I'm afraid we have to manually parse the string in TSQL code. Also, it's
better to encapsulate such code in a stored procedure to improve
performance. I think the article Erland has provided is useful for you.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "David Lozzi" <Da********@nos pam.nospam>
| References: <e0************ **@TK2MSFTNGP12 .phx.gbl>
<11************ *********@g49g2 000cwa.googlegr oups.com>
| Subject: Re: Array in TSQL?
| Date: Tue, 1 Nov 2005 20:48:50 -0500
| Lines: 41
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <ue************ *@tk2msftngp13. phx.gbl>
| Newsgroups:
microsoft.publi c.dotnet.framew ork.aspnet,micr osoft.public.sq lserver.program m
ing
| NNTP-Posting-Host: c-24-63-42-200.hsd1.ma.com cast.net 24.63.42.200
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!tk 2msftngp13.phx. gbl
| Xref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.sqlserver.pro gramming:128510
microsoft.publi c.dotnet.framew ork.aspnet:1353 92
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
|
| Thanks for your help.
|
| :-|
|
| --
| David Lozzi
| Web Applications Developer
| dlozzi@(remove-this)delphi-ts.com
|
|
|
| "--CELKO--" <jc*******@eart hlink.net> wrote in message
| news:11******** *************@g 49g2000cwa.goog legroups.com...
| > You have missed the foundations of RDBMS and really need to get a book
| > or a class before you try to code anything.
| >
| > You want to violate First Normal Form (1NF). All data values are
| > scalar; there are no arrays. Each of those attribures would be a
| > separate column.
| >
| > Rows are not records; fields are not columns; tables are not files.
| >
| > We do not put silly redundant prefixes like "tbl-" on table names.
| > Look up ISO-11179.
| >
| > I hope you know that IDENTITY cannot be a relational key by definition.
| > But you are using a bad thing in the wrong way. It mimics a
| > sequential file record number counter without your intervention when
| > you declare it as part of the DDL.
| >
| > Do you know about check digits, a Regular Expression or some other rule
| > to validate your user id?
| >
| > SQL is a set-oriented language, so you can insert a query result into a
| > base table or updatable VIEW. You are writing SQL like it was a 3GL.
| >
| >
| > You need a lot more help thanyou can get in a Newsgroup.
| >
|
|
|

Nov 19 '05 #8
>I believe SQL Server 2005 supports arrays
Really?

--
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I believe SQL Server 2005 supports arrays. I'm just getting into it,
though.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
A watched clock never boils.

"--CELKO--" <jc*******@eart hlink.net> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
You have missed the foundations of RDBMS and really need to get a book
or a class before you try to code anything.

You want to violate First Normal Form (1NF). All data values are
scalar; there are no arrays. Each of those attribures would be a
separate column.

Rows are not records; fields are not columns; tables are not files.

We do not put silly redundant prefixes like "tbl-" on table names.
Look up ISO-11179.

I hope you know that IDENTITY cannot be a relational key by definition.
But you are using a bad thing in the wrong way. It mimics a
sequential file record number counter without your intervention when
you declare it as part of the DDL.

Do you know about check digits, a Regular Expression or some other rule
to validate your user id?

SQL is a set-oriented language, so you can insert a query result into a
base table or updatable VIEW. You are writing SQL like it was a 3GL.
You need a lot more help thanyou can get in a Newsgroup.


Nov 19 '05 #9
Hi Roji,

I think so. SQL 2005 has integrated the .NET CLR internally. So we can used
net code to create managed store procedure which can utilize most of the
NET basic classes and types.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Roji. P. Thomas" <th********@gma il.com>
| References: <e0************ **@TK2MSFTNGP12 .phx.gbl>
<11************ *********@g49g2 000cwa.googlegr oups.com>
<#N************ **@TK2MSFTNGP09 .phx.gbl>
| Subject: Re: Array in TSQL?
| Date: Wed, 2 Nov 2005 12:34:51 +0530
| Lines: 54
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Response
| Message-ID: <Oy************ **@tk2msftngp13 .phx.gbl>
| Newsgroups:
microsoft.publi c.dotnet.framew ork.aspnet,micr osoft.public.sq lserver.program m
ing
| NNTP-Posting-Host: 180.239.88.202. asianet.co.in 202.88.239.180
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!tk 2msftngp13.phx. gbl
| Xref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.sqlserver.pro gramming:128538
microsoft.publi c.dotnet.framew ork.aspnet:1354 33
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
|
| >I believe SQL Server 2005 supports arrays
| Really?
|
| --
| Roji. P. Thomas
| Net Asset Management
| http://toponewithties.blogspot.com
|
|
| "Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
| news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
| >I believe SQL Server 2005 supports arrays. I'm just getting into it,
| >though.
| >
| > --
| > HTH,
| >
| > Kevin Spencer
| > Microsoft MVP
| > .Net Developer
| > A watched clock never boils.
| >
| > "--CELKO--" <jc*******@eart hlink.net> wrote in message
| > news:11******** *************@g 49g2000cwa.goog legroups.com...
| >> You have missed the foundations of RDBMS and really need to get a book
| >> or a class before you try to code anything.
| >>
| >> You want to violate First Normal Form (1NF). All data values are
| >> scalar; there are no arrays. Each of those attribures would be a
| >> separate column.
| >>
| >> Rows are not records; fields are not columns; tables are not files.
| >>
| >> We do not put silly redundant prefixes like "tbl-" on table names.
| >> Look up ISO-11179.
| >>
| >> I hope you know that IDENTITY cannot be a relational key by definition.
| >> But you are using a bad thing in the wrong way. It mimics a
| >> sequential file record number counter without your intervention when
| >> you declare it as part of the DDL.
| >>
| >> Do you know about check digits, a Regular Expression or some other rule
| >> to validate your user id?
| >>
| >> SQL is a set-oriented language, so you can insert a query result into a
| >> base table or updatable VIEW. You are writing SQL like it was a 3GL.
| >>
| >>
| >> You need a lot more help thanyou can get in a Newsgroup.
| >>
| >
| >
|
|
|

Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5469
by: Steve | last post by:
Hi; I'm brand spanking new to sqlserver ( nice so far ). I need to make a simple data change across a list of tables. Basically replace an old date with a new date. However, the people I am doing it for want a program that produces logging of what it does.
18
4598
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between an end-user in an organization and the database, through the exclusive use of stored procedures which are authored by the organization or by software developers. All development work at the application software level may thereby be conducted...
2
18701
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would sequentially call the *.sql files? i.e. call schemaVersionCheck.sql call addFieldToLoanTable.sql call updateLoanTrigger.sql
1
1725
by: TOM GUGGER | last post by:
OMNI GROUP tgugger@aimexec.com T-SQL/ CONTRACT TO PERM/ ATLANTA
3
2533
by: David Lozzi | last post by:
Howdy, ISSUE 1: See issue 2 below. I have a distance calculator on my site which works great. However, the users need to sort by distance, which make sense. I'm not sure how to do it other than like this. With the returning query include the distance from origin. Here's my dilemma, I have the script working great in VB which provides the distance, but that is not sortable, but when I port it over to TSQL I get differing results. Here is the...
7
9379
by: Filips Benoit | last post by:
Dear all, Tables: COMPANY: COM_ID, COM_NAME, ..... PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE ( nvarchar) COMPANY_PROPERTY: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE (nvarchar) Use: Without adding new field the user can add new properties to the companies just by adding a new property in table PROPERTY and mapping the
8
1451
by: David Lozzi | last post by:
I'm fairly new to ASP.Net 2.0 SQLDatasource objects. It defaults using TSQL statments for the SELECT, INSERT, UPDATE, DELETE commands, which is great and it works. However, I've always been taught that all SQL work should be completed by the SQL server, therefore use stored procedures even for the simplest of select statements. Does this still hold true for sqldatasources? Should I use procs instead of the default tsql?? Thanks, ...
0
3142
by: DR | last post by:
Unable to start TSQL Debugging. Could not attach to SQL Server Process on 'srvname'. The RPC server is unavailable. I get this error when I try to run a SQL Server Project with a CLR stored Procedure in it. The target DB is SQL Server 2005 and im using VS 2005. I simply create a new SQL Server Project which creates a Test.sql, i then simply added a Stored Procedure to it called the default name StoredProcedure1.cs and put a break point...
1
6067
by: Sagaert Johan | last post by:
Hi How can i create an sql server login (sql 2005 express) ? I have my TSQL code ready but how can i run it from c#? How can i execute TSQL code from Csharp, or are there better ways using some of the Microsoft.SqlServer.Management classes to manage server logins?
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.