PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > I want to return a string to a wrapper from a subordinate stored procedure
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
I want to return a string to a wrapper from a subordinate stored procedure

Réponse
 
LinkBack Outils de la discussion
Vieux 27/09/2007, 21h36   #1
bobc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut I want to return a string to a wrapper from a subordinate stored procedure

Using SQL Server 2000...

I wrote a wrapper to call a sub proc (code provided below). The
intended varchar value returned in the output parameter of each proc
is a string implementation of an array.
(The string separates elements by adding a period after each value.
e.g. 1. 2. 3. 4. 5. etc., although my simplified example only creates
two elements.)
My vb.net calling code parses the returned string into individual
elements.

I TESTED BOTH PROCS FIRST:
The wrapper returns 'hello' when I test it by inserting
SELECT @lString='hello'
before the GO, so I believe it is called properly.

The sub_proc returns the "array" I want when I call it directly.

THE PROBLEM: When I call the wrapper, and expect it to call sub_proc,
it returns a zero.
In fact, when I assign a literal (like 'hello') to @lString in
sub_proc, 'hello' is not returned.
So the wrapper is not calling the sub_proc, or the sub_proc is not
returning an output value.
OR...I have read about some issues with OUTPUT string parameters being
truncated or damaged somehow when passed. I doubt this is the
problem, but I'm open to anything.

I want to use the wrapper because, when it's finally working, it will
call several sub_procs and
return several output values.

Any thoughts? Thanks for looking at it! - Bob

The Wrapper:
-----------------------------------------------------------------
CREATE PROCEDURE wrapper
@lString varchar(255) OUT
AS

EXEC @lString = sub_proc @CommCode, @lString OUT
GO
-----------------------------------------------------------------

The subordinate procedure:
-----------------------------------------------------------------
CREATE PROCEDURE sub_proc
@lString varchar(255) OUT

AS

DECLARE @var1 int,
@var2 int

SELECT @var1 =
(SELECT count(mycolumn)
FROM mytable
WHERE condition=1)

SELECT @var2 =
(SELECT count(mycolumn)
FROM mytable
WHERE condition=2)

/* If @var1 returns 5 and @var2 returns 7, Then @lString below would
be "5. 7." */

SELECT @lString = STR(@var1) + '.' + STR(@var7) + '.'
GO
-----------------------------------------------------------------

  Réponse avec citation
Vieux 27/09/2007, 21h44   #2
bobc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: I want to return a string to a wrapper from a subordinate stored procedure

On Sep 27, 3:36 pm, bobc <bcana...@fmbnewhomes.com> wrote:
> Using SQL Server 2000...
>
> I wrote a wrapper to call a sub proc (code provided below). The
> intended varchar value returned in the output parameter of each proc
> is a string implementation of an array.
> (The string separates elements by adding a period after each value.
> e.g. 1. 2. 3. 4. 5. etc., although my simplified example only creates
> two elements.)
> My vb.net calling code parses the returned string into individual
> elements.
>
> I TESTED BOTH PROCS FIRST:
> The wrapper returns 'hello' when I test it by inserting
> SELECT @lString='hello'
> before the GO, so I believe it is called properly.
>
> The sub_proc returns the "array" I want when I call it directly.
>
> THE PROBLEM: When I call the wrapper, and expect it to call sub_proc,
> it returns a zero.
> In fact, when I assign a literal (like 'hello') to @lString in
> sub_proc, 'hello' is not returned.
> So the wrapper is not calling the sub_proc, or the sub_proc is not
> returning an output value.
> OR...I have read about some issues with OUTPUT string parameters being
> truncated or damaged somehow when passed. I doubt this is the
> problem, but I'm open to anything.
>
> I want to use the wrapper because, when it's finally working, it will
> call several sub_procs and
> return several output values.
>
> Any thoughts? Thanks for looking at it! - Bob
>
> The Wrapper:
> -----------------------------------------------------------------
> CREATE PROCEDURE wrapper
> @lString varchar(255) OUT
> AS
>
> EXEC @lString = sub_proc @CommCode, @lString OUT
> GO
> -----------------------------------------------------------------
>
> The subordinate procedure:
> -----------------------------------------------------------------
> CREATE PROCEDURE sub_proc
> @lString varchar(255) OUT
>
> AS
>
> DECLARE @var1 int,
> @var2 int
>
> SELECT @var1 =
> (SELECT count(mycolumn)
> FROM mytable
> WHERE condition=1)
>
> SELECT @var2 =
> (SELECT count(mycolumn)
> FROM mytable
> WHERE condition=2)
>
> /* If @var1 returns 5 and @var2 returns 7, Then @lString below would
> be "5. 7." */
>
> SELECT @lString = STR(@var1) + '.' + STR(@var7) + '.'
> GO
> -----------------------------------------------------------------


