473,503 Members | 11,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Maintaining Variable After EXEC

Hello,

I am fairly new at stored procedures. I have created some that will
go through a table and return a start date and an end date that is
dependent upon the fiscal period you want, but I then need to use
those dates in another stored procedure to retrieve the information I
need. My stored procedure looks like this.

================================================== ====================

CREATE PROCEDURE dbo.R920ExtTotal
@MthsBack Decimal OUTPUT
AS

DECLARE @sSQL AS NVARCHAR(255), @StartDate as SMALLDATETIME, @EndDate
as SMALLDATETIME

Exec @StartDate = GetMthStart @MthsBack

Exec @EndDate = GetMthEnd @MthsBack

SET @sSQL = 'Select count(extension) as Total From r920f00 Where
([date] BETWEEN "' +
CONVERT(nvarchar, @StartDate) +
'" and "' +
CONVERT(nvarchar, @EndDate) +
'")'

Select @sSQL

EXEC (@sSQL)

Return
GO

================================================== ===============

The problem is my variables @StartDate and @EndDate do not retain
their values after the EXEC statement and revert to 01/01/1900. How
can I get around this problem?

Thanks!!!!

Chip
Jul 20 '05 #1
2 8906
Variables are only available in the scope within which they are declared. If
you need to move values out of a stored procedure, you must use output
parameters for all of them. Also, why are you building dynamic SQL in your
procedure? You can use the variables directly in SQL queries - there is no
need to convert them to strings, and insert them into a SQL string.

On 6 Jan 2004 11:05:01 -0800, cm****@lesliecontrols.com (Chip Mayan) wrote:
Hello,

I am fairly new at stored procedures. I have created some that will
go through a table and return a start date and an end date that is
dependent upon the fiscal period you want, but I then need to use
those dates in another stored procedure to retrieve the information I
need. My stored procedure looks like this.

================================================= =====================

CREATE PROCEDURE dbo.R920ExtTotal
@MthsBack Decimal OUTPUT
AS

DECLARE @sSQL AS NVARCHAR(255), @StartDate as SMALLDATETIME, @EndDate
as SMALLDATETIME

Exec @StartDate = GetMthStart @MthsBack

Exec @EndDate = GetMthEnd @MthsBack

SET @sSQL = 'Select count(extension) as Total From r920f00 Where
([date] BETWEEN "' +
CONVERT(nvarchar, @StartDate) +
'" and "' +
CONVERT(nvarchar, @EndDate) +
'")'

Select @sSQL

EXEC (@sSQL)

Return
GO

================================================= ================

The problem is my variables @StartDate and @EndDate do not retain
their values after the EXEC statement and revert to 01/01/1900. How
can I get around this problem?

Thanks!!!!

Chip


Jul 20 '05 #2
Chip Mayan (cm****@lesliecontrols.com) writes:
I am fairly new at stored procedures. I have created some that will
go through a table and return a start date and an end date that is
dependent upon the fiscal period you want, but I then need to use
those dates in another stored procedure to retrieve the information I
need. My stored procedure looks like this.

================================================= =====================

CREATE PROCEDURE dbo.R920ExtTotal
@MthsBack Decimal OUTPUT
AS

DECLARE @sSQL AS NVARCHAR(255), @StartDate as SMALLDATETIME, @EndDate
as SMALLDATETIME

Exec @StartDate = GetMthStart @MthsBack

Exec @EndDate = GetMthEnd @MthsBack

SET @sSQL = 'Select count(extension) as Total From r920f00 Where
([date] BETWEEN "' +
CONVERT(nvarchar, @StartDate) +
'" and "' +
CONVERT(nvarchar, @EndDate) +
'")'

Select @sSQL

EXEC (@sSQL)

Return
GO


I'm afraid that there are a couple of errors or strange things in this
procedure.

First: there is absolutely no reason to use dynamic SQL here. Just write:

SELKCT count(extension) AS Total
FROM r920f00
WHERE [date] BETWEEN @StartDate AND @EndDate

Second: the calls to set @StartDate and @EndDate looks funny. If
GetMthStart and GetMthEnd are user-defined functions it would be alright,
but you indicated that they were stored procedures. The return value from
a stored procedure is always an integer value, so you cannot return a
date here. And I would strongly recommend you to use return values solely
for indication of success/failure (with 0 meaning success, and about
everything else meaning failure.) So you would have to make the output
parameters:

EXEC GetMthStart @MthsBack, @StartDate OUTPUT

Third: the @MthsBack parameter is declared as output, but you never assign
it any value, you only seem to use it for input.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
17351
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
8
97354
by: Mario Pranjic | last post by:
Hi! Can anybody give me a hint how to put sa resut from EXEC into a variable. EXEC is called: EXEC(@TmpQuery) and it returns a single int value (SELECT COUNT(*) ....) Thanks!
4
12065
by: Greg | last post by:
I need to send the result of a procedure to an update statement. Basically updating the column of one table with the result of a query in a stored procedure. It only returns one value, if it didnt...
5
13359
by: Aamer Nazir | last post by:
Hi, I am having problems setting the value of a variable in a SQL String that I have to create dynamically in my procedure. The code that I currently have is as follows: set...
3
2401
by: Thomas R. Hummel | last post by:
Hello, I have a large set of data that I have set up as a partitioned view. The view is partitioned by a datetime column and the individual tables each represent one month's worth of data. I...
6
51831
by: Dipak Patel | last post by:
Platform: MS SQL 7.00 - 7.00.1063 Standard Edition / Win2000 I wish to store the numeric result of a query into a variable, as described below. This does not work, it fails on the last line with...
0
1394
by: RickBen | last post by:
I have written a stored proc to write order details to the db. If there is additional description text, I am writting it out to another note table. i initially performed this using EXEC with...
11
2233
by: mfglinux | last post by:
Hello to everybody I would like to know how to declare in python a "variable name" that it is in turn a variable In bash shell I would wrote sthg like: for x in `seq 1 3` do M$i=Material(x)...
2
7693
by: spattag | last post by:
I am trying to script the process RESTORE FILELISTONLY using an Exec() statement. I cant seem to get it to work with a variable. Here is what I have. Declare @Sql varchar(100) Declare @File...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.