473,382 Members | 1,726 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,382 software developers and data experts.

Executing a multiple line statement

Hello:

I'm having a problem formulating the code to execute a multiple line
command. I'm trying to execute something like:

set rowcount 100000
declare @rowct int
select @rowct = 1
while (@rowct > 0)
begin
delete Foo where creationDate < dateadd(day, -5, getdate())
select @rowct = @@rowcount
end

I know the change to rowcount will persist, but I'm having problems
formulating the rest of it. Specifically, I'm having problems
declaring the variable and executing the while loop.

$query = "declare @rowct int \n select @rowct = 1 \n" doesn't seem to
be working probably due to the interpretation of the "\n".

I was unable to find anything at perldoc or cpan searching for DBD or
DBI modules. Any assistance would be appreciated.

Thanks

John
Jul 19 '05 #1
8 11714
MrTrix wrote:
I'm having a problem formulating the code to execute a multiple line
command. I'm trying to execute something like:

set rowcount 100000
declare @rowct int
select @rowct = 1
while (@rowct > 0)
begin
delete Foo where creationDate < dateadd(day, -5, getdate())
select @rowct = @@rowcount
end


This has not even a remote resemblance to Perl.
Are you sure you are in the right NG?

jue
Jul 19 '05 #2
Hello:

Sorry about the confusion. This is a question about formulating
Sybase SQL queries in Perl. Let me put it into more of a perl
context:

I can certainly do single command lines in perl using:

$dbh = DBI->connect("dbi:Sybase:server=$dbHost", $dbUser, $dbPass);

if (!defined $dbh) { death ("Could not connect to database\n."); }
else { print LOGFILE "Connected to database.\n"; }

$query = "use ${dbDatabase}";
$sth = $dbh->prepare(${query});
$sth->execute;

$query = "set rowcount 100000";
$sth = $dbh->prepare(${query});
$sth->execute;

However, as fas as I can tell Sybase needs to have variables in the
executable block that they are used. So, I can't do something like:

$query = " declare @rowct int";
$sth = $dbh->prepare(${query});
$sth->execute;

$query = "select @rowct =1";
$sth = $dbh->prepare(${query});
$sth->execute;

Nor can I combine statements to do something like:

$query = "declare @rowct int\n select @rowct =1";
$sth = $dbh->prepare(${query});
$sth->execute;

So, my problem is that I have to find a way to group the following SQL
statements together and have them execute at once:

declare @rowct int
select @rowct = 1
while (@rowct > 0)
begin
delete Foo where creationDate < dateadd(day, -5, getdate())
select @rowct = @@rowcount
end

Thanks,

John

"Jürgen Exner" <ju******@hotmail.com> wrote in message news:<0H*******************@nwrddc01.gnilink.net>. ..

This has not even a remote resemblance to Perl.
Are you sure you are in the right NG?

jue

Jul 19 '05 #3
What is the statement separator in Sybase SQL ? In MySQL, you can execute
mulitple queries by separating them with a ";". Perhaps, you can do
something similar in Sybase SQL.
"MrTrix" <eu********@yahoo.com> wrote in message
news:f1**************************@posting.google.c om...
Hello:

Sorry about the confusion. This is a question about formulating
Sybase SQL queries in Perl. Let me put it into more of a perl
context:

I can certainly do single command lines in perl using:

$dbh = DBI->connect("dbi:Sybase:server=$dbHost", $dbUser, $dbPass);

if (!defined $dbh) { death ("Could not connect to database\n."); }
else { print LOGFILE "Connected to database.\n"; }

$query = "use ${dbDatabase}";
$sth = $dbh->prepare(${query});
$sth->execute;

$query = "set rowcount 100000";
$sth = $dbh->prepare(${query});
$sth->execute;

However, as fas as I can tell Sybase needs to have variables in the
executable block that they are used. So, I can't do something like:

$query = " declare @rowct int";
$sth = $dbh->prepare(${query});
$sth->execute;

$query = "select @rowct =1";
$sth = $dbh->prepare(${query});
$sth->execute;

Nor can I combine statements to do something like:

$query = "declare @rowct int\n select @rowct =1";
$sth = $dbh->prepare(${query});
$sth->execute;

So, my problem is that I have to find a way to group the following SQL
statements together and have them execute at once:

declare @rowct int
select @rowct = 1
while (@rowct > 0)
begin
delete Foo where creationDate < dateadd(day, -5, getdate())
select @rowct = @@rowcount
end

