PC Review


Reply
Thread Tools Rate Thread

Calling a SQL stored procedure

 
 
=?Utf-8?B?RGF2aWQgRGF2aXM=?=
Guest
Posts: n/a
 
      17th Nov 2004
I have the following stored procedure in an sql database

------------------------------------
CREATE PROCEDURE zspQuoteSummary
@dStart DateTime,
@dEnd DateTime
AS

SELECT qtmast.fstatus,
qtmast.festimator,
qtmast.fcompany,
qtmast.fquoteno AS Quote,
qtmast.frevno AS Rev,
qtmast.fquotedate,
qtitem.fenumber AS ItemNumber,
qtitem.fpartno AS Part,
qtitem.fpartrev AS PartRev,
qtpest.fmatlcost + qtpest.ftoolcost + qtpest.flabcost + qtpest.fovhdcost +
qtpest.fsubcost + qtpest.fothrcost + qtpest.fsetupcost AS TotalCost,
qtitem.funetprice AS UnitPrice,
qtitem.festqty AS Qty,
qtitem.funetprice*qtitem.festqty AS Extended,

GM =
CASE
WHEN (qtitem.funetprice*qtitem.festqty) <> 0 THEN
((qtitem.funetprice*qtitem.festqty) - (qtpest.fmatlcost + qtpest.ftoolcost +
qtpest.flabcost + qtpest.fovhdcost + qtpest.fsubcost + qtpest.fothrcost +
qtpest.fsetupcost) )/(qtitem.funetprice*qtitem.festqty)
ELSE
0
END,

EAU =
CASE
WHEN SUBSTRING(qtitem.fdescmemo,1,4) = UPPER('EAU:') THEN
SUBSTRING(qtitem.fdescmemo,5,10)
ELSE
0
END,

EAUExtended =
CASE
WHEN SUBSTRING(qtitem.fdescmemo,1,4) = UPPER('EAU:') THEN
SUBSTRING(qtitem.fdescmemo,5,10)* qtitem.funetprice
ELSE
0
END,

EAUGM =
CASE
WHEN SUBSTRING(qtitem.fdescmemo,1,4) = UPPER('EAU:') THEN
CASE
WHEN SUBSTRING(qtitem.fdescmemo,5,10)* qtitem.funetprice <> 0 THEN
((SUBSTRING(qtitem.fdescmemo,5,10)* qtitem.funetprice) - (qtpest.fmatlcost +
qtpest.ftoolcost + qtpest.flabcost + qtpest.fovhdcost + qtpest.fsubcost +
qtpest.fothrcost + qtpest.fsetupcost)) / (SUBSTRING(qtitem.fdescmemo,5,10)*
qtitem.funetprice)
ELSE
0
END
ELSE
0
END,

'For ' + @dStart + ' thru ' + @dEnd AS Parameters
INTO ztmpQuoteSummary
FROM qtmast INNER JOIN (qtitem INNER JOIN qtpest ON
(qtitem.fpartrev=qtpest.fcpartrev) AND (qtitem.fpartno=qtpest.fpartno) AND
(qtitem.finumber=qtpest.finumber) AND (qtitem.fenumber=qtpest.fenumber) AND
(qtitem.fquoteno=qtpest.fquoteno)) ON qtmast.fquoteno=qtitem.fquoteno
WHERE (((qtmast.fquotedate)>= @dStart And (qtmast.fquotedate)<=@dEnd))
ORDER BY qtmast.fcompany, qtmast.fquoteno, qtmast.frevno;
GO
------------------------------------

how do I call this proc from within VB
--
Thanks

David Davis
 
Reply With Quote
 
 
 
 
Bernie Yaeger
Guest
Posts: n/a
 
      17th Nov 2004
Hi David,

This should give you the general idea of how to use a command object to
build the parameters and then to execute the stored proc. ocmd2 is the
command object you'll be interested in.

HTH,

