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

Wednesday, March 21, 2012

Passing arrays to stored procedures

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
subhajitWell, what you are doing is probably the way to go. I don't understand though, how you can say a 'complicated stored procedure' was written to chop up a ~ delimited list. It should be only a couple of very simple statements to do.

I do the same thing quite often, for example if I have a checkboxlist and I want to pass a list of all items that the user checked to a stored proc, I go through the list in my ASP code and create a string in much the same way you do. Then in my stored proc I do something like:


while strpos(@.List, '~') > 0
begin
insert into #Table values (substring(@.List, 1, strpos(@.List, '~') - 1))
set @.List = substring(@.List, strpos(@.List, '~') + 1, length(@.List))
end

which is easy enough for me, and executes in a flash, even for hundreds of items.

If you reach the limit of this code (@.List close to 8000 chars) you need to do something else. In this case it can be useful to consider your specific application. Where are those values coming from? Are you storing it in bits and pieces to the array, and then in one shot you want to write it to your DB? If so you should think about writing the values to your DB as they become available - instead of keeping them in an array in server memory.

But from your code it looks like you're just doing the same sort of thing I do, so I don't see the problem.

BTW, your code is very difficult to look at, since 1/2 of it is commented out. If you can distill it into just what we need to see your problem, it makes it easier for us to help you. It will also increase the likelyhood of an accurate answer.sql

Passing arrays to SP

I am implementing a multi-variable search SP to which I need pass single
values and arrays as arguments, e.g.
exec mySP 'abc', {3,5,8}, 'en-US', {4,5}, ... etc
How to pass the args to mySP and how to retrieve the values from the arrays
inside the SP?
TIASee if these examples help:
http://vyaskn.tripod.com/passing_ar..._procedures.htm
--
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"alto" <altodorov@.hotmail.com> wrote in message
news:OonW%237G7FHA.2676@.TK2MSFTNGP15.phx.gbl...
> I am implementing a multi-variable search SP to which I need pass single
> values and arrays as arguments, e.g.
> exec mySP 'abc', {3,5,8}, 'en-US', {4,5}, ... etc
> How to pass the args to mySP and how to retrieve the values from the
arrays
> inside the SP?
> TIA
>

Passing arrays as parameter (SqlDataSource)

My sql-string looks like this:
 SelectCommand="SELECT *FROM Table1WHERE Field1IN @.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?

I wish I new how to do it with a Parameter, but I'm running into the same problem as you. However, here's an example on how to do it by simple modifying the CommandText during the SqlDataSource.Selecting event:

ASPX

<asp:checkboxlist id="lstProducts" runat="server" datasourceid="sdsProducts" datatextfield="ProductName"datavaluefield="ProductID" autopostback="True" onselectedindexchanged="lstProducts_SelectedIndexChanged"></asp:checkboxlist><asp:sqldatasource id="sdsProducts" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString%>"selectcommand="SELECT TOP 10 ProductID, ProductName FROM Products"></asp:sqldatasource><br /><asp:gridview id="gvProducts" runat="server" datakeynames="ProductID" datasourceid="sdsProducts2"></asp:gridview><asp:sqldatasource id="sdsProducts2" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString%>"onselecting="sdsProducts2_Selecting" selectcommand="SELECT * FROM Products"></asp:sqldatasource>

CODE-BEHIND

protected void sdsProducts2_Selecting(object sender, SqlDataSourceSelectingEventArgs e){if (this.IsPostBack){e.Command.CommandText = String.Format("SELECT * FROM Products WHERE ProductID IN ({0})",this.GetInExpression(lstProducts));}else{e.Cancel =true;}}private string GetInExpression(ListControl control){List<string> list =new List<string>();foreach (ListItem itemin control.Items){if (item.Selected){list.Add(item.Value);}}return String.Join(", ", list.ToArray());}protected void lstProducts_SelectedIndexChanged(object sender, EventArgs e){gvProducts.DataBind();}

|||There must be a more simple way to do this?

Monday, February 20, 2012

Partitions as SQL Server Resources in Clustering Services