Correction: delete "@CommCode," from the EXEC statement in wrapper.
Should read as follows:

EXEC @lString = sub_proc @lString OUT

It's been a long day. -BobC

  Réponse avec citation
Vieux 27/09/2007, 23h38   #3
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: I want to return a string to a wrapper from a subordinate stored procedure

bobc (bcanavan@fmbnewhomes.com) writes:
> The Wrapper:
> -----------------------------------------------------------------
> CREATE PROCEDURE wrapper
> @lString varchar(255) OUT
> AS
>
> EXEC @lString = sub_proc @CommCode, @lString OUT
> GO


Remove "@lString =". The return value from a stored procedure is
always integer, and customary you use it to return success/failure
indication, with 0 meaning success.

> DECLARE @var1 int,
> @var2 int
>
> SELECT @var1 =
> (SELECT count(mycolumn)
> FROM mytable
> WHERE condition=1)
>
> SELECT @var2 =
> (SELECT count(mycolumn)
> FROM mytable
> WHERE condition=2)


Rather you can do:

SELECT @lString =
ltrim(str(SUM(CASE condition WHEN 1 THEN 1 ELSE 0 END)) + '.' +
ltrim(str(SUM(CASE condition WHEN 2 THEN 1 ELSE 0 END)) + '.' +
...
ltrim(str(SUM(CASE condition WHEN 7 THEN 1 ELSE 0 END))
FROM mytable
WHERE mycolumn IS NOT NULL
AND condition BETWEEN 1 AND 7

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Vieux 28/09/2007, 16h56   #4
bobc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: I want to return a string to a wrapper from a subordinate stored procedure

On Sep 27, 5:38 pm, Erland Sommarskog <esq...@sommarskog.se> wrote:
> bobc (bcana...@fmbnewhomes.com) writes:
> > The Wrapper:
> > -----------------------------------------------------------------
> > CREATE PROCEDURE wrapper
> > @lString varchar(255) OUT
> > AS

>
> > EXEC @lString = sub_proc @CommCode, @lString OUT
> > GO

>
> Remove "@lString =". The return value from a stored procedure is
> always integer, and customary you use it to return success/failure
> indication, with 0 meaning success.
>
> > DECLARE @var1 int,
> > @var2 int

>
> > SELECT @var1 =
> > (SELECT count(mycolumn)
> > FROM mytable
> > WHERE condition=1)

>
> > SELECT @var2 =
> > (SELECT count(mycolumn)
> > FROM mytable
> > WHERE condition=2)

>
> Rather you can do:
>
> SELECT @lString =
> ltrim(str(SUM(CASE condition WHEN 1 THEN 1 ELSE 0 END)) + '.' +
> ltrim(str(SUM(CASE condition WHEN 2 THEN 1 ELSE 0 END)) + '.' +
> ...
> ltrim(str(SUM(CASE condition WHEN 7 THEN 1 ELSE 0 END))
> FROM mytable
> WHERE mycolumn IS NOT NULL
> AND condition BETWEEN 1 AND 7
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx


Thanks very much, Erland! -BobC

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h32.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13271 seconds with 12 queries