Bernie Yaeger
Dim oconn As New SqlConnection("data source=d5z0071;initial
catalog=imc;integrated security=sspi;")

Dim ocmd As New SqlCommand("select * from histd_", oconn)

Try

oconn.Open()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

Dim ocmd2 As New SqlCommand

ocmd2 = New SqlCommand("sp_testupdatehistd_", oconn)

Dim oda As New SqlDataAdapter(ocmd2)

Dim pimcacct, pbipad, ****uecode As SqlParameter

ocmd2.CommandType = CommandType.StoredProcedure

pimcacct = ocmd2.Parameters.Add("@mimcacct", SqlDbType.Char, 21)

pbipad = ocmd2.Parameters.Add("@mbipad", SqlDbType.Char, 6)

****uecode = ocmd2.Parameters.Add("@missuecode", SqlDbType.Char, 15)

pimcacct.Direction = ParameterDirection.Input

pbipad.Direction = ParameterDirection.Input

****uecode.Direction = ParameterDirection.Input

pimcacct.Value = "12778-000001"

pbipad.Value = "92789"

****uecode.Value = "200406"

Try

ocmd2.ExecuteNonQuery()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

oconn.Close()

"David Davis" <(E-Mail Removed)> wrote in message
news:64A2E85A-22E0-4AF9-9F0A-(E-Mail Removed)...
>I have the following stored procedure in an sql database
>
> ------------------------------------
> CREATE PROCEDURE zspQuoteSummary
> @dStart DateTime,
> @dEnd DateTime
> AS
>
> SELECT qtmast.fstatus,
> qtmast.festimator,
> qtmast.fcompany,
> qtmast.fquoteno AS Quote,
> qtmast.frevno AS Rev,
> qtmast.fquotedate,
> qtitem.fenumber AS ItemNumber,
> qtitem.fpartno AS Part,
> qtitem.fpartrev AS PartRev,
> qtpest.fmatlcost + qtpest.ftoolcost + qtpest.flabcost + qtpest.fovhdcost +
> qtpest.fsubcost + qtpest.fothrcost + qtpest.fsetupcost AS TotalCost,
> qtitem.funetprice AS UnitPrice,
> qtitem.festqty AS Qty,
> qtitem.funetprice*qtitem.festqty AS Extended,
>
> GM =
> CASE
> WHEN (qtitem.funetprice*qtitem.festqty) <> 0 THEN
> ((qtitem.funetprice*qtitem.festqty) - (qtpest.fmatlcost + qtpest.ftoolcost
> +
> qtpest.flabcost + qtpest.fovhdcost + qtpest.fsubcost + qtpest.fothrcost +
> qtpest.fsetupcost) )/(qtitem.funetprice*qtitem.festqty)
> ELSE
> 0
> END,
>
> EAU =
> CASE
> WHEN SUBSTRING(qtitem.fdescmemo,1,4) = UPPER('EAU:') THEN
> SUBSTRING(qtitem.fdescmemo,5,10)
> ELSE
> 0
> END,
>
> EAUExtended =
> CASE
> WHEN SUBSTRING(qtitem.fdescmemo,1,4) = UPPER('EAU:') THEN
> SUBSTRING(qtitem.fdescmemo,5,10)* qtitem.funetprice
> ELSE
> 0
> END,
>
> EAUGM =
> CASE
> WHEN SUBSTRING(qtitem.fdescmemo,1,4) = UPPER('EAU:') THEN
> CASE
> WHEN SUBSTRING(qtitem.fdescmemo,5,10)* qtitem.funetprice <> 0 THEN
> ((SUBSTRING(qtitem.fdescmemo,5,10)* qtitem.funetprice) - (qtpest.fmatlcost
> +
> qtpest.ftoolcost + qtpest.flabcost + qtpest.fovhdcost + qtpest.fsubcost +
> qtpest.fothrcost + qtpest.fsetupcost)) /
> (SUBSTRING(qtitem.fdescmemo,5,10)*
> qtitem.funetprice)
> ELSE
> 0
> END
> ELSE
> 0
> END,
>
> 'For ' + @dStart + ' thru ' + @dEnd AS Parameters
> INTO ztmpQuoteSummary
> FROM qtmast INNER JOIN (qtitem INNER JOIN qtpest ON
> (qtitem.fpartrev=qtpest.fcpartrev) AND (qtitem.fpartno=qtpest.fpartno) AND
> (qtitem.finumber=qtpest.finumber) AND (qtitem.fenumber=qtpest.fenumber)
> AND
> (qtitem.fquoteno=qtpest.fquoteno)) ON qtmast.fquoteno=qtitem.fquoteno
> WHERE (((qtmast.fquotedate)>= @dStart And (qtmast.fquotedate)<=@dEnd))
> ORDER BY qtmast.fcompany, qtmast.fquoteno, qtmast.frevno;
> GO
> ------------------------------------
>
> how do I call this proc from within VB
> --
> Thanks
>
> David Davis



 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgRGF2aXM=?=
Guest
Posts: n/a
 
      18th Nov 2004
Bernie

Thanks for the code.

I'm able to assign values to the parameters. The only problem I have now is
that I get a syntax error

"Syntax error converting datetime from character string."

How do I pass a datetime value to a datetime parameter. Following is the
code for the parameters.

Dim dStart, dEnd As SqlParameter
dStart = SprocCommand.Parameters.Add("@dStart", SqlDbType.DateTime, 8)
dEnd = SprocCommand.Parameters.Add("@dEnd", SqlDbType.DateTime, 8)
dStart.Direction = ParameterDirection.Input
dEnd.Direction = ParameterDirection.Input
dStart.Value = Me.dteStart.DateTime.ToShortDateString
dEnd.Value = Me.dteEnd.DateTime.ToShortDateString

Thanks

David Davis

"Bernie Yaeger" wrote:

> Hi David,
>
> This should give you the general idea of how to use a command object to
> build the parameters and then to execute the stored proc. ocmd2 is the
> command object you'll be interested in.
>
> HTH,
>
> Bernie Yaeger
> Dim oconn As New SqlConnection("data source=d5z0071;initial
> catalog=imc;integrated security=sspi;")
>
> Dim ocmd As New SqlCommand("select * from histd_", oconn)
>
> Try
>
> oconn.Open()
>
> Catch ex As Exception
>
> MessageBox.Show(ex.Message)
>
> End Try
>
> Dim ocmd2 As New SqlCommand
>
> ocmd2 = New SqlCommand("sp_testupdatehistd_", oconn)
>
> Dim oda As New SqlDataAdapter(ocmd2)
>
> Dim pimcacct, pbipad, ****uecode As SqlParameter
>
> ocmd2.CommandType = CommandType.StoredProcedure
>
> pimcacct = ocmd2.Parameters.Add("@mimcacct", SqlDbType.Char, 21)
>
> pbipad = ocmd2.Parameters.Add("@mbipad", SqlDbType.Char, 6)
>
> ****uecode = ocmd2.Parameters.Add("@missuecode", SqlDbType.Char, 15)
>
> pimcacct.Direction = ParameterDirection.Input
>
> pbipad.Direction = ParameterDirection.Input
>
> ****uecode.Direction = ParameterDirection.Input
>
> pimcacct.Value = "12778-000001"
>
> pbipad.Value = "92789"
>
> ****uecode.Value = "200406"
>
> Try
>
> ocmd2.ExecuteNonQuery()
>
> Catch ex As Exception
>
> MessageBox.Show(ex.Message)
>
> End Try
>
> oconn.Close()
>
> "David Davis" <(E-Mail Removed)> wrote in message
> news:64A2E85A-22E0-4AF9-9F0A-(E-Mail Removed)...
> >I have the following stored procedure in an sql database
> >
> > ------------------------------------
> > CREATE PROCEDURE zspQuoteSummary
> > @dStart DateTime,
> > @dEnd DateTime
> > AS
> >
> > SELECT qtmast.fstatus,
> > qtmast.festimator,
> > qtmast.fcompany,
> > qtmast.fquoteno AS Quote,
> > qtmast.frevno AS Rev,
> > qtmast.fquotedate,
> > qtitem.fenumber AS ItemNumber,
> > qtitem.fpartno AS Part,
> > qtitem.fpartrev AS PartRev,
> > qtpest.fmatlcost + qtpest.ftoolcost + qtpest.flabcost + qtpest.fovhdcost +
> > qtpest.fsubcost + qtpest.fothrcost + qtpest.fsetupcost AS TotalCost,
> > qtitem.funetprice AS UnitPrice,
> > qtitem.festqty AS Qty,
> > qtitem.funetprice*qtitem.festqty AS Extended,
> >
> > GM =
> > CASE
> > WHEN (qtitem.funetprice*qtitem.festqty) <> 0 THEN
> > ((qtitem.funetprice*qtitem.festqty) - (qtpest.fmatlcost + qtpest.ftoolcost
> > +
> > qtpest.flabcost + qtpest.fovhdcost + qtpest.fsubcost + qtpest.fothrcost +
> > qtpest.fsetupcost) )/(qtitem.funetprice*qtitem.festqty)
> > ELSE
> > 0
> > END,
> >
> > EAU =
> > CASE
> > WHEN SUBSTRING(qtitem.fdescmemo,1,4) = UPPER('EAU:') THEN
> > SUBSTRING(qtitem.fdescmemo,5,10)
> > ELSE
> > 0
> > END,
> >
> > EAUExtended =
> > CASE
> > WHEN SUBSTRING(qtitem.fdescmemo,1,4) = UPPER('EAU:') THEN
> > SUBSTRING(qtitem.fdescmemo,5,10)* qtitem.funetprice
> > ELSE
> > 0
> > END,
> >
> > EAUGM =
> > CASE
> > WHEN SUBSTRING(qtitem.fdescmemo,1,4) = UPPER('EAU:') THEN
> > CASE
> > WHEN SUBSTRING(qtitem.fdescmemo,5,10)* qtitem.funetprice <> 0 THEN
> > ((SUBSTRING(qtitem.fdescmemo,5,10)* qtitem.funetprice) - (qtpest.fmatlcost
> > +
> > qtpest.ftoolcost + qtpest.flabcost + qtpest.fovhdcost + qtpest.fsubcost +
> > qtpest.fothrcost + qtpest.fsetupcost)) /
> > (SUBSTRING(qtitem.fdescmemo,5,10)*
> > qtitem.funetprice)
> > ELSE
> > 0
> > END
> > ELSE
> > 0
> > END,
> >
> > 'For ' + @dStart + ' thru ' + @dEnd AS Parameters
> > INTO ztmpQuoteSummary
> > FROM qtmast INNER JOIN (qtitem INNER JOIN qtpest ON
> > (qtitem.fpartrev=qtpest.fcpartrev) AND (qtitem.fpartno=qtpest.fpartno) AND
> > (qtitem.finumber=qtpest.finumber) AND (qtitem.fenumber=qtpest.fenumber)
> > AND
> > (qtitem.fquoteno=qtpest.fquoteno)) ON qtmast.fquoteno=qtitem.fquoteno
> > WHERE (((qtmast.fquotedate)>= @dStart And (qtmast.fquotedate)<=@dEnd))
> > ORDER BY qtmast.fcompany, qtmast.fquoteno, qtmast.frevno;
> > GO
> > ------------------------------------
> >
> > how do I call this proc from within VB
> > --
> > Thanks
> >
> > David Davis

>
>
>

 
Reply With Quote
 
C-Services Holland b.v.
Guest
Posts: n/a
 
      19th Nov 2004
David Davis wrote:
> Bernie
>
> Thanks for the code.
>
> I'm able to assign values to the parameters. The only problem I have now is
> that I get a syntax error
>
> "Syntax error converting datetime from character string."
>
> How do I pass a datetime value to a datetime parameter. Following is the
> code for the parameters.
>
> Dim dStart, dEnd As SqlParameter
> dStart = SprocCommand.Parameters.Add("@dStart", SqlDbType.DateTime, 8)
> dEnd = SprocCommand.Parameters.Add("@dEnd", SqlDbType.DateTime, 8)
> dStart.Direction = ParameterDirection.Input
> dEnd.Direction = ParameterDirection.Input
> dStart.Value = Me.dteStart.DateTime.ToShortDateString
> dEnd.Value = Me.dteEnd.DateTime.ToShortDateString
>
> Thanks
>
> David Davis
>
> "Bernie Yaeger" wrote:
>


about this: dStart.Value = Me.dteStart.DateTime.ToShortDateString
Why do you convert it to shortdatestring?


--
Rinze van Huizen
C-Services Holland b.v.
 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgRGF2aXM=?=
Guest
Posts: n/a
 
      19th Nov 2004
I'm using the shortdatestring because even though the field in sql is
datetime they are only storing the first 8 positions (the date).

I found out that the problem was not with the assigning of the parameter to
pass but a header line:

'For ' + @dStart + ' thru ' + @dEnd AS hdrLine

I had to change the header line to look like this:

'For ' + Convert(VarChar, @dStart, 101) + ' thru ' + Convert(VarChar,@dEnd,
101) AS hdrLine

"C-Services Holland b.v." wrote:

> David Davis wrote:
> > Bernie
> >
> > Thanks for the code.
> >
> > I'm able to assign values to the parameters. The only problem I have now is
> > that I get a syntax error
> >
> > "Syntax error converting datetime from character string."
> >
> > How do I pass a datetime value to a datetime parameter. Following is the
> > code for the parameters.
> >
> > Dim dStart, dEnd As SqlParameter
> > dStart = SprocCommand.Parameters.Add("@dStart", SqlDbType.DateTime, 8)
> > dEnd = SprocCommand.Parameters.Add("@dEnd", SqlDbType.DateTime, 8)
> > dStart.Direction = ParameterDirection.Input
> > dEnd.Direction = ParameterDirection.Input
> > dStart.Value = Me.dteStart.DateTime.ToShortDateString
> > dEnd.Value = Me.dteEnd.DateTime.ToShortDateString
> >
> > Thanks
> >
> > David Davis
> >
> > "Bernie Yaeger" wrote:
> >

>
> about this: dStart.Value = Me.dteStart.DateTime.ToShortDateString
> Why do you convert it to shortdatestring?
>
>
> --
> Rinze van Huizen
> C-Services Holland b.v.
>

 
Reply With Quote
 
Bernie Yaeger
Guest
Posts: n/a
 
      19th Nov 2004
Hi David,

I think it's the value of the date variables that is the problem.

The following works fine for me:
Dim ocmd3 As New SqlCommand

ocmd3 = New SqlCommand("testdatevar", oconn)

Dim oda3 As New SqlDataAdapter(ocmd3)

Dim pdate As SqlParameter

ocmd3.CommandType = CommandType.StoredProcedure

pdate = ocmd3.Parameters.Add("@datevar", SqlDbType.DateTime, 8)

pdate.Direction = ParameterDirection.Input

pdate.Value = #1/1/2004#

Try

ocmd3.ExecuteNonQuery()

MessageBox.Show("fine")

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

HTH,

Bernie Yaeger



"David Davis" <(E-Mail Removed)> wrote in message
news:BE996262-3200-495F-87CC-(E-Mail Removed)...
> I'm using the shortdatestring because even though the field in sql is
> datetime they are only storing the first 8 positions (the date).
>
> I found out that the problem was not with the assigning of the parameter
> to
> pass but a header line:
>
> 'For ' + @dStart + ' thru ' + @dEnd AS hdrLine
>
> I had to change the header line to look like this:
>
> 'For ' + Convert(VarChar, @dStart, 101) + ' thru ' +
> Convert(VarChar,@dEnd,
> 101) AS hdrLine
>
> "C-Services Holland b.v." wrote:
>
>> David Davis wrote:
>> > Bernie
>> >
>> > Thanks for the code.
>> >
>> > I'm able to assign values to the parameters. The only problem I have
>> > now is
>> > that I get a syntax error
>> >
>> > "Syntax error converting datetime from character string."
>> >
>> > How do I pass a datetime value to a datetime parameter. Following is
>> > the
>> > code for the parameters.
>> >
>> > Dim dStart, dEnd As SqlParameter
>> > dStart = SprocCommand.Parameters.Add("@dStart",
>> > SqlDbType.DateTime, 8)
>> > dEnd = SprocCommand.Parameters.Add("@dEnd", SqlDbType.DateTime,
>> > 8)
>> > dStart.Direction = ParameterDirection.Input
>> > dEnd.Direction = ParameterDirection.Input
>> > dStart.Value = Me.dteStart.DateTime.ToShortDateString
>> > dEnd.Value = Me.dteEnd.DateTime.ToShortDateString
>> >
>> > Thanks
>> >
>> > David Davis
>> >
>> > "Bernie Yaeger" wrote:
>> >

>>
>> about this: dStart.Value = Me.dteStart.DateTime.ToShortDateString
>> Why do you convert it to shortdatestring?
>>
>>
>> --
>> Rinze van Huizen
>> C-Services Holland b.v.
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling stored procedure using c++ =?Utf-8?B?Qm95ZA==?= Microsoft Dot NET 0 17th Aug 2007 01:44 AM
calling a stored procedure in ADO.NET 2.0 David Sagenaut Microsoft ADO .NET 12 15th Nov 2005 12:05 AM
calling stored procedure in ADO.NET 2.0 David Sagenaut Microsoft ASP .NET 1 24th Oct 2005 03:19 PM
calling a stored procedure in ADO.NET 2.0 David Sagenaut Microsoft C# .NET 1 24th Oct 2005 05:11 AM
Calling a Stored Procedure in a Stored Procedure Group DJM Microsoft ADO .NET 1 21st Jan 2004 08:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:24 PM.