I've got 2 Arrays on my SAN.
Array 1 is 66 gb.
Array 2 is 230 gb.
I've partitioned the drives in MS2003 this way:
Array 1, Drive Q: (Quarum) 2gb
Array 1, Drive S: 64gb
Array 2, Drive R: 230gb.
I installed SQL Server after setting up MSCS.
Everything went fine.
Now I want to add the Drive S: as a resource in MSCS, so I can store
my transaction logs on it.
MSCS seems to look for a Physical Disk.
Is this possible?
Oh its possible all right. MSCS has to have a Physical disk, as you have
noticed. You have two to the OS> 1 - 66 GB, 1 230 GB.
Cheers,
Rod
"Travis" <twillard@.generasystems.com> wrote in message
news:bc4cf00.0406161203.1b035e4a@.posting.google.co m...
> I've got 2 Arrays on my SAN.
> Array 1 is 66 gb.
> Array 2 is 230 gb.
> I've partitioned the drives in MS2003 this way:
> Array 1, Drive Q: (Quarum) 2gb
> Array 1, Drive S: 64gb
> Array 2, Drive R: 230gb.
> I installed SQL Server after setting up MSCS.
> Everything went fine.
> Now I want to add the Drive S: as a resource in MSCS, so I can store
> my transaction logs on it.
> MSCS seems to look for a Physical Disk.
> Is this possible?
|||Sorry, I'm probably not being clear.
Cluster Group has 1 physical disk, Q:
SQL Server has 1 physcial disk, R:
I've broken the cluster's physical disk into two partitions: Q and S.
How can I get my SQL Sever group to see the S partition as a resource,
so i can store my logs there?
When I try to add it as a physical drive, it of course doesn't show up.
Is there another resource type that allows me to see the partition?
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
|||Ok, so here is my trouble.
When i go into add a resource as a phyical dive, no drive letter shows
up.
How do I add the S: drive so that the SQL Cluster can see it?
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
|||You have all 3 drive letters already in the cluster. Double click on the Q
drive and look at parameters, notice it says Q & S. That is because
clustering goes by the physical disk and not partitions.
You add another array or have your logs and quorum on the same physical
disk, but different partitions of it.
Cheers,
Rod
"Travis Willard" <twillard@.generasystems.com> wrote in message
news:e2%23Tjs%23UEHA.1656@.TK2MSFTNGP09.phx.gbl...
> Sorry, I'm probably not being clear.
> Cluster Group has 1 physical disk, Q:
> SQL Server has 1 physcial disk, R:
> I've broken the cluster's physical disk into two partitions: Q and S.
> How can I get my SQL Sever group to see the S partition as a resource,
> so i can store my logs there?
> When I try to add it as a physical drive, it of course doesn't show up.
> Is there another resource type that allows me to see the partition?
> *** Sent via Devdex http://www.devdex.com ***
> Don't just participate in USENET...get rewarded for it!
|||See my other reply
Cheers,
Rod
"Travis Willard" <twillard@.generasystems.com> wrote in message
news:%23dkIms%23UEHA.1656@.TK2MSFTNGP09.phx.gbl...
> Ok, so here is my trouble.
> When i go into add a resource as a phyical dive, no drive letter shows
> up.
> How do I add the S: drive so that the SQL Cluster can see it?
>
> *** Sent via Devdex http://www.devdex.com ***
> Don't just participate in USENET...get rewarded for it
|||Hi Travis,
It depends on how exactly the drives have been configured in Array 1. If Q:
and S: are file system partitions on the same 'disk' (LUN in the array) then
you will not be able to add a new Physical Disk resource. If they are single
partitions each on their own 'disk' (LUN in the array) then you will be able
to if you first set the disk up for the o/s, basically by creating and
formatting the 64 GB NTFS partition (if you don't use NTFS the disk won't
appear in the GUI when you try to add the resource).
The Cluster Administrator GUI to add a physical disk resource queries the
Clusdisk key in HKLM\System\CCS\Services and looks for disk signatures (4
byte ID numbers generated by the o/s). These signatures are created and
written to the disk's master boot record (defined as first sector on the
disk, before the file system partitions begin, but remember in an array this
all virtualised) when you first use the Disk Management snap-in on a disk in
Computer Management.
Upshot of this is after you have configured the LUNs in the array you have
to configure the disk in Disk Management for it to be visible to the
clusdisk driver and therefore appear in the GUI to add a Physical Disk
resource.
Hope this helps.
"Travis" <twillard@.generasystems.com> wrote in message
news:bc4cf00.0406161203.1b035e4a@.posting.google.co m...
> I've got 2 Arrays on my SAN.
> Array 1 is 66 gb.
> Array 2 is 230 gb.
> I've partitioned the drives in MS2003 this way:
> Array 1, Drive Q: (Quarum) 2gb
> Array 1, Drive S: 64gb
> Array 2, Drive R: 230gb.
> I installed SQL Server after setting up MSCS.
> Everything went fine.
> Now I want to add the Drive S: as a resource in MSCS, so I can store
> my transaction logs on it.
> MSCS seems to look for a Physical Disk.
> Is this possible?
|||Thanks Peter,
I was able to solve the problem with a little trail and error. I added a
new Array and then created a new partition and was able to add it as a
physical disk resource. Great! SQL Server would still not see it! So
finally after beating my head on the wall, I figured out that you need
to add the physical disk as a dependency of the SQL Server resource.
Problem solved!
I've not got an array that is raid 1, for my quorum and some space for
backing up. 1 array for my data (raid 5) and another for my logs (raid
5).
Thanks for responding!
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!