Thanks,

John

"Jürgen Exner" <ju******@hotmail.com> wrote in message news:<0H*******************@nwrddc01.gnilink.net>. ..

This has not even a remote resemblance to Perl.
Are you sure you are in the right NG?

jue

Jul 19 '05 #4
eu********@yahoo.com (MrTrix) wrote in message news:<f1**************************@posting.google. com>...
Hello:

I'm having a problem formulating the code to execute a multiple line
command. I'm trying to execute something like:

set rowcount 100000
declare @rowct int
select @rowct = 1
while (@rowct > 0)
begin
delete Foo where creationDate < dateadd(day, -5, getdate())
select @rowct = @@rowcount
end

I know the change to rowcount will persist, but I'm having problems
formulating the rest of it. Specifically, I'm having problems
declaring the variable and executing the while loop.

$query = "declare @rowct int \n select @rowct = 1 \n" doesn't seem to
be working probably due to the interpretation of the "\n".


My advice would be to do your programming in Perl, rather than trying
to write Sybase code. That is, write the loop code in perl and have
several parameterized SQL statements for interacting with the database
as necessary. I don't know what the block is supposed to be doing. Is
there some reason you can't write it in Perl?

I can't speak for Sybase, but DBD::Oracle will accept blocks of
PL/SQL, if that's what I want to do. For that, each line would need to
have a semicolon on the end, and the whole thing would need to be
wrapped in a BEGIN/END. But that's Oracle, where such blocks are
processed as single statements. The rule is that you can only pass a
single statement via DBD. If Sybase understands the block above as
multiple statements, which are handled by its interactive interpreter,
you're out of luck. (In Oracle, an equivalent thing that wouldn't work
would be declaring something as VARIABLE.)
Jul 19 '05 #5
If Sybase has a line terminator, I'm not aware of it. I may have to
do it programatically in Perl...

Thanks!
Jul 19 '05 #6
eu********@yahoo.com (MrTrix) wrote in message news:<f1**************************@posting.google. com>...
Hello:

I'm having a problem formulating the code to execute a multiple line
command. I'm trying to execute something like:

set rowcount 100000
declare @rowct int
select @rowct = 1
while (@rowct > 0)
begin
delete Foo where creationDate < dateadd(day, -5, getdate())
select @rowct = @@rowcount
end

I know the change to rowcount will persist, but I'm having problems
formulating the rest of it. Specifically, I'm having problems
declaring the variable and executing the while loop.


You simply need to escape the '@' signs, like so:

my $sql = "
set rowcount 100000
declare \@rowct int
select \@rowct = 1
while (\@rowct > 0)
begin
delete Foo where creationDate < dateadd(day, -5, getdate())
select \@rowct = \@\@rowcount
end
";

Note that you really should reset rowcount to 0 after completing the operation.

Michael
Jul 19 '05 #7
It worked!!! Thanks so much for your help

- john
Jul 19 '05 #8
eu********@yahoo.com (MrTrix) wrote in message news:<f1**************************@posting.google. com>...
It worked!!! Thanks so much for your help

- john


If you're not interpolating variables or special characters, you
should be using single quotes, anyway.
Jul 19 '05 #9

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

Similar topics

2
by: Richard Adams | last post by:
Is it possible to execute more than one statement in SQL via MDAC ODBC? I have a fairly complex select I wanted to create a view with, but trying to send it all as one string with terminators ';'...
6
by: Alex Vilner | last post by:
Hello! We have a set of individual .SQL scripts which we would like to execute against a MS SQL Server 2000. Is there any way to have ISQL utility (or any other means) to execute all of them...
7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
3
by: Dean g | last post by:
Hi, I have a problem with running multiple sql statements using asp. Basically if there is an error with any of the statements inside the query a rollback is done. the sql and rollback work fine,...
10
by: Heiko Pliefke | last post by:
Hi NG! I wrote a stored procedure which at first deletes 100.000 rows and then inserts 100.000 new rows. There is a huge difference between executing this SP in the query-analyzer (runtime...
10
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
7
by: tshad | last post by:
I thought I understood how the SaveViewState is working and was trying to use this (as per some code I found) to detect refreshes. It seemed to be working but I found that the SaveViewState was...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
1
by: bill | last post by:
Here's a question about getting the most out of the interface when executing SQL in the interactive window of M In TOAD (a popular front end for Oracle by Quest Software), the equivalent of the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.