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
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.
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
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?
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?
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
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?
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
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)
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
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
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.
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);
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.
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
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)
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!
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
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.
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
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?
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>
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.
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
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
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.