Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 Other Development Tools
 Testing to execute SQL statment.

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2005-12-21 : 02:53:02
I am writting a script to auto input info into a SQL 2k DB Table and in it I need to do a check to make sure an SQL statment can be ran is there a way to do this in either TSQL or PERL? This is the code (I even formated it):

#!user/local/bin/perl -w
use strict;
use DBI;
use GD;
my ($myobject, $srcimage, $base_path, @games, $game, @subsets,
$subset, $game_name, $subset_name, @images, $image, $width, $height, $alt,
$dbh, $sth, $number, @resultholder, $resultholder);
$base_path = "f:/FFimages/Compleated";
$alt = 'None';
opendir (BASEDIR, "$base_path") or die "Error opening $base_path";
@games = grep {-d "$base_path/$_" && !/\./} readdir(BASEDIR);
closedir (BASEDIR);
foreach $game (@games){
$game_name = $game;
opendir (GAMEDIR, "$base_path/$game");
@subsets = grep {-d "$base_path/$game/$_" && !/\./} readdir (GAMEDIR); #Error Line
foreach $subset (@subsets){
$subset_name = $subset;
opendir (SUBSETDIR, "$base_path/$game/$subset");
@images = grep {/\.jpg/} readdir (SUBSETDIR);
foreach $image (@images){
$myobject = "$base_path/$game/$subset/$image";
$srcimage = GD::Image -> newFromJpeg($myobject);
if ($srcimage) {
($width, $height) = $srcimage -> getBounds();}
$dbh = DBI -> connect ('dbi:ODBC:read', 'UID', 'PASS') or die "Could not connect to DB";
$sth = $dbh -> prepare (qq~select ImageNumber from images where SubSet = "$subset_name" order by ImageNumber desc~);
$sth -> execute ();
@resultholder = $sth -> fetchrow_array;
if ($resultholder[0]) {
$number = $resultholder[0];
} else {
$number = 0;
}
print $number;
$sth = $dbh -> prepare (qq~insert into images (Game, SubSet, Image, Width, Height, AltText, ImageNumber) values ('$game_name', '$subset_name', '$image', $width, $height, '$alt', $number)~);
$sth -> execute();
$dbh -> disconnect();}}}

The statment I want to check is the select statment.

--
If I get used to enving others...
Those things about my self I pride will slowly fade away.
-Stellvia

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2005-12-21 : 08:14:58
What Query Analyzer does when you press the button to "Parse Query" is add a "SET PARSEONLY ON" statement before the query, and "SET PARSEONLY OFF" after the query.

You could add those statements in your code if it's in a "test mode" and trap for errors.


Damian
"A foolish consistency is the hobgoblin of little minds." - Emerson
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-12-21 : 08:19:49
You might want to consider using my Handy-Dandy page timer routine which could easily be ported to php:


Dim intDeltaT ' Declared outside Sub HTMLTimer to preserve values between calls.

Sub HTMLTimer (strParm)
IF intDeltaT = 0 Then intDeltaT = timT1 ' Setup intDeltaT once. timT1 = timer() on page load
intTnow = timer()
Response.WRite vbCrLf & "<!--" & vbCrLf & _
FORMATNUMBER(intTnow-timT1, 4) & " sec from page top. " & _
FORMATNUMBER(intTnow-intDeltaT, 4) & " sec from last check. | " & _
strParm & vbCrLf & " -->"
intDeltaT = intTnow
End Sub


This get's two jobs done. It writes an HTML comment with the time in milliseconds since page load [handy for timing database calls] and it writes out a comment which can contain the SQL command you are about to execute.

If the page fails for any reason, you can view source, cut and paste the command into Query Analyzer. This is handy whether you are calling stored procedures or executing dynamic SQL as you are here.
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2005-12-21 : 08:23:19
How does that help with sql syntax Sam ?


Damian
"A foolish consistency is the hobgoblin of little minds." - Emerson
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-12-21 : 08:28:06
quote:
Originally posted by Merkin

How does that help with sql syntax Sam ?

It'll put the dynamic SQL string in an HTML comment so it can be cut / paste into QA for analysis - "after the fact". I've had dynamic SQL work fine for days, then suddenly break. Usually a parameter problem. It's nice to view / source and have a copy of the offending command.

Beats figuring out what the command "probably" looked like.

It ain't perfect, but it's been helpful on several occasions.

I've also had page load time skyrocket for no reason. If there are several DB calls on the page, printing HTML comments with the milliseconds from page start to page end can pinpoint the SQL that is blowing up the time.
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2005-12-21 : 08:36:59
mmmm..... StoredProcs, StoredProcs, StoredProcs - dynamic and inline sql just makes things confusing, That's my Humble Opinion.

Duane.
"It's a thankless job, but I've got a lot of Karma to burn off."
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2005-12-21 : 14:51:51
Thanks but non of that is working. The problem is in the scripts it is trying to pull the infor form ImageNumber for when it adds new images but if no preexisting entry is in the DB for the subset I get an SQL error of invalid colum name. So I need some way to test in Perl or SQL to make sure that colum is there or not so I do not get this error.

--
If I get used to enving others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2005-12-21 : 15:13:01
Nevermind I found a round about way of doing this.

--
If I get used to enving others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page
   

- Advertisement -