Are There Such A Thing As Arrays In TSQL?
Feb 10, 2004
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
--------------------------------------------------
GO
View 6 Replies
ADVERTISEMENT
Jul 20, 2005
Hi;I have been writing a lot of short tsql scripts to fix a lot of tinydatabase issues.I was wondering if a could make an array of strings in tsql that Icould process in a loop, something likearray 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
View 2 Replies
View Related
Dec 6, 2007
As a burgeoning SQL developer I have never really understood the need for SQL Server / Enterprise Manager to show us the whole dbo.Table name thing. What is dbo, and why do we need to know deal with it?With that said, in my SQL 2005 Express database all of my project tables for my project management demo were named guard.pgUsers for example and not dbo.pgUsers.How come? Why did they get named different on their own?
View 2 Replies
View Related
Oct 1, 2002
Hi all,
New to MS-SQL. Is there any concept of arrays in MS-SQL. If yes, how to implement it and where can i find more information?
Thank you
Suresh
View 5 Replies
View Related
Oct 13, 1999
hi, i'm passing a string to a Stored procedure(sp).this is string is delimited, say,
mon:tue:wed..is there a way in a sp to parse this string out an store it in an array and then use a for i=i to..whatever.. loop to access each element?
thanks
rohit
View 2 Replies
View Related
Feb 8, 2008
Is there any way to mimic the functionality of a dynamic array using T-SQL?
View 13 Replies
View Related
Apr 22, 2004
I have data I am retrieving using a datareader...and SQLSERVER
It could return 1 row of information or perhaps 3 rows of information
I need to know how to use an array here I would guess so I can access each element in this row or rows.
HOw might I use reader.read and get it into the array
View 4 Replies
View Related
Dec 4, 2004
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.
View 2 Replies
View Related
Mar 25, 2003
After being put in a position where I had to deal with a SQL problem without very little SQL knowledge I have screwed a clients main database up.
I backed up the transaction log and then deleted the log. The database is shown as suspect and won't let me restore.
Am I F&%ked?
Any help in sorting this out will be rewarded with much kudos and thanks
View 12 Replies
View Related
Aug 26, 2004
See Attachment
View 10 Replies
View Related
Apr 1, 2008
Hi,
I'm a complete newbie to SQL, and I need to know if there's an easy way to do this....I'm working with PHP, and it uses the following SQL statement:
$query = mysql_query("INSERT INTO `databaseName_tags`.`data` (`order` ,`name` ,`rating` ,`info` ,`comment` ,`comment2`)
VALUES ('0','abc','5','helllo','great work','woohooo')");
But what I'd prefer to do, is to have two arrays like so:
$cols = array("order" ,"name" ,"rating" ,"info" ,"comment","comment2");
$data = array("0","abc","5","helllo","great work","woohooo");
$query = mysql_query("INSERT INTO `databaseName_tags`.`data` ($cols) VALUES ($data)");
Is it possible to do it something like what is given above? It'd make the program so much more flexible....
View 6 Replies
View Related
Mar 31, 2008
I try to do:
DECLARE
@ThisMinute DateTime,
@ThisDay DateTime
SET@ThisMinute = DATEADD(MINUTE, DATEDIFF(MINUTE, '20000101', CURRENT_TIMESTAMP), '20000101')
SET@ThisDay = DATEADD(DAY, DATEDIFF(DAY, '20000101', CURRENT_TIMESTAMP), '20000101')
SELECT
row_number() over (order by MAX(HeadlineDate)) as Number,
COUNT(ArticleID) AS [Count],
MIN(DATEADD(dd, - (DAY(HeadlineDate) - 1), HeadlineDate)) AS HeadlineDate
FROM
dbo.ZMArticle
WHERE
PortalID=0
GROUP BY
MONTH(HeadlineDate), Year(HeadlineDate)
ORDER BY
MAX(HeadlineDate) desC
Which works fine, but when I do:
DECLARE
@ThisMinute DateTime,
@ThisDay DateTime
SET@ThisMinute = DATEADD(MINUTE, DATEDIFF(MINUTE, '20000101', CURRENT_TIMESTAMP), '20000101')
SET@ThisDay = DATEADD(DAY, DATEDIFF(DAY, '20000101', CURRENT_TIMESTAMP), '20000101')
SELECT
row_number() over (order by MAX(HeadlineDate)) as Number,
COUNT(ArticleID) AS [Count],
MIN(DATEADD(dd, - (DAY(HeadlineDate) - 1), HeadlineDate)) AS HeadlineDate
FROM
dbo.ZMArticle
WHERE
PortalID=0
AND
Expiredate <> Null
GROUP BY
MONTH(HeadlineDate), Year(HeadlineDate)
ORDER BY
MAX(HeadlineDate) desC
it doesn't return anything....
How can I change that
The secret to creativity is knowing how to hide your sources. (Einstein)
View 3 Replies
View Related
Jul 20, 2005
Hi,I have a corporate database with about 60 different tables that spansmanufacturing, accounting, marketing, etc.It is possible, but unwieldy, to establish a relationship for eachtable in the entire database through critical fields like customer_idor product_id.But should I do that?My question is: Is there such a thing as too many relationships? CanI establish referential integrity via relationships with criticaltables like Accounting, but leave the rest unconnected and simply useJOINS in my business code?Thanks,HC
View 4 Replies
View Related
Mar 25, 2008
Hi everyone
My stored procedure accepts 2 parameters: names, number of names like this: (name1, name2, name3,3) Problem is: the number of names changes each time i want to run it. Some time it is: Name1,1 and other time it is: name1, name2,2. So every time i run it i need to change code. I thought "arrays" might solve that problem but i never met arrays in sql2005. Does it exist ? Case answer is "no": What is it to replace arrays and solve the above problem?
Thanks
View 5 Replies
View Related
Jan 8, 2007
Hey all,I have a datagrid with populated by this query: SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE (TABLE_TYPE = 'BASE TABLE')I have paging, sorting and selection enabled.Now I am looking for a way to use a wild card as a placeholder for the table name in my select statements so I can use the valued selected from the datagrid.Example : SELECT * FROM %TABLENAME%TIAWOOHOO! my first post.
View 2 Replies
View Related
May 23, 2008
i got this stored procedure.
i tried to modify it and now its giving me this scaler error.
Msg 137, Level 15, State 2, Procedure insertuser, Line 4
Must declare the scalar variable "@seller_id".
USE [DBCars]
GO
/****** Object: StoredProcedure [dbo].[insertuser] Script Date: 05/23/2008 20:44:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[insertuser]
(@Make nchar(10),@Model nchar(10),@City nchar(10),@SellerID varchar(50),@MileAge nchar(10),@Year_Model int)
as
insert into tbcar values(@make,@model,@city,@seller_id,@mileage,@year_model);
View 4 Replies
View Related
Jun 1, 2006
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.
View 1 Replies
View Related
Nov 27, 2001
I have this field in a table.
NAME
--------------------
Johnson, Bill P.
I need to cut it up into this in another table:
LAST_NAME FIRST_NAME MID_INIT
-----------------------------------------------------
Johnson Bill P
I can't use SUBSTRING command because the length of this field will change with every row.
Is there a way I can tell it to truncate past or prior to the comma or spaces?
Thanks!
Lance
View 2 Replies
View Related
Dec 30, 1999
I want to pass my stored proc a list, and either loop through it as a list or (better) turn it into an array and loop through it that way to insert it. Psuedocode would be...
Loop from 0 to ListLength
begin
INSERT Transaction_Data
end
Thanks from an SQLS7 newbie,
jE
View 4 Replies
View Related
Apr 30, 2007
Hello all,
I'm working on an ASP.NET with a SQL server for database. Some of the tables, for example, contain information such as different types of Fabrics (silk, cotton, etc..) . I'd like to have this table localizable (English and French for instance). Is this possible ? Is there an equivalent of resource files in SQL server ?
Or do I have to do this manually ? (have 2 separate fields in the table for those 2 locales)
View 1 Replies
View Related
Feb 21, 2008
I'm newer to MS SQL, but have a programming background, so I'm going to try to describe what I'm doing in that sense.
What I want to do is pull an entire column of data from one table, and insert it into a column in another table. Typically, I would do this with an array and while loop or something similar. I've figured out how to do a while loop in SQL, but the array situation has me stumped. I tried reading http://weblogs.sqlteam.com/jeffs/archive/2007/06/26/60240.aspx that article, however it referred to a procedure, and I have no background or experience with procedures.
Any sort of idea or document on a technique to do this would be most appreciated, thank you!
View 15 Replies
View Related
Mar 4, 2007
I am glad to announce that there is now a version of my article "Arrays andLists in SQL Server" for SQL 2005 available on my web site.THe URL for the article ishttp://www.sommarskog.se/arrays-in-sql-2005.html. If you are curious aboutthe performance numbers they are in an appendix athttp://www.sommarskog.se/arrays-in-sql-perftest.html.The old version of the aritlce remains, as the new article covers SQL 2005only. The old version is now athttp://www.sommarskog.se/arrays-in-sql-2000.html.The old URL, http://www.sommarskog.se/arrays-in-sql.html leads to a pagethat links to both articles.And the reason that there are two articles is simply that SQL 2005 addsso many new features: nvarchar(MAX), the CLR, the xml data type, CTE thatall can be used in the realm of arrays and lists.--Erland Sommarskog, SQL Server MVP, Join Bytes!Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/pr...oads/books.mspxBooks Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodin...ions/books.mspx
View 1 Replies
View Related
Apr 25, 2007
This should be so easy.
All I want to do is have a cumalative counter that counts from 1 to whatever.
any ideas how i can do this?
View 6 Replies
View Related
Sep 24, 2007
I have a DTS Package that I am running from a command line via .bat file. Does anyone know if there is a command to have the command window minimized or running in the background? I used the /Rep N command but that still leaves the window open until the package has executed.
Thanks!
View 3 Replies
View Related
Aug 3, 2007
Hi ALl,
When I open the VarDecimal option in our SAP R3P system, I found the space used is increased.
Before turn on the option:
Total space:528315.31MB
Fee Sapce:0.00MB
After trun on the option:
Total Space:733815.31MB
Free Space:119006.31MB
it is very interestion, anyone has this experience?
thanks.
View 4 Replies
View Related
Apr 13, 2006
in MySql:
SELECT * FROM table LIMIT 5,10
How to do this in MicroSoftSql?
View 9 Replies
View Related
Feb 19, 2008
Hi,
I'm using Visual Studio and Visual Basic to save data about members in a database.
I use SQL-database as it's included.
However, I'm new to this saving in the datbase and wonder how I can at the best save data about a member without having to index each value in the arrays:
The array has 2 index and is declared
dim Results(6, 50) as string
Of course, I can do results11, results12,... results150, results21, ...results250....
But it seems rather awkward to do it this way. I'm sure that there is a better way.
Thanks, any assistance is appriciated!
Best wishes, Per
View 10 Replies
View Related
Jan 12, 2007
My sql-string looks like this:
SelectCommand="SELECT * FROM Table1 WHERE Field1 IN @target"
And my parameter looks like this:
<asp:ControlParameter Name="target" ControlID="CheckBoxList1" PropertyName="SelectedValue" />
This code gives me a syntax error near @target. Someone got a solution?
View 2 Replies
View Related
Jul 2, 2007
In a previous post, someone helped me with creating stored procedures, and I am grateful because I am transitioning from the Access World.
Anyway, I get an error at .ExecuteNonQuery in visual studio 2005 when I run the following code:
Dim strsql As String Dim strconn As String strsql = "sp_Roster" strconn = "server=xxxx; user=xxxx; pwd=xxxx; database=xxxx;" With comm .Connection = New SqlConnection(strconn) .CommandText = strsql .CommandType = CommandType.StoredProcedure With .Parameters.Add("TeacherID", SqlDbType.Char) .Value = "DawsMark@aol.com" End With With .Parameters.Add("ClassID", SqlDbType.Int) .Value = classid End With With .Parameters.Add("sID", SqlDbType.Int) .Value = ssID End With With .Parameters.Add("sLastName", SqlDbType.Char) .Value = lastname End With With .Parameters.Add("sFirstName", SqlDbType.Char) .Value = firstname End With With .Parameters.Add("sMiddleName", SqlDbType.Char) .Value = middlename End With With .Parameters.Add("Student", SqlDbType.Char) .Value = fullname End With With .Parameters.Add("Password", SqlDbType.Char) .Value = password End With .Connection.Open() .ExecuteNonQuery() With comm.Connection If .State = ConnectionState.Open Then .Close() End If End With End With
The error was: Error converting data type char to int.
The stored procedure in sql server was as follows
CREATE PROCEDURE sp_Roster -- Add the parameters for the stored procedure here@TeacherID varchar(50),@ClassID int,@sID int,@sLastName varchar(50),@sFirstName varchar(50),@sMiddleName varchar(50),@Student varchar(50),@Password varchar(50)ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here INSERT INTO Roster (TeacherID, ClassID, sID, sLastName, sFirstName, sMiddleName, Student, Password) VALUES (@TeacherID, @ClassID, @sID, @sLastName, @sFirstName, @sMiddleName, @Student, @Password)ENDGO
The error also says "sqlexception was unhandled by user code.
This is strange because this code worked perfectly when connecting to Access and when I used oledb.
So how is it the code's problem? Is the stored procedure causing the error or the code. Can someone please help. thanks.
<Edited by Dinakar Nethi>
Please mask your useird/pwd info in the connection string when posting to a public forum like this
</Edit>
View 2 Replies
View Related
Feb 23, 2004
Dear all,
i want to know how i can pass multiple values in the form of arrays to a stored procedures.
the technique by which i pass multiple values to a stored procedure beginning along with declarations are as follows:
Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader()
Me.cmdInsSlabHmst = New System.Data.OleDb.OleDbCommand()
Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection()
Me.cmdInsSlabDMst = New System.Data.OleDb.OleDbCommand()
'
'cmdInsSlabHmst
'
Me.cmdInsSlabHmst.CommandText = "PKGSLABHMST.INSSLABHMST"
Me.cmdInsSlabHmst.CommandType = System.Data.CommandType.StoredProcedure
Me.cmdInsSlabHmst.Connection = Me.OleDbConnection1
Me.cmdInsSlabHmst.Parameters.Add(New System.Data.OleDb.OleDbParameter("iSLABDESC", System.Data.OleDb.OleDbType.VarChar, 50))
Me.cmdInsSlabHmst.Parameters.Add(New System.Data.OleDb.OleDbParameter("iSLABUNIT", System.Data.OleDb.OleDbType.VarChar, 1))
Me.cmdInsSlabHmst.Parameters.Add(New System.Data.OleDb.OleDbParameter("iREMARKS", System.Data.OleDb.OleDbType.VarChar))
Me.cmdInsSlabHmst.Parameters.Add(New System.Data.OleDb.OleDbParameter("iSLABFROM", System.Data.OleDb.OleDbType.VarChar))
Me.cmdInsSlabHmst.Parameters.Add(New System.Data.OleDb.OleDbParameter("iSLABRATE", System.Data.OleDb.OleDbType.VarChar))
Me.cmdInsSlabHmst.Parameters.Add(New System.Data.OleDb.OleDbParameter("iNOOFRECORDS", System.Data.OleDb.OleDbType.Integer))
'
'OleDbConnection1
'
Me.OleDbConnection1.ConnectionString = CType(configurationAppSettings.GetValue("ConnectionString", GetType(System.String)), String)
'Passing multiple values to the procedure with the help of ~ sign
Dim strCode As String
Dim i As Integer
'Dim dblSlabRate As Decimal
'Dim dblSlabFrom As Decimal
Dim strSlabRate As String
Dim strSlabFrom As String
Dim strSlabRateP As String
Dim strSlabFromP As String
Dim intCntr As Integer
'Me.cmdInsSlabHmst.Parameters("iSLABDESC").Value = txtSlabDesc.Text
'Me.cmdInsSlabHmst.Parameters("iSLABUNIT").Value = ddlSalbUnit.SelectedItem.Value
'Me.cmdInsSlabHmst.Parameters("iREMARKS").Value = txtRemarks.Text
'OleDbConnection1.Open()
'strCode = cmdInsSlabHmst.ExecuteScalar
'OleDbConnection1.Close()
For i = 0 To dgSlabDtl.Items.Count - 1
If i = dgSlabDtl.Items.Count - 1 Then
'dblSlabRate = CType(dgSlabDtl.Items(i).FindControl("txtSlabRate"), TextBox).Text
'dblSlabFrom = CType(dgSlabDtl.Items(i).FindControl("txtSlabFrom"), TextBox).Text
strSlabRate = CType(dgSlabDtl.Items(i).FindControl("txtSlabRate"), TextBox).Text
strSlabFrom = CType(dgSlabDtl.Items(i).FindControl("txtSlabFrom"), TextBox).Text
Else
'dblSlabRate = CType(dgSlabDtl.Items(i).FindControl("lblSlabRate"), Label).Text
'dblSlabFrom = CType(dgSlabDtl.Items(i).FindControl("lblSlabFrom"), Label).Text
strSlabRate = CType(dgSlabDtl.Items(i).FindControl("lblSlabRate"), Label).Text
strSlabFrom = CType(dgSlabDtl.Items(i).FindControl("lblSlabFrom"), Label).Text
End If
strSlabRateP += strSlabRate & "~"
strSlabFromP += strSlabFrom & "~"
intCntr += 1
'If dblSlabRate <> "" And dblSlabFrom <> "" Then
'InsDtl(strCode, dblSlabFrom, dblSlabRate)
'End If
Next
If strSlabRateP <> "" And strSlabFrom <> "" Then
With cmdInsSlabHmst
.Parameters("iSLABDESC").Value = UCase(txtSlabDesc.Text)
.Parameters("iSLABUNIT").Value = ddlSalbUnit.SelectedItem.Value
.Parameters("iREMARKS").Value = txtRemarks.Text
.Parameters("iSLABFROM").Value = strSlabFromP
.Parameters("iSLABRATE").Value = strSlabRateP
.Parameters("iNOOFRECORDS").Value = intCntr
End With
OleDbConnection1.Open()
cmdInsSlabHmst.ExecuteNonQuery()
OleDbConnection1.Close()
End If
to the insert procedure i am passing multiple values with the help of ~ sign and in the procedure the individual values are separated by identifying the position of ~ sign and the no. of records which have been passed. For which a complicated stored procedure has been written.
i want to pass multiple values in an array, so that my stored procedure becomes simple and runs faster. So, if someone tells me how to pass arrays to a stored procedure (with code example), it will be of real help.
regards
subhajit
View 1 Replies
View Related
Jul 4, 2005
I would like to write a fun or stored procedure to do some operation. It require me to know that what category is currently belong to certain people(people_table: category_table1 to Many)However, when i use the select statement in stored proc, it return a set of result, not a scalar , therefore, i cannot use the variable to hold it. In addition, there are no array in SQL server.Question:1. Is there any way to hold the collection of result(like array)?2. Also, how to determine to use fun or stored procedure?(Since a integer is need to return by them)Thx
View 4 Replies
View Related
Sep 15, 2005
Every morning our sql server runs very slowly which means our log on
page times out on a simple query. If we stop and start the sql server
everything runs fine for the rest of the day until the follwing
morning. the server is not used out of business hours excpet for a few
very small and simple jobs to delete records, These all run to
completion. Any help with this would be much aprreciated! Thanks.
Rob
View 2 Replies
View Related
Feb 20, 2003
Someone recently tried to tell me that it is possible to pass an array to a stored procedure.
I have tried creating procedures with the 'table' datatype for the parameters but the attempts fail with a syntax error.
We currently workaround this limitation by passing the equivalent of an array to temporary tables and selecting from those tables as needed within our stored procedure but if we can circumvent this by passing an array, we'd definetly like to try that instead.
Thanks in advance for any advice you may share.
View 4 Replies
View Related