473,395 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Transact SQL probklems with variable scope

I have 24 tables named tblData1 ... tblData24 and I have a scheduled
job that runs successfully to delete all data older than 31 days.

My problem is that I need to keep at least one record in each table
for the aggregate function max() to work in one of my application's
functions, as if there are no records the result is null.
Although I have figured out a workaround in the function using max() I
would like to know how to change my script.

Functionally I would like to get the max() value of the ID column
(autoincrementing) and then add to the where "And ID <> @maxID".
I have tried a few options and come unstuck with scope of variables,
and tried to use a temp table to store the max values for the 24
tables and got no where. Can anyone help ?
Working script without the @maxID bit:-
DECLARE @days VARCHAR(12)
DECLARE @intData int
DECLARE @SQL1 VARCHAR(2000)
set @Days = 31
set @intData = 1

While @intData<=24
Begin
SET @SQL1 = 'DELETE FROM [DB1_SQL].[dbo].[tblData'+
rtrim(CONVERT(char(2), @intData)) + '] Where
datediff(Day,Datim,getdate())> '+ @days

EXEC(@SQL1)

/*print @SQL1*/

set @intData= @intData + 1

End

go
Jul 20 '05 #1
2 1564
On 1 Oct 2004 00:26:09 -0700, B Moor wrote:
I have 24 tables named tblData1 ... tblData24 and I have a scheduled
job that runs successfully to delete all data older than 31 days.

My problem is that I need to keep at least one record in each table
for the aggregate function max() to work in one of my application's
functions, as if there are no records the result is null.
Although I have figured out a workaround in the function using max() I
would like to know how to change my script.

Functionally I would like to get the max() value of the ID column
(autoincrementing) and then add to the where "And ID <> @maxID".
I have tried a few options and come unstuck with scope of variables,
and tried to use a temp table to store the max values for the 24
tables and got no where. Can anyone help ?
You don't need @maxID as a variable; you can use a subselect. See what I
added below:

Working script without the @maxID bit:-
DECLARE @days VARCHAR(12)
DECLARE @intData int
DECLARE @SQL1 VARCHAR(2000)
set @Days = 31
set @intData = 1

While @intData<=24
Begin
SET @SQL1 = 'DELETE FROM [DB1_SQL].[dbo].[tblData'+
rtrim(CONVERT(char(2), @intData)) + '] Where
datediff(Day,Datim,getdate())> '+ @days + ' AND ID <> ( SELECT MAX(ID) FROM [DB1_SQL].[dbo].[tblData'+
rtrim(CONVERT(char(2), @intDat)) + '] )'
EXEC(@SQL1)

/*print @SQL1*/

set @intData= @intData + 1

End

go

Jul 20 '05 #2
thank you , worked a treat
Jul 20 '05 #3

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

Similar topics

6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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...